personal learning note for JAVA
1.create MyFirstJavaClass.java (the class name (CamelCase) is same as the file name.)
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World");
}
}
2.javac MyFirstJavaClass.java to created MyFirstJavaClass.class
3.java MyFirstJavaClass to run MyFirstJavaClass
use super() to inheritate parent constructions and functions
use @Override if child function needs to override parent function
to tell the operation system the jdk path. For example: Path %JAVA_HOME%\bin
byte, short, int(default), long
float, double (default), char, boolean
String
auto type transfer (byte,short,char) == int -> long -> float -> double
type a = 12
int b = a
System.out.println(b) // 12
**two digit
System.out.printf("%.2f\n", d);
force type transfer
int a = 20;
byte b = (byte)a;
"/" int divide int is still int. e.g. 5 / 2 = 2. To get 5 / 2 = 2.5, use 1.0 * 5 / 2 = 2.5
++,-- :plus 1, minus 1
int a = 10;
a++ // a=11;
int i = 10;
int rs = ++1;
// plus first, then set value
// rs=11; i=11;
int j = 10;
int rs2 = j++;
// set value first, then plus
// rs2=11; j=10;
=,+=,-=,*=, /=, %= // calculate first, then set value
, >=,<,<=, ==, !=
&, |, !, ^ , &&, ||
&& is Short-circuit evaluation, only run left logic if left is false. its result is the same as &
&& is prior to ||
*/ is prior to +-
^ return if two logic are different
2 >1 ^ 3>1 // =false,
if, switch, for, while, do{}while(),
import java.util..Random;
Random r = new Random();
int data = r.nextInt(10); // 0-9
int[] arr = {20,10,80};
String[] arr2 = {"Adam","Ben", "Charlie"};
int arr3[] = {1,2,3};
int[] arr = new int[3] // create an array whose length is 3
print multidimensional Array: Arrays.deepToString()
allow user to input a double
import java.util.Scanner
Scanner sc = new Scanner(System.in)
double score = sc.nextDouble()
void
public static int sum(int a,int b){
int c = a+b;
return c; // type is the same as the return type above
}
init Class Object
public Student(){
}
public Student(String name, double score){
}
private, public
import package.class
java.lang.String (No need to import java.lang.*)
String name = "test"; (string is unchanged object)
String str = public String(STRING/CHAR[]/BYTE[]);
length()
charAt(int index)
toCharArray()
equals(Object anObject)
String s1="abc";
String s2="ab";
String s3=s2+"c;
String s4="a"+"b"+"c"
// s1 == s3 returns false;
// s1.equals(s3) returns true;
// s1 == s4 returns true;
equalsIgnoreCase(String str)
substring(int startIndex, int endIndex?)
replace(CharSequence target, CharSquence replacement)
contains(CharSequence s)
startsWith(String prefix)
split(String regex)
java.util.ArrayList ArrayList()
add(E element) // to the last
add(int index, E element)
remove(int index);
remove(Object o);
get(int index)
size()
set(int index,E element)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // create Scanner
System.out.print("Input your name: ");
String name = scanner.nextLine(); // input string
System.out.print("Input your age: ");
int age = scanner.nextInt(); // input integer
System.out.printf("Hi, %s, you are %d\n", name, age);
}
}
public class Main {
public static void main(String[] args) {
for (String arg : args) {
if ("-version".equals(arg)) {
System.out.println("v 1.0");
break;
}
}
}
}
cannot be used with 'new', only for inheritage.
abstract class Person {
public abstract void run();
}
interface is the abstract class with only functions
class can implements more than 1 interface