Learning Java Day01

Learning Java Day01-- Java basic

1. Java& JDK & JRE& JVM

  1. Java is a program language ,writed by C/C++.

  2. Java programs have to be run at JRE(Java runtime environment).

  3. JRE include the JVM(Java virtual machine) and some labrary files

  4. Java can run at any OS by the different version of JVM.

  5. JDK(java development kits) is the tools for developers. It includes the JRE, of course and the JVM.

2. First Java-language program

public class Demo{
	public static void main(String[] args)
	{
		System.out.print("hello world");
	}
}

  • The “public” is for the scope of the class.

  • The “class” is to define a class named “Demo”

  • The “Demo” is the class name

  • The “public static void main” is to define a main method. It’s the interface of a program.

  • The “String[] args” is to define the arguments needed. the “String” is the type of the arguments.

  • The “System.out.print(“hello world”);” is to make the shell program to print “hello world”.

The .java file(the source file) has to be compiled by the javac.exe, then a .class file(binary file) will be created. And then the java.exe (it can be considered as some kind of interpreter) can execute the .class file

3. The environment variable config

  • configuring the environment is to make sure the cmd shell can find the “java.exe” and “javac.exe” we needed.

4. The syntax of java

4.1 The constants

The constants is the data cannot be changed. they are stocked in the static area.

  1. clarification of the constants: numbers,letters, strings, boolean

4.2 proimitive data types:

Type Size Range
byte 1 B -128 ~127
short 2 B -32768 ~ 32768
int 4 B -2147483648~2147483648
long 8 B -9233372036854477808~9233372036854477808
flout 4 B -3.4E38~3.4E38
double 8 B 1.7E308~1.7E308
char 2 B 0~65535(can store Chinese characters)
boolean 1 B true/false

Note: 1. the type char occupy 2 Bytes in java, it can store a Chinese character; in C/C++, the short char occupy 1 Byte.

4.3 The variables

The variables :a symbolic name associated with a value and whose associated value may be changed. They are stocked at some place in memory .these value can be changed.

  • define a variable:
    1. type modifier + variable’s name. eg: int sum;
    2. type modifier + variable’s name = value. eg: int sum = 99;
Data type conversion:
  • Auto type conversion: when a small-range data type assigned to a wide-range data type.

byte < short < int < long < float < double

public class DataTypeDemo01 {
    public static void main(String[] args) {
        byte b = 100;
        System.out.println(b);
		// from byte to short 
        short s = b;
        System.out.println(s);

        short s1 = 10000;
        //from short to int
        int i = s1;
        System.out.println(i);

        // from int to long
        long l = i;
        System.out.println(l);

        //from long to float
        float f = l;
        System.out.println(f);

        //from float to double
        double d = f;
        System.out.println(d);

    }
}

Note: 1. the byte,short,char is by default valued as integer when do the operation; the float is valued as double.

​ 2.when you need to define a float or long- type variable, you need to append an F or L at end of it just as follows:

​ float i = 4.66F; long j = 15363643L;

  • force type conversion: when a wide-range data type assigned to a small-range data type

format: type modifier + variable’s name = (data type) value

public class DataTypeDemo02 {
    public static void main(String[] args) {
        int i = 100;
        byte b = (byte)i;// force type conversion: from int to byte.
        System.out.println(b);
    }
}

note : cannot do the force type conversion between Boolean and the others.

4.4 the key words

​ In java,a keyword is any one of 57 reserved words that have a predefined meaning in the language; because of this, programmers cannot use keywords as names for variables, methods, classes, or as any other identifier.

some common keyword: public ,static, void ,abstract ,extends ,interface ,etc…

4.5 Identifier

Identifier: In computer languages, identifiers are tokens (also called symbols) which name language entities. Some of the kinds of entities an identifier might invite include variables, types, labels, subroutines, and packages.

//eg:
public class Demo{        // "Demo" is for a name of class
	public static void main(String[] args) {
//"main" is also a kind of identifier, repretenting a kind of method.
        
    int haha = 3;   //"haha" is for a name of variable
    }  
}

Identifier is made up by uppercase and lowercase English letters(az/AZ) ,digits(0~9),the underline、$(dollar symbol). It can only be made up by them. Besides, the digit cannot be the first letter of a variable

4.6.Math and logic operators

Operator Purpose
+ addition of numbers, concatenation of Strings
+= add and assign numbers, concatenate and assign Strings
- subtraction
-= subtract and assign
* multiplication
*= multiply and assign
/ division
/= divide and assign
% take remainder
%= take remainder and assign
++ increment by one
- - decrement by one
!= not equal to
&& short-circuit AND(if one condition is false, the others don’t need to be calculated)
|| short-circuit OR (if one condition is true, the others don’t need to be calculated)
& AND(even one condition is false, the others still need to be calculated)
| OR(even one condition is true, the others still need to be calculated)

5.flow control

​ **if-else, switch , while loop, for loop’**s usage in java is the same as in C.

猜你喜欢

转载自blog.csdn.net/weixin_39749664/article/details/88317951