[Java Review] 1: The World of Java

The following notes are based on the book: << Head First JAVA >> and video series: 手把手教你用Java基础教程 - 北京尚学堂 - 马士兵

- Java is a interpreted language

After .java is compiled into .class, JVM takes .class as input, one line at a time, translates a line into machine language to have it executed.

(The other kind is complied language, ex. C/C++)

- Garbage Collection

Java has its own garbage collector to automatically clean up the unused ram. (C/C++ needs coder to manunlly specify the clean-up process)

- Java’s work flow: JVM

IMG_AE01CAEAF6E4-1


- Ram segement
  • heap: new.
  • stack: local data.
  • data segment: static data, const data.
  • code segment: code.
- Terminology

JDK: java development kit (developer)

JRE: java runtime environment (user)

- Run java in shell
1
2
$ javac HelloWorld.class //compile
$ java HelloWolrd //execute
- Rules
  • one .java can have at most only one public class
  • public class, if exists, name has to be same as filename
  • execution entry is fixed public static void main (String[] args)
1
2
3
4
5
public class MyFirstApp{
public static void main (String[] args){
system.out.print("I Rule!"); //statement
} //method
} //class
- Commement
1
2
3
4
5
6
7
8
9
10
11
12
// single line comment

/*
mutiple
lines
comments
*/

/**
* mnutiple lines
* can be interpreted by dos
*/
- Java keywords
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
- Java Variables

java data type.001

1
2
3
4
5
int i = 10; // default inteiger 
long l1 = 8888888L; // 'L' is a must!
float f = 12.3f; // 'f' is a must
double d1, d2, d3 = 0.123; // only d3 is given 0.123;
String s = 'hello';

Smaller types of variables can be cast to larger types:

byte, short, char -> int -> long -> float -> double

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class TestConvert {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i1 = 123;
int i2 = 456;
double d1 = (i1+i2) * 1.2; // (i1+i2) ->double
float f1 = (float) ((i1+i2) * 1.2); // force casting

byte b1 = 1;
byte b2 = 2;
byte b3 = (byte) (b1+b2); // auto casting to int first, must force cast back

double d2 = 1e200;
float f2 = (float) d2; // overflow
System.out.println(f2); // become inf

float f3 = 1.23f;
long l1 = 123; //auto cast to int
long l2 = 300000L;
float f = l1 + l2 + f3; // auto cast to float first
System.out.println(f);
long l = (long) f; // forced to get rid of after=decimal
System.out.println(l);
}
}
- Java operators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* binary oprators
+ - !
& -------- and
| -------- or
^ -------- 异或, 同false异true
&& || ---- 短路与,短路或: 若左表达式能确定整体取值,则不计算右表达式
*/

// triple expression
int score = 80;
String type = score < 60 ? "fail!": "pass!";

// string +
int c = 100;
System.out.println('c = ' + c); // c will be auto cast to string
- Others
  • java uses unicode (UTF-16), 1 character always takses 2 bytes.
0%