The third day of learning Java

Java basic data types

Variables apply for memory to store values. In other words, when creating a variable, you need to apply for space in memory.

Therefore, by defining variables of different types, integers, decimals, or characters can be stored in memory.

Built-in data types

The Java language provides eight basic types. Six numeric types (four integers, two floating point types), a character type, and a Boolean type.

byte:

  • The byte data type is an 8-bit, signed, integer represented in two's complement;
  • The byte type is used to save space in large arrays, mainly replacing integers, because the byte variable occupies only one-quarter of the space of the int type;
  • Example: byte a = 100, byte b = -50.

short:

  • The short data type is a 16-bit, signed two's complement integer
  • The Short data type can also save space like byte. A short variable is half the space occupied by an int type variable;
  • Example: short s = 1000, short r = -20000

int:

  • The int data type is a 32-bit, signed integer represented in two's complement;
  • Generally, integer variables default to int type;
  • Example: int a = 100000, int b = -200000.

long:

  • The long data type is a 64-bit, signed two's complement integer;
  • This type is mainly used on systems that need to compare large integers;
  • Example: long a = 100000L, long b = -200000L.
    "L" is theoretically not case-sensitive, but if it is written as "l", it will be easily confused with the number "1" and difficult to distinguish. So it’s best to capitalize.

float:

  • The float data type is a single-precision, 32-bit, floating-point number that complies with the IEEE 754 standard;
  • Loat can save memory space when storing large floating point arrays;
  • The default value is 0.0f;
  • Floating point numbers cannot be used to represent precise values ​​such as currency;
  • Example: float f1 = 234.5f.

double:

  • The double data type is a double-precision, 64-bit, IEEE 754-compliant floating-point number;
  • The default type of floating point numbers is double;
  • The double type also cannot represent precise values, such as currency;
  • The default value is 0.0d;

boolean:

  • The boolean data type represents one bit of information;
  • There are only two values: true and false;
  • This type only serves as a flag to record true/false conditions;
  • The default value is false;
  • Example: boolean one = true.

char:

  • The char type is a single 16-bit Unicode character;
  • The minimum value is \u0000 (decimal equivalent is 0);
  • The maximum value is \uffff (that is, 65535);
  • The char data type can store any character;
  • Example: char letter = 'A';.

For the value range of the basic types of numeric types, we do not need to be forced to remember, because their values ​​​​have already been defined in the corresponding packaging class in the form of constants.

Example:

public class PrimitiveTypeTest {  
    public static void main(String[] args) {  
        // byte  
        System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE);  
        System.out.println("包装类:java.lang.Byte");  
        System.out.println("最小值:Byte.MIN_VALUE=" + Byte.MIN_VALUE);  
        System.out.println("最大值:Byte.MAX_VALUE=" + Byte.MAX_VALUE);  
        System.out.println();  
  
        // short  
        System.out.println("基本类型:short 二进制位数:" + Short.SIZE);  
        System.out.println("包装类:java.lang.Short");  
        System.out.println("最小值:Short.MIN_VALUE=" + Short.MIN_VALUE);  
        System.out.println("最大值:Short.MAX_VALUE=" + Short.MAX_VALUE);  
        System.out.println();  
  
        // int  
        System.out.println("基本类型:int 二进制位数:" + Integer.SIZE);  
        System.out.println("包装类:java.lang.Integer");  
        System.out.println("最小值:Integer.MIN_VALUE=" + Integer.MIN_VALUE);  
        System.out.println("最大值:Integer.MAX_VALUE=" + Integer.MAX_VALUE);  
        System.out.println();  
  
        // long  
        System.out.println("基本类型:long 二进制位数:" + Long.SIZE);  
        System.out.println("包装类:java.lang.Long");  
        System.out.println("最小值:Long.MIN_VALUE=" + Long.MIN_VALUE);  
        System.out.println("最大值:Long.MAX_VALUE=" + Long.MAX_VALUE);  
        System.out.println();  
  
        // float  
        System.out.println("基本类型:float 二进制位数:" + Float.SIZE);  
        System.out.println("包装类:java.lang.Float");  
        System.out.println("最小值:Float.MIN_VALUE=" + Float.MIN_VALUE);  
        System.out.println("最大值:Float.MAX_VALUE=" + Float.MAX_VALUE);  
        System.out.println();  
  
        // double  
        System.out.println("基本类型:double 二进制位数:" + Double.SIZE);  
        System.out.println("包装类:java.lang.Double");  
        System.out.println("最小值:Double.MIN_VALUE=" + Double.MIN_VALUE);  
        System.out.println("最大值:Double.MAX_VALUE=" + Double.MAX_VALUE);  
        System.out.println();  
  
        // char  
        System.out.println("基本类型:char 二进制位数:" + Character.SIZE);  
        System.out.println("包装类:java.lang.Character");  
        // 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台  
        System.out.println("最小值:Character.MIN_VALUE="  
                + (int) Character.MIN_VALUE);  
        // 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台  
        System.out.println("最大值:Character.MAX_VALUE="  
                + (int) Character.MAX_VALUE);  
    }  
}

 Print results

基本类型:byte 二进制位数:8
包装类:java.lang.Byte
最小值:Byte.MIN_VALUE=-128
最大值:Byte.MAX_VALUE=127

基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768
最大值:Short.MAX_VALUE=32767

基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE=-2147483648
最大值:Integer.MAX_VALUE=2147483647

基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808
最大值:Long.MAX_VALUE=9223372036854775807

基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45
最大值:Float.MAX_VALUE=3.4028235E38

基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324
最大值:Double.MAX_VALUE=1.7976931348623157E308

基本类型:char 二进制位数:16
包装类:java.lang.Character
最小值:Character.MIN_VALUE=0
最大值:Character.MAX_VALUE=65535

Reference data type

  • In Java, reference type variables are very similar to pointers in C/C++. A reference type points to an object, and a variable pointing to an object is a reference variable. These variables are assigned a specific type when declared, such as Employee, Puppy, etc. Once a variable is declared, its type cannot be changed.
  • Objects and arrays are reference data types.
  • The default value for all reference types is null.
  • A reference variable can be used to refer to any compatible type.
  • Example: Site site = new Site("Runoob").

Java constants

Use the final keyword in Java to modify constants, and declare them in a similar way to variables:

final double PI = 3.1415927;

Although constant names can also be lowercase, for ease of identification, uppercase letters are usually used to represent constants.

The Java language supports some special escape character sequences.

automatic type conversion

Integer, real (constant), and character data can be mixed for operation. During the operation, different types of data are first converted into the same type, and then the operation is performed

Convert from low level to high level

低  ------------------------------------>  高

byte,short,char—> int —> long—> float —> double 

Data type conversion must meet the following rules:

  • 1. Type conversion cannot be performed on boolean type.

  • 2. The object type cannot be converted into an object of an unrelated class.

  • 3. Forced type conversion must be used when converting a large-capacity type into a small-capacity type.

  • 4. The conversion process may cause overflow or loss of accuracy, for example:

int i =128;   
byte b = (byte)i;

Because the byte type is 8 bits and the maximum value is 127, when int is cast to the byte type, the value 128 will cause overflow.

Floating point to integer conversion is obtained by discarding decimals, rather than rounding

automatic type conversion

The number of digits of the data type before conversion must be lower than that of the data type after conversion. For example: if the number of digits of the short data type is 16, it can be automatically converted to the int type with 32 digits. Similarly, the number of digits of the float data type is 32, it can be automatically converted to a 64-bit double type.

cast

  • 1. The condition is that the converted data types must be compatible.

  • 2. Format: (type)value type is the data type to be forced to type conversion Example:

public class QiangZhiZhuanHuan{
    public static void main(String[] args){
        int i1 = 123;
        byte b = (byte)i1;//强制类型转换为byte
        System.out.println("int强制类型转换为byte后的值等于"+b);
    }
}

result: 

The value after int is cast to byte is equal to 123

Implicit cast

  • 1. The default type of integer is int.

  • 2. Decimals are double type floating point by default. When defining the float type, F or f must be followed by the number.

Guess you like

Origin blog.csdn.net/Vince_13/article/details/134447889