Java SE (16) Variable 2

 

table of Contents

 Variable data type

Integer variable

Floating point type variable

Character type variable

Boolean variable

Variable type conversion

         Automatic type conversion

Forced type conversion


 Variable data type

    Java is a strongly typed language, and every variable must declare its data type. 
    Java's data types can be divided into two categories: primitive data types and reference data types.

    Integer variable

1. Function: Integer type variable is used to store integer value , that is, value without decimal part.

2. Classification

    

   In Java, in order to reasonably allocate storage space for integers in different sizes, integer types are divided into 4 different types.

  • The integer type is int type by default .
  • When assigning a value to a variable of type long , a letter L (or lowercase l) should be added after the assigned value , indicating that the assignment is of type lonh.

       If the assigned value does not exceed the value range of the int type , you can omit the letter L (or lowercase l).

       Such as: long num=2200000000 L;      //The assigned value exceeds the value range of the int type, and the letter L must be added after it

              long num=189L; //The assigned value does not exceed the value range of the int type, and the letter L can be added after it

              long num=189; //The assigned value exceeds the value range of the int type, and the letter L can be omitted later

 Example 1: How to use different number system (base) types

public class TestVar1{
	public static void main(String[] args){
		//定义整数类型的变量
		//给变量赋值时,值可以为不同进制的
		int num1=12;    //默认情况下赋值就是十进制数
		System.out.println("num1"+"="+num1);
		
		int num2=0b10;  //前面加上0b或0B,这个值就是二进制数
		System.out.println("num2"+"="+num2);
		
		int num3=012;   //前面加上0,这个值就是八进制数
		System.out.println("num3"+"="+num3);
		
		int num4=0x12;  //前面加上0x或0X,这个值就是十六进制数
		System.out.println("num4"+"="+num4);
	}
}

Operation result: the output is all decimal type

Example 2: How to use different integer types

public class TestVar2{
	public static void main(String[] args){
		//定义byte类型的变量
		byte b=12;    //定义了一个byte类型的变量,名字叫b,赋值为12
		System.out.println("b"+"="+b);
		
		short s=30000;  //定义了一个short类型的变量,名字叫s,赋值为30000
		System.out.println("s"+"="+s);
		
		int i=1234;   //定义了一个int类型的变量,名字叫i,赋值为1234
		System.out.println("i"+"="+i);
		
		long l=12;  //定义了一个long类型的变量,名字叫l,赋值为12
		System.out.println("l"+"="+l);
		
		//整数类型默认就是int类型的,所以12345678910是一个int类型的数,对于int类型来说,它超出范围了
        //要想把一个数给long类型变量,那么后面加上L(推荐)或者l就可以了
        long L = 12345678910L;
        System.out.println("L"+"="+L);
	}
}

  operation result:

  

    Floating point type variable

1. Function: Variables of floating-point number type are used to store decimal values .

2. Classification:

    In Java, there are two types of floating-point numbers: single-precision floating-point (float) and multi-precision floating-point (double).

PS: Significant numbers refer to the first non-zero number from the left to the last number

  •  In java, a decimal will be defaulted to double .

    Therefore, when assigning a value to a float type variable, you need to add the letter F (or lowercase f) after the assigned value ;

     When assigning a variable of double type , you can add the letter D (or lowercase d) after the assigned value, or not .

         Such as: float f=123.4F; //Assign a value to a float type variable, the letter F (or lowercase f) must be added after it

                double=110.1D; //Assign a value to a variable of type double, you can add the letter D (or lowercase d) after it

                double=112.5; //Assign a value to a variable of type double, without the letter D (or lowercase d)

  • You can also assign an integer value to a floating-point number variable in the program, for example, the following writing is also possible. 

                float f=100; //Declare a variable of type float and assign an integer value

                double=100; //Declare a variable of type double and assign an integer value

Example 1: The use of two representations of floating-point numbers

public class TestVar3{
	public static void main(String[] args){
		//浮点数类型常量的两种形式
		//十进制小数法
		double num1=2.14;  
		System.out.println("num1"+"="+num1);
		//科学计数法
		double num2=214E-2;
		System.out.println("num2"+"="+num2);
	}
}

operation result:

Example 2: Two types of declaration and assignment of floating-point numbers

public class TestVar4{
	public static void main(String[] args){
			/*浮点数类型的变量
			注意:浮点数类型默认是double类型,要想将一个double类型的数赋给
			float类型后面必须加上F(或小写f)
			*/
			//声明并赋值一个float类型变量,后面必须加上F(或小写f)
			float f1=3.1415926784F;
			System.out.println("f1"+"="+f1);
			//声明并赋值一个double类型变量,后面的D或者d一般省略不写
			double d1=3.1415926784;
			System.out.println("d1"+"="+d1);
			
			//注意:最好不要进行不同浮点数类型的比较
			float f2=0.3F;
			double d2=0.3D;
			System.out.println(f2==d2);
	}
}

operation result:

    Character type variable

        Character type variables are used to store a single character , represented by char in Java , and each character variable of type char occupies 2 bytes .

  • The char type is used to represent the characters in the Unicode code table . (Unicode is compatible with ASCII code, the first 128 positions of Unicode are ASCII)

  • When assigning a value to a variable of type char, you need to enclose the character with a pair of single quotation marks ('') in English half-width format .

  •  You can also assign char type variables to integers in the range of 0~65535 ( without single quotes ) , and the computer will automatically convert these numbers into the corresponding characters . For example, the character corresponding to the value 97 is'a'.
  • When calculating the underlying char type, it is actually calculated according to ASCII

    Boolean variable

       The boolean type has two constant values, true and false , which occupy one bit in memory (not a byte). You cannot use 0 or non-zero integers to replace true and false. This is different from the C language. The boolean type is used to determine logical conditions, and is generally used for program flow control.


Variable type conversion

    In the program, when assigning a value of one data type to a variable of another data type, data type conversion is required. According to the type conversion method can be divided into two types: automatic type conversion and forced type conversion .

The value range of different variables is sorted: double>float>long> int>short>char>byte

Compatibility: char cannot be assigned to short and byte.

              Boolean has no compatibility with other data types.

    Automatic type conversion

       Automatic type conversion is also called implicit type conversion , which refers to the declaration of two data types that do not need to be displayed during the conversion process .

       To achieve automatic type conversion must be simultaneously filled foot two conditions: first, two types of data are compatible with each other , the second type of target is in the range of greater than source type range.

  • Format: source type source type variable name = value 1;

                  Target type target type variable = source type variable name;

For example : byte b=6;

           int i=b; //The program converts the variable b of type byte to type int, without special declaration

    Forced type conversion

    Forced type conversion is also called explicit type conversion , which refers to the declaration of two data types that need to be displayed during the conversion process .

    The requirement for forced conversion: the two data types are incompatible with each other , or the value range of the target type is smaller than the source type.

  • Declaration format: target type variable name = (target type) value;

For example: byte b=(byte) value;

  • Note: During the forced type conversion, when the value of the source type is greater than the value range of the target type, the data will be accurately lost.

     The value range of byte is -128~127

supplement

1.  Automatic promotion of expression type

        The so-called expression refers to an expression composed of variables and operators. Variable expressions may also undergo automatic type conversion during operations . This is the automatic promotion of expression data types.

For example, a byte type variable will be automatically promoted to int type during operation.

Reason : During the operation of expression b1+b2, variables b1 and b2 are automatically promoted to int type , and the result of the expression becomes int type. At this time, if the result is assigned to a byte type variable, an error will be reported and need to be performed Force type conversion.

Solution : Change the fifth line of code to byte b3= (byte) b1+b2;

2. When there are multiple data types in the same expression, the calculation is performed according to the highest type in the current expression.

 

Guess you like

Origin blog.csdn.net/wqh101121/article/details/111468659