Data Types and Variables_Java SE

content

1. Literal constants

2. Data Type

3. Variables

3.1 The concept and syntax of variables

3.2 Integer variables

3.2.1 Integer variable (int)

3.2.2 Long integer variable (long)

3.2.2 Short integer variables (short)

3.2.4 Byte variable (byte)

3.3 Floating point variables

3.3.1 Single-precision floating-point (float)

3.3.2 Double-precision floating-point type (double)

3.4 Character variables (char)

3.5 Boolean Variables

3.6 Type Conversion

3.6.1 Automatic Type Conversion (Implicit Type Conversion)

3.6.2 Coercion (Explicit Type Conversion)

3.6.3 Topic Analysis

3.7 Type promotion

4. String type


 

1. Literal constants

Constants are quantities that are fixed during the running of the program.

Classification of literal constants:

string constant Surrounded by "", such as "12345", "hello", "hello"
integer constant Numbers written directly in the program (note that there is no decimal point), such as: 100, 1000
floating point constant Decimals written directly in the program, such as: 3.14, 0.49
character constant A character enclosed in single quotes, for example: 'A', '1'
boolean constant There are only two kinds of true and false
empty constant null

2. Data Type

Java data types are mainly divided into two categories: basic data types and reference data types

There are eight basic data types in four categories:

Four categories:

Integer, Float, Char, Boolean

Eight kinds:

type of data keywords memory usage scope
character type byte 1 byte -128~127
character type byte 1 byte -128~127
Integer int 4 bytes -2^31~ 2^32-1
long integer long 8 bytes -2^63~ 2^63-1
single precision floating point number float 4 bytes There is scope, generally not concerned
double precision floating point number double 8 bytes There is scope, generally not concerned
character type char 2 bytes 0 ~ 65535(Unicode)
boolean boolean not clearly specified true / false

Reference data type  :

Arrays, Strings, Classes, Interfaces, Enumerations... 

3. Variables

3.1 The concept and syntax of variables

Constants are always the same, and in a program, some content may change frequently. For these frequently changed content, in Java, it is called a variable . Data types are used to define different kinds of variables.

A variable is equivalent to a small box, which can put things in it, and the box can be big or small.

Wrapper class: Each basic data type corresponds to a class type, and this class type is the wrapper class

The syntax format for defining variables is:
data type variable name = initial value;

3.2 Integer variables

3.2.1 Integer variable (int)

1. Size: int is 4 bytes (portable type is strong , whether it is 4 bytes under 32 or 64-bit systems);

Represents the range (-2^31 ~ 2^31-1)

2. Wrapper class: The wrapping type of int is Integer

3. In Java, int does not have so-called unsigned numbers, so int can represent both positive and negative numbers

public class TestDemo {
    public static void main(String[] args) {
        //1.在定义时给出初始值
        int a = 10;
        System.out.println(a);
 
        //2.在定义时没有给初始值,但使用时必须设置初始值
        int b;
        b = 10;
        System.out.println(b);
 
        //3.int 型变量的表示范围
        System.out.println("最大值"+Integer.MAX_VALUE);
        System.out.println("最小值"+Integer.MIN_VALUE);
 
        //4.在定义int 性变量时,所赋值不能超过int的范围
        int d = 1234566702334;//编译时报错,初值超过int的范围
 
    }
}

3.2.2 Long integer variable (long)

1. Size: long occupies 8 bytes regardless of which operating system is used

Represents the range (-2^63 ~ 2^63-1)

2. Add L or l after the initial value of the long integer variable

3. Packaging class: The packaging type of long is Long

 public calss TestDemo{
   public static void main1(String[] args) {
        //1.long 为了区分int和long,一般给long类型变量的初始值之后加上L或l
        long a = 10L;
        System.out.println(a);
        //2.long类型表示的范围
        System.out.println("最大值"+Long.MAX_VALUE);
        System.out.println("最小值"+Long.MIN_VALUE);
    }
}

 

3.2.2 Short integer variables (short)

1. Size: short occupies 2 bytes under any system

Represents the range (-2^15 ~ 2^15-1)

2. There is no unsigned, short can represent positive or negative numbers

3. Wrapper class type: The wrapper class type of short is Short

public class TestDome{
public static void main(String[] args) {
 
    short a = 10;
    System.out.println(a);
 
    //short型表示的范围
    System.out.println("最大值"+Short.MAX_VALUE);
    System.out.println("最小值"+Short.MIN_VALUE);
}
}

3.2.4 Byte variable (byte)

1. Size: byte occupies 1 byte under any system

Range (-2^7 ~ 2^7-1, ie -128~127)

2. Packaging class type: byte packaging class type is Byte

public class TestDome{
public static void main(String[] args) {
    
    byte b = 10;
    System.out.println(b);
    
    //byte型所表示的范围
    System.out.println(Byte.MAX_VALUE);
    System.out.println(Byte.MIN_VALUE);
}
 
}

3.3 Floating point variables

3.3.1 Single-precision floating-point (float)

1. Size: float occupies 4 bytes under any size system

2. Wrapper class type: The wrapper class type of float is Float

3. Add F or f after the initial value of the float variable

public class TestDome{
    public static void main(String[] args) {
        
        float num = 1.0F;
        System.out.println(num);
    }
}

3.3.2 Double-precision floating-point type (double)

1. Size: double occupies 8 bytes under any system

2. Wrapper class type: The wrapper class type of double is Double

3. The double type follows the IEEE754 standard for memory layout. The floating point number is an approximate value, not an exact value.

public static void main4(String[] args) {
    
    double d = 3.14;
    System.out.println(d);
    
}

3.4 Character variables (char)

1. Size: char occupies 2 bytes under any size system , and cannot represent negative numbers

2. ASCII is used to represent characters in C language, while Unicode is used to represent characters in Java

Therefore, there are more types of representation (0~65535), including many languages ​​Chinese, Latin, etc.

3. Packaging class type: The packaging class type of char is Character

public static void main(String[] args) {
    
    char ch = 'a';
    char ch2 = '高';
    char ch3 = '1';
    System.out.println(ch);
    System.out.println(ch2);
    System.out.println(ch3);
}

3.5 Boolean Variables

1. The variable of type Boolean has only two values, true means true or false means false

2. Java's boolean type and int type cannot be converted to each other. There is no 1 for true and 0 for false.

3. Wrapper class type: The wrapper class type of boolean is Boolean

4. In the Java virtual machine, it is not clearly specified how many bytes Boolean occupies

public class TestDome{
public static void main(String[] args) {
    
    boolean value = true;
    System.out.println(value);
}
}

3.6 Type Conversion

3.6.1 Automatic Type Conversion (Implicit Type Conversion)

Automatic type conversion means that the  code does not need to undergo any processing, and the compiler will automatically process it when the code is compiled  . Features:  When the data range is small and the data range is large, it will be automatically carried out  .

That is, small-scale -> large-scale can

            Large-scale -> small-scale No

public class TestDome{
   public static void main(String[] args) {
        System.out.println(1024);//整型情况下默认为int
        System.out.println(3.14);//浮点型情况下默认为double
        
        int a = 100;
        long b = 10L;
        
        b = a;//a是int 小范围,b是long 大范围 ,把小范围赋给大范围,会自动将a提升为大范围也就是long类型
        
        a =b;//这样反过来的话就是 大范围-》小范围 会有数据丢失不安全
        
        float f = 3.14F;
        double d = 5.12;
        
        d = f;//小范围-》大范围,可以,f会自动转化为double
        
        f = d;//大范围-》小范围,不可以,会有数据丢失不安全
        
        byte b1 = 100;//编译通过
        byte b2 = 257;//编译失败,257超过byte的数据范围,有数据丢失
    }
}

3.6.2 Coercion (Explicit Type Conversion)

Mandatory type conversion: When performing an operation, the code needs to be processed in a certain format, which cannot be done automatically. Features:  From a large data range to a small data range.

which is

When casting happens: large range -> small range 

public class TestDome{   
public static void main(String[] args) {
 
        int a = 10;
        long b = 100L;
        b = a;//小范围-》大范围   可以
        a = (int)b;//大范围-》小范围 需要强制转化
 
        float f = 3.14F;
        double d = 5.12;
        d = f;//小范围-》大范围   可以
        f = (float)d;// 大范围-》小范围  需要强制转化
 
        a = d;//报错,类型不兼容
        a = (int)d;//大范围-》小范围 ,需要强转,小数点之后全部丢弃
 
        byte b1 = 100;//100默认为int,隐式转换后,没有超过byte范围
        byte b2 = (byte)257;//257默认为int,超过了byte范围,需要强制转化
        
        boolean flag = true;
        a = flag;//编译失败
        flag = a;//编译失败 类型不兼容
    }
}

 

Notice:

1. Small-scale -> large-scale implicit conversion

2. Large-scale -> small-scale must be forced, otherwise the compilation will fail, and the precision may also be lost

3. When assigning a literal value constant, Java will automatically check the number range

4. Forced conversion is not necessarily successful, and types that do not want to do cannot be converted to each other

3.6.3 Topic Analysis

int i = 10;
float f = 19.9F;
i = f;//会报错吗

 The answer is: an error will be reported, because although i and f are both 4 bytes, the data width that float can represent, in addition to the integer part, also has a fractional part. What if the integer part is given to int and the fractional part? There is a danger of losing data.

3.7 Type promotion

1. When two different data types are used for operation, one of the smaller data types will be converted into a larger one to participate in the operation.

int a = 10;
long b = 20;
 
int c = a + b;//编译失败 a+b->int+long->long+long 赋值给int会丢失数据
long d = a + b;//编译通过 赋值给long就可以

2. For types less than 4 bytes, the operation will be upgraded to int first, and then participate in the operation

byte a = 10;
byte b = 20;
byte c = a + b;
system.out.printfln(c);//编译失败,从a+b-》提升为 int+int-》byte 可能会有损失
 
byte c = (byte)(a + b);
system.out.printfln(c);//编译通过,将int强制转化为byte

 

4. String type

Use String class to define string type in Java,

It's a bit different from C, there is no '\0' ending, there is no such statement at all

public static void main(String[] args) {
    
    String s1 = "hello";
    String s2 = "world";
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(s1+s2);//s1+s2表示:s1和s2进行拼接
}

In some cases, it is necessary to convert between strings and integer numbers:

1. Convert int to String

    int num = 10;
    String str1 = num + "";//法1
    String str2 = String.valueOf(num);//法2

2. Convert String to int

    String str = "100";
    int num = Integer.parseInt(str);

Guess you like

Origin blog.csdn.net/weixin_53939785/article/details/124369089