Use of 7-Variables

Posted by alex.saidani on Sat, 20 Jul 2019 13:52:39 +0200

Use of variables

1. Classification of variables

1.1 Classification by data type

Detailed description:

// 1. Integration byte(1 byte: - 128 - 127) short (2 bytes) int(4 bytes) long(8 bytes)
// In development, the commonly used integers are: int
// To define long variables, you need to end with "L" or "l"
// Integer constants, specified as int type

// 2. Floating-point type float(4 bytes) double(8 bytes)
// Defining float variables requires ending with "F" or "f"
// In development, the commonly used integers are: double
// In terms of table number range, float range is larger than long range.
// Floating-point constants, specified as double type

// 3. Character type char(2 bytes)
// Used to represent a character, this character can be English characters, Chinese characters, numbers, Japanese characters,...
// Usually caused by using a pair of''

// 4. Boolean type: boolean
// boolean type can only be true or false
// Commonly used for conditional judgment, circular judgment and other operations

class VariableTest1 {
	public static void main(String[] args) {
		//1. Integer: byte(1 byte: - 128 - 127)  short (2 bytes)  int(4 bytes)  long(8 bytes)
		// In development, the commonly used integers are: int
		// To define long variables, you need to end with "L" or "l"
		byte b1 = 127;
		

	//b1 = 128;

	short s1 = 4344;

	int i1 = 32432432;

	long l1 = 2353452432L;

	System.out.println("b1 = " + b1);
	System.out.println("s1 = " + s1);
	System.out.println("i1 = " + i1);
	System.out.println("l1 = " + l1);

	//2. Floating point type: float(4 bytes)  double(8 bytes)
	//Defining float variables requires ending with "F" or "f"
	//In development, the commonly used integers are: double
	//In terms of table number range, float range is larger than long range.
	float f1 = 242.33f;
	
	double d1 = 23423423.3423;

	System.out.println("f1 = " + f1);
	System.out.println("d1 = " + d1);

	//3. Character type: char(2 bytes)
	//Used to represent a character, this character can be English characters, Chinese characters, numbers, Japanese characters,...
	//Usually caused by the use of a pair of''
	char c1 = 'a';
	char c2 = 'in';
	char c3 = '1';
	char c4 = 'ス';
	//char can also be represented as an escape character
	char c5 = '\n';
	c5 = '\t';

	System.out.println("hello" + c5 + "world");
	
	//char can also be expressed as a unicode value (see)
	char c6 = '\u3344';
	System.out.println(c6);

	//4. boolean type: boolean
	//boolean type can only be true or false
	//Commonly used for conditional judgment, circular judgment and other operations
	
	boolean isYoung = true;
	//isYoung = 12;
	if(isYoung){
		System.out.println("Sorry, we can't register for marriage. You can talk more about love");
	}else{
		System.out.println("Congratulations, you can go to the adult venue");
	}

}

}

1.2 Categorized by the location of the declaration (Understanding)

2. Define the format of variables:

Format: Data type variable name = variable value

3. Notes on variable use:

(1) Defined variables, when used, are valid only within their scope.
Scope: The pair of {} where variables are defined
(2) In the same scope, variables with the same name cannot be defined.
The value of a variable can only be assigned within the allowable range of its data type.

class VariableTest {
	public static void main(String[] args) {

	//1. Variable declaration: data type variable name
	int myNum;

	//2. Assignment of variables: variable name = variable value
	myNum = 10;

	int myAge = 30;
	//3. Use of variables
	myAge = 20;

	//4. Variables with the same name can no longer be defined
	int myAge = 20;

	System.out.println("My age is:" + myAge);

	//5. The assignment of myAge is beyond the range of int
	//myAge = 2432536653542353463; //Error

	boolean isMale = true;
	//isMale = 10; // Wrong
}

public void test(){
	//System.out.println("My age is:" + myAge);
}

}

4. Operational Rules Between Variables of Basic Data Types

4.1 involves basic data types:

Byte short int long float double char. No boolean type

4.2 Automatic Type Conversion (involving only seven basic data types)

When the variables of small data type and large data type are computed, the result is large data type.
byte,short,char —> int ----> long ----> float ----> double
Note: At this point, the size of the capacity refers to the size of the range of data to be stored, not the size of the number of bytes occupied by the space.

class VariableTest2 {
	public static void main(String[] args) {
		
	byte b1 = 10;
	int i1 = 20;
	int i2 = b1 + i1;

	System.out.println("i2 = " + i2);

	short s1 = 10;
	float f1 = 12.3F;
	double d1 = s1 + f1;
	float f2 = s1 + f1;

	long l1 = 10L;
	float f3 = 12.3F;
	float f4 = l1 + f3;
	System.out.println("f4 = " + f4);

	int i3 = 20;
	double d2 = i3;

	//**************************************
	//In particular: if byte, short and char are operated between them, the result is at least int type.
	byte bb1 = 10;
	short ss1 = 20;
	//FALSE:
	//short ss2 = bb1 + ss1;
	int ii1 = bb1 + ss1;

	byte bb2 = 30;
	//FALSE:
	//byte bb3 = bb1 + bb2;

	char cc1 = 'a';//ascii code: 97'A'ascii code is: 65
	//FALSE:
	//char cc2 = cc1 + bb1;
	int ii2 = cc1 + bb1;
	System.out.println("ii2 = " + ii2);
}

}

4.3 Mandatory type conversion (involving only seven basic data types):

Convert variables of large data type to variables of small data type.
1) Mandatory type converters are required: ()
2) Mandatory type conversion may result in loss of accuracy.

class VariableTest3 {
	public static void main(String[] args) {

int i1 = 10;
//error
//byte b1 = i1;
byte b1 = (byte)i1;
System.out.println(b1);

int i2 = 1221;
//Examples of loss of accuracy:
byte b2 = (byte)i2;
System.out.println(b2);

//Examples of loss of accuracy:
double d1 = 12.3;
long l1 = (long)d1;
System.out.println(l1);

short s1 = 12;
//Correct, compiled through
int i3 = (int)s1;

char c1 = 'A';
int i4 = c1 + 32;
System.out.println("i4 = " + i4);

char c2 = (char)i4;  //(char)(c1 + 32);
System.out.println("c2 = " + c2);
//Usually, we use a pair of''to declare a character. And because a character corresponds to the corresponding ascii code, so we can
//This ascii code value is directly used to assign char variables.
char c3 = 97;
System.out.println("c3 = " + c3);

//Distinguish the following operations
char c4 = 2;
char c5 = '2'; //The ascii code corresponding to'2'is 50
int i5 = (int)c5;
System.out.println(i5);
}

}

4.4 String and Operations between Eight Basic Data Types

1.String is a reference data type
2.String uses a pair of "" to assign values to variables
3. The operation between String and basic data type (8 kinds) variables: only + join operation can be done. And the result of the operation is String type

class StringTest {
	public static void main(String[] args) {
		String s1 = "Hello World!";
		String s2 = "a";
		String s3 = "";
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);

		int num1 = 10;
		String s4 = s1 + num1;
		System.out.println(s4);

		boolean b1 = true;
		char c1 = 'a';

		short ss1 = 123;

		String s5 = s2 + num1 + b1 + c1 + ss1;
		System.out.println(s5);

		//Exercise 1
		char myChar = 'a';
		int myInt = 10;
		String myStr = "hello";
		System.out.println(myChar + myInt + myStr);//107hello
		System.out.println(myChar + (myInt + myStr));//a10hello
		System.out.println(myChar + myStr + myInt);//ahello10

		//Exercise 2
		//*	*	
		System.out.println("*\t*");
		System.out.println('*' + '\t' + '*');
		System.out.println('*' + "\t" + '*');
		System.out.println('*' + '\t' + "*");
		System.out.println("*" + '\t' + '*');

		int num2 = 10;
		long lnum1 = num2;//Automatic type elevation
		//FALSE
		//String myStr1 = num2;

		String myStr1 = num2 + "";//"10"
		//FALSE
		//int num3 = (int)myStr1;
		//Superoutline: Converting String to int using Integer's method
		int num3 = Integer.parseInt(myStr1);
		System.out.println(num3 + 10);
}

}


		System.out.println("*" + '\t' + '*');

		int num2 = 10;
		long lnum1 = num2;//Automatic type elevation
		//FALSE
		//String myStr1 = num2;

		String myStr1 = num2 + "";//"10"
		//FALSE
		//int num3 = (int)myStr1;
		//Superoutline: Converting String to int using Integer's method
		int num3 = Integer.parseInt(myStr1);
		System.out.println(num3 + 10);
}

}

Topics: ascii