chapter 1 Null
chapter 2 Basic JAva
commend in terminal in mac:
javac filename.java // compile the file
java filename // run the file
sign:
- all simple statements end of semi colon;
- grouping by condition
if (n > 10){
n++;
} // {} used for grouping
else{
n--;
}
// while loop
while (i < n){
i = 3;
i++;
}
// for loop
for (int i = 1; i < n; i++) {
total += i;
}
// if i++ is in the for loop, it would run by adding one on the second time;
// same as for loop as above but express as while loop
int i = 1;
while (i < n) {
total += i;
i++;
}
i++ and ++i:
- if i++ as independent statment in the method, it runs after the line;
- compare ++i vs. i++ : ++i : runs at the same line
declaring the variable:
int x = 14;
// or
int x;
x = 14;
String:
String y = "string"; // String are object; and 'a' is type char, not string
chat t = y.charAt(1); // find the character in the y at index of 1 (start at 0);
String suby = suby.substring(2,5); // create new string from y start from index of 2 and end at 5 --> "rin";
int count = y.lenth(); // calculate the lenth of the string;
double radius = 10.5; // can have fraction;
final pi = 3.141592658 // final is the value cannot be changed for pi;
output printing:
System.out.println("print out on the new line");
System.out.print("print out on the same line");
Math calculation:
String x = "one";
int y = 2;
System.out.println("x + y euqal to : "+(x+y)); // x + y euqal to : 2string
int n = 5;
System.out.println("y+n" + n + y ); // y+n52 <-- start at string, then others are
string
System.out.println( n + y + "y+n" ); // 7y+n <-- start at two integer, then calcualte tham first
equality
String start = "qwert";
String end = "qwertyui";
end.equals(start); // return true if start have same sequence of characters as end;
if (start == end){
return ture
}
// false, b/c they are not totally the same
inequality (sorting by alphabetically )
String minion = "minion";
String marble = "marble";
int valueOfSame = minion.compareTo(marble);
// 0 if exact euqal; positive is minion alphabeticaly greater than marble; nor ...
input by user
import java.util. *; // import all classes ; or import java.util.Scanner;
// or just decling into the variable: java.util.Scanner name = new java.util.Scanner(System.in);
Scanner scanner = new Scanner(System.in);
int newInt = scanner.newInt():
double newDouble = scanner.nextDouble(); // input type double;
scanner.next(); // input next token as string, delete space on the front, as space create after some characters, it delets the rest of them;
scanner.nextLine(); // input at the current line;
scanner.hasNextInt(); // return true if they only contain integer
void: no return from the method
break: end of loop;
return: return variable of method;
System.exit(n); // normal exit if n = 0;
public static call(int a, String b, double c){
} // static can be called from the main method automatically
vairbale cannot redefine the type :
int a = 4;
double a = 5;// illegal
array:
// no length specified;
int [] NewArrayName;
int NewArrayName[];
NewArrayName = new int[20];
String[] TimeList = new String[]{"morning", "afternoon", "evening"};
int [] ArrayName = int []; // [] = new []
int [][] twoDimentionalArray = int [][];
twoDimentionalArray.length(); // check the size of row;
twoDimentionalArray[0].length(); // check the size of column at row one;
int NewArrayName[][];
twoDimentionalArray[0] // [ ], index start from 0
public static int[][] array( int singleArray[], int[][] twoDArray){
return twoDArray
}
chapter 3 Class
- name of file and name of class should be the same;
- object can instance the class (into the variable was declaring in the class); if the class call "score" and there is variable called "grading", --> can called: score newgrading = score.grading (for the variable in grading)
special method toString:
public String toString(){
}
System.out.println(printOut.toString());
// or
System.out.println(printOut); // toString can automatically print printOut as formate in toString;
Constructor:
- same name as the class name;
- can invoke the method below inside, as class was called, it can run at the same time;
- declaring the variable for the method below
- insert the parameter from the class for method to use as below --> rename them using by "this"
- if no parameter, default as 0; if defined as exiting of parameters, it should have parameters
public/ private
- public: can be used outside of class
- private: can only be used and defined inside the class --> cannot run on class.private name
get/set method:
- can get from outside; limited the variable can be get
- set: set the status for the variable; such as public and private
- use for limitation for access and change the variable compensation
- use public “get” method to access private variable
???
className.getvariable()
this:
- take whole parameters from the class;
- rename it from the parameters
this(variable1,varaible2);// within the same class
?? about this
public void zero() {
classname.variable(this);
} // end zero
java.lang
- automatically import in your system
null:
- empty object can defined as null
List:
// List newList = new ArrayList();
newList.size(); // length or size of the newList