MacOS java development 2 variables and constants

Posted by riex1 on Thu, 24 Feb 2022 16:24:11 +0100

MacOS java development 2 variables and constants

constant

Constants: data that cannot be changed

Data, keywords and identifiers are all components of JAVA programs

Constant category:

Integer 100, 23, 1

Decimals 3.14, 21.231

Character - enclosed in single quotation marks, can only write one character and must have a value

String - enclosed in double quotation marks. Multiple characters can be written or empty

Boolean constant - true or false (in java, 0 or non-0 integers cannot be used instead of true and false)

Null null reference data type

public class Study(){
  public static void main(String[] args) {
		//1. Integer
		System.out.println(2020);
		//2. Decimal
		System.out.println(3.14);
		//3. Character - if enclosed in single quotation marks, only one character can be written and there must be content
		System.out.println('a');
		System.out.println('Left');
//	System.out.println('1234567'); Illegal character constant
		//4. String - enclosed in double quotation marks. Multiple characters can be written or left blank
		System.out.println("abcdefg");
		System.out.println("Light yellow dress and fluffy hair");
		System.out.println("");
		//5. Boolean constant - true or false (pay attention to spelling)
		System.out.println(true);
		System.out.println(false);
	}
}

variable

Variable: data that can be changed

Essence: it is to open up a space in the memory space, which has the specified storage type, variable name and value.

1. Different data have different types, and the space occupied by different types is different

2. Variable name: identifier - use the small hump naming method, such as bigSur, goodBoy

3. The "=" assignment operator passes the data value to the variable name

be careful:

1. Variable name cannot be duplicate

2. "=" variable declaration and initialization assignment can be in one statement or multiple statements

int a=12
int b=24
int c=12,d=24;

3. The declaration can only be made once, and the assignment can be made multiple times. Once the value is re assigned, it will change.

public class Study {
	public static void main(String[] args) {
		//1. Declaration and initialization, in two statements
		
		/*int a;//Declare an integer variable a
		a = 10;//Initialization assignment
*/	
		//2. Declaration and initialization, in one statement
		int a = 10;
		
		//3. One line declaration - multiple variables of the same data type
		int b = 10, c = 20, d = 30;
//		int b = 10,short c = 20,long d = 30; Wrong writing
		int b1 = 10;
		short c1 = 20;
		long d1 = 30;//Correct writing
		
		//4. The declaration can only be declared once, and the assignment can be assigned multiple times. Once the assignment is re assigned, the value has changed
		int e;//Declare an integer variable e
		e = 200;//The initialization assignment is 200
		//int e;// Declaration can only be declared once
		e = 400;//Assignment can be assigned multiple times
		
		//5. Value - write the variable name in the direct output statement
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	}
}

Member variables and local variables

1. Member variable: the variable outside the class method

There is no need to initialize the assignment to have a default value (all 0):

Integer: 0

Floating point: 0.0

Boolean: false(0)

Character type: Space

String (reference data type): null

2. Local variables: variables in class methods

You must initialize the value to use

Attention*

Static modified method if you want to call a variable or method outside the method, the variable and method must also be static modified

public class VariableTest02 {
	//Member variable
	static byte a1;//0  
	public static void main(String[] args) {
		//local variable
		byte a;//statement
		System.out.println(a1);//Because of the static keyword
//		System.out.println(a);a local variable is not initialized and assigned
	}

}

Defining basic data types with variables

//Integer type
		//1 constant of type int
		byte  a=1;//Rarely used
		
		short b=1;
		
		int   c=1;//Most used
		
		long  d=1;//secondly
		
		//If the number of integer constants is very large and int cannot be put down, you need to add l to change this int constant into a long constant
		long  f=13434343434L;
		
		//Floating point type
		//Floating point type constant defaults to double followed by f to convert double into float type
		float m=3.0F;
		
		double w=3.0;
		
		//Character type
		char q='Oh';
		
		//Boolean type
		boolean l=false;
		
		System.out.println(l);
		System.out.println(q);
		

Topics: Java macOS