Chapter 2 fundamentals of Java language

Posted by dvayne on Sat, 05 Mar 2022 00:31:35 +0100

catalogue

2.2 variables and constants

Example 2.1 declare a double constant, assign a value to the constant, and use the constant for calculation.

2.3 basic data type

Example 2.2 assign values to int variables according to decimal, octal and hexadecimal

Example 2.3 shows the error result of 4.35 * 100 and gives the solution.

Example 2.4 calculation results of 2.0-1.9

2.3 character type

Example 2.5 using escape characters

Example 2.6 declaring boolean variables

2.4 data type conversion

Example 2.7 create variables of different numerical types for implicit conversion.

Example 2.8 create different numeric types for implicit conversion.

2.5 operators

Example 2.9 use the assignment operator to assign values to variables. The example code is as follows

Example 2.10 using arithmetic operators to output the calculation results of variables

Example 2.11 use the auto increment operator in the loop to see the effect of auto increment

Example 2.12 compare variables using relational operators.

Example 2.13 use logical operators and relational operators to operate variables.

Example 2.14 operation using bitwise logical operators

Example 2.15 displacement operation of variable using displacement operator

Example 2.16 let byte and short variables do unsigned right shift operation

2.2 variables and constants

Java keyword

Example 2.1 declare a double constant, assign a value to the constant, and use the constant for calculation.

public class Const {
	public static void main(String[] args) {
		final double PI =3.14; //Declare constant PI	
		System.out.println("constant PI The value of is:"+PI);
		System.out.println("The circumference of a circle with radius 3 is:"+(PI*2*3));
		System.out.println("The area of a circle with a radius of 4 is:"+(PI*4*4));
	}

}

2.3 basic data type

Example 2.2 assign values to int variables according to decimal, octal and hexadecimal

public class Radix {
	public static void main(String[] args) {
	int a=11;            //Decimal integer
	System.out.println(a);//Outputs an integer value in decimal
	int b=011;           //Octal integer
	System.out.println(b);//Output integer value in octal
	int c=0x11;           //Hexadecimal integer
	System.out.println(c);//Outputs an integer value in hexadecimal
 
	}

}

Example 2.3 shows the error result of 4.35 * 100 and gives the solution.

public class Doudle {
	public static void main(String[] args) {
		double a=4.35*100; //Calculate the result of 4.35 * 100 with double
		System.out.println("a="+ a);//Output this value as double
		int b= (int) a;//Cast double type to int type
		System.out.println("b="+b);//Output int value
		System.out.println("a Rounding value of="+Math.round(a));

	}

}

Example 2.4 calculation results of 2.0-1.9

public class DoubleUNAcc2 {
	 public static void main(String[] args){
		 double a=0.1;
		 double b=2.0-1.9;
	   System.out.println("a="+a);
	   System.out.println("b="+b);
	   System.out.println("a==b Results:"+(a==b));//The result of a==b is false
/*Math.abs()Is the method of taking absolute value. 1e-6 represents the - 6th power of 1 + 10. It is the concept of the smallest decimal in the computer
*If the absolute value of the difference between two numbers is less than the minimum number, the two numbers are considered equal.
*/
	   boolean bool =Math.abs(a-b)<(1e-6);
	   System.out.println("The result that the absolute value of the difference between two numbers is less than the lowest number:"+bool);
   }
}

 

2.3 character type

Escape character

Example 2.5 using escape characters

public class EscapeCharacter {
	public static void main(String[] args) {
		char c1='\\';//Backslash escape character
		char c2='\'';//Single quote escape character
		char c3='\"';//Double quote escape character
		char c4='\u2605';//051 hexadecimal characters
		char c5='\101';//Octal representation character
		char c6='\n';//Tab escape character
		char c7='\t';//Line break escape character
		 System.out.println("[" + c1 + "]");
		 System.out.println("[" + c2 + "]");
		 System.out.println("[" + c3 + "]");
		 System.out.println("[" + c4 + "]");
		 System.out.println("[" + c5 + "]");
		 System.out.println("[" + c6 + "]");
		 System.out.println("[" + c7 + "]");
	}
}

Boolean type

Example 2.6 declaring boolean variables

public class Boolean {
	public static void main(String[] args) {
     boolean b;//Declare Boolean variables
     boolean b1,b2;//Declare boolean variable B1 b2
     boolean ture = false;Declare Boolean variables b1 Assign to initial value ture,b2 Assign to initial value false,
     boolean b3=ture, b4=false;
     boolean b5= 2<3, b6=(2==4);
     System.out.println("b5 The result is:"+b5);
     System.out.println("b6 The result is:"+b6);
   }
}

2.4 data type conversion

Example 2.7 create variables of different numerical types for implicit conversion.

public class ImplicitConver {
	public static void main(String[] args) {
		byte mytybe =127;
		int myint =150;//Declare the int variable myint and assign a value of 150
		float myfloat =452.12f;//Declare the float type variable myfloat and assign a value
		char mychar =10; //Declare the char type variable mychar and assign a value
		double mydouble =45.46546;//Declare a double variable and assign a value
		float mybyte=127;
		System.out.println("byte Type and float Type data, and the result is:"+  (mybyte+myfloat));
		System.out.println("byte Type and int Type data, and the result is:"+  (mybyte*myint));
		System.out.println("byte Type and char Type data, and the result is:"+  (mybyte / mychar));
		System.out.println("double Type and char Type data, and the result is:"+  (mydouble+mychar));
		
	}

}

Example 2.8 create different numeric types for implicit conversion.

public class EqualSign {
	    public static void main(String[] args) {
	    	int a = (int)45.23;// double type forced to int type
	    	long b = (long)456.6F;//float type strong long type
	    	char c = (char) 97.14;// double forced char        		
	    	System.out.println("45.23 Cast to int Results:"+a);
	    	System.out.println("456.6F Cast to 1 ong Results:"+b)	;    
	    	System.out.println("97.14 Cast to char Results"+c);
	        }
}	    

2.5 operators

 

Example 2.9 use the assignment operator to assign values to variables. The example code is as follows

public class EqualSign1 {
	 public static void main(String[] args) {
        int a,b,c=11;
           a=32;
           c=b=a+4;
           System.out.println("a="+a);
           System.out.println("b="+b);
           System.out.println("c="+c);
 }
}

 

Example 2.10 using arithmetic operators to output the calculation results of variables

public class Arit {
	public static void main(String[] args) {
		float num1 = 45.2f;
		int num2 = 120;
		int num3 = 17, num4=10;
		System.out.println("num1+num2 The sum of is:"+(num1+ num2));
		System.out.println("num1-num2 The difference is:"+(num1- num2));
		System.out.println("num1*num2 The product of is:"+(num1*num2));
		System.out.println("num1/num2 The quotient is:"+ (num1 / num2));
		System.out.println("num3/num4 The remainder of is:"+(num3 % num4));
	}

}

Example 2.11 use the auto increment operator in the loop to see the effect of auto increment

public class Auto {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a =1; //Create an integer variable a with an initial value of 1
		System.out.println("a="+ a);// Output the value of a at this time
		a++;                        // a self increasing + 1
		System.out.println("a++="+ a);//Output the value of a at this time
		a++;                       // a self increasing + 1
		System.out.println("a++="+ a);//Output the value of a at this time
		a++;                         // a self increasing + 1
		System.out.println("a++ ="+ a);//Output the value of a at this time
		a--;                           // a self subtraction-1
		System.out.println("a--="+ a);//Output the value of a at this time
	}

}

Relational operator

Example 2.12 compare variables using relational operators.

public class Relat {
	public static void main(String[] args) {
		int num1 = 4, num2 =7;
		int num3 = 7;
		System.out.println("num1<num2 Results:"+(num1 < num2));
		System.out.println("num1>num2 Results:"+(num1> num2));
		System.out.println("num1==num2 Results:"+(num1 == num2));
		System.out.println("numl!=num2 Results:"+(num1=num2));
		System.out.println("num1<=num2 Results:"+(num1<= num2));
		System.out.println("num2>=num3 Results:"+(num2 >= num3));	}

}

Example 2.13 use logical operators and relational operators to operate variables.

public class Logical {
	public static void main(String[] args) {
		int a=2;statement int Type variable a
		int b= 5; // Declare int variable b
		//Declare a boolean variable to save the return value after applying the logical operator "& &"
		boolean result=((a>b) &&(a!=b));
	//Declare a boolean variable, which is used to save the return value after applying the logical operator "|"	
		boolean result2=((a>b) ||(a!=b));
		System.out.println(result); Will variable result output
		System.out.println(result2); Will variable result2 output
	}

}

Example 2.14 operation using bitwise logical operators

public class Logical1 {
	public static void main(String[] args) {
		short x=-123;								//Create a short variable x, equal to the negative value of 123
			System.out.println("12 The result of and 8 is:"+(12&8));			//Bit logic or the result of calculating an integer
			System.out.println("4 Or the result of 8:"+(4|8));			//Bit logic or the result of calculating an integer
			System.out.println("31 XOR 22:"+(31^22));			//The result of a bitwise logical XOR calculation of an integer
			System.out.println("123 The negative result is:"+x);		//Bit logic negates the result of Boolean calculation
			System.out.println("2>3 And 4!=7 And results:"+(2>3&4!=7));//Bit logic and the result of calculating Boolean values
			System.out.println("2>3 And 4!=7 Or result:"+(2>3|4!=7));//Bit logic or the result of calculating Boolean values
			System.out.println("2>3 And 4!=7 And XOR result:"+(2<3^4!=7));	
                                                   //The result of bit logical XOR calculation of Boolean value
	}

}

Example 2.15 displacement operation of variable using displacement operator

public class Bitwise2 {
	public static void main(String[] args) {
		int a=24;
		System.out.println(a+"The result of moving two bits to the right is:"+(a>>2));
		int b=-16;
		System.out.println(b+"The result of moving two bits to the right is:"+(b<<3));
		int c=-256;
		System.out.println(c+"The result of a two digit unsigned right shift is:"+(c>>>2));
	}

}

Example 2.16 let byte and short variables do unsigned right shift operation

public class Bitwise3 {
	public static void main(String[] args) {
		byte a=(byte) (-32>>>1);
		System.out.println("byte Result of unsigned right shift:"+a);
		short b=(short) (-123>>>4);
		System.out.println("short Result of unsigned right shift:"+b);

	}

}

Topics: Java Eclipse