2.java data types and variables

2.1 Literal constants

System.out.println("Hello java");//打印的Hello java就是一个常量

Constants are constants that are fixed during the running of the program;
constants are divided into:

  1. String constant: enclosed by "", such as "123456", "abcd".
  2. Character constants: enclosed by '', such as '1', 'q'.
  3. Integer constants: that is, integers, 1, 2, 3.
  4. Floating-point constants: numbers with decimal points such as 1.1, 2.2, 6.6, etc.
  5. Boolean constants: only false and true
  6. Empty constant: null
System.out.println("ABCDEFG");
        System.out.println('A');
        System.out.println(666);
        System.out.println(true);
        System.out.println(false);
        System.out.println(12.26);

2.2 Data Types

Data types in java are divided into basic data types and reference data types.
The basic data types are divided into four categories and eight categories.

The four categories are: Integer, Float, Boolean, and Character.
The eight categories are:
insert image description here

Notice

  1. Because java runs on the JVM, the int type is four bytes, and the long type is 8 bytes, regardless of the machine model.
  2. Integers and floating-point types are signed.
  3. Integer defaults to int, and floating point defaults to double.
  4. Strings belong to the reference type, which will be introduced later.

2.3 Variables

2.3.1 Overview of variables

A variable is a quantity that needs to be changed frequently. For example, our test scores change every time, and the number type is used to define the type of our variable.

2.3.2 Syntax format

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

For example:

  int a=0;
        double b=0;
        char c='a';
        a=999;          //a是变量值可以修改
        int q=0,w=0,e=0;//也可以一行定义多个变量

2.3.3 Integer variables

2.3.3.1 Integer variables

 int a;
        a=66;//先定义后赋值
        System.out.println(a);
        int b=99;//定义时给出初始值
        System.out.println(b);
        System.out.println(Integer.MAX_VALUE);   //整形所能接收的最大值,大于会报错
        System.out.println(Integer.MIN_VALUE);    //整形所能接收的最小值,小于会报错

insert image description here

Notice
1. It is recommended to assign the initial value after defining the variable. If you do not know what value to assign, you can assign 0.
2. Variables must be assigned an initial value before use, otherwise an error will be reported when compiling.
3. The packaging type of int is Integer (will learn later).

2.3.3.2 Long variable

  long a;       //先定义后赋值
        a=66l;        //长整型赋初始值后后面加l或者L
        long b=99l;   //定义时给出初始值
        System.out.println(a);
        System.out.println(b);
        System.out.println(Long.MAX_VALUE);         //长整形所能接收的最大值,大于会报错
        System.out.println(Long.MIN_VALUE);         //长整形所能接收的最小值,小于会报错

insert image description here

Notice

  1. The wrapper type of long is Long
  2. Add l or L after assigning the initial value of the long integer, and L is recommended.

2.3.3.3 Short variable

     short a;
          a=66;
          short b=66;
        System.out.println(Short.MAX_VALUE);
        System.out.println(Short.MIN_VALUE);

insert image description here

Notice
1. When assigning a value to a short type variable, note that its storable range is (-32768——32767)
2. The packaging type of short is Short

2.3.3.4 Byte variables

short a=6System.out.println(Byte.MAX_VALUE);    //最大范围
        System.out.println(Byte.MIN_VALUE);    //最小范围

insert image description here

Notice

  1. The wrapper class of byte is Byte

2.3.4 Floating point variables

2.3.4.1 Double floating point type

  double a=6.6;
        System.out.println(a);
        System.out.println(a/1.1);
        System.out.println(Double.MAX_VALUE);
        System.out.println(Double.MIN_VALUE);       

insert image description here

Notice

  1. Possible value range of double floating-point variables
  2. The reference type of double is Double
  3. double occupies 8 bytes under any system
  4. Floating-point numbers are stored differently from integers in memory, and cannot be calculated simply by using

2.3.4.2 Single floating point type

float a=6.6f;
        System.out.println(a);
        System.out.println(a/1.1);
        System.out.println(Float.MAX_VALUE);
        System.out.println(Float.MIN_VALUE);

insert image description here
*Obviously the value range of single floating-point numbers is smaller than the range of double floating-point numbers

Notice

  1. Also define the initial value of the float type data and add f or F after the value
  2. The reference type of float is Float

2.3.5 Character variables

char a='A';   //字符可以为字母
        char b='6';   //字符可以为数字
        char c='爱'   //字符可以是汉字
        System.out.println(Character.MAX_RADIX);
        System.out.println(Character.MIN_RADIX);

insert image description here

Notice

  1. Java uses the form of single quotes + a single letter to represent character literals.
  2. The wrapper type of char is Character
  3. A character in a computer is essentially an integer. ASCII is used to represent characters in C language, and Unicode is used to represent characters in Java. Therefore, a character occupies two bytes, and there are more types of characters represented, including Chinese
 char a='A';
        char b='a';
        System.out.println(a+1);    //打印对应Unicode码值
        System.out.println(b+1);

insert image description here

2.3.6 Boolean variables

       boolean a=true;
        System.out.println(a);
        boolean b=false;
        System.out.println(b);

insert image description here

Notice

  1. A variable of boolean type has only two values, true means true, and false means false.
  2. Java's boolean type and int cannot be converted to each other. There is no such usage as 1 means true and 0 means false.
  3. The wrapper type for boolean is Boolean.

2.3.7 Type Conversion

2.3.7.1 Automatic data type conversion

Automatic type conversion means: the code does not need to undergo any processing, and the compiler will automatically process it when the code is compiled. Features: It will be automatically converted from a small data range to a large data range.

System.Out.println(1024); // 整型默认情况下是int
System.Out.println(3.14); // 浮点型默认情况下是double
int a = 100;
long b = 10L;
b = a; // a和b都是整形,a的范围小,b的范围大,当将a赋值给b时,编译器会自动将a提升为long类型,然后赋值
a = b; // 编译报错,long的范围比int范围大,会有数据丢失,不安全
float f = 3.14F;
double d = 5.12;
d = f; // 编译器会将f转换为double,然后进行赋值
f = d; // double表示数据范围大,直接将float交给double会有数据丢失,不安全
byte b1 = 100; // 编译通过,100没有超过byte的范围,编译器隐式将100转换为byte
byte b2 = 257; // 编译失败,257超过了byte的数据范围,有数据丢失

2.3.7.2 Mandatory data type conversion

Mandatory type conversion: When performing operations, the code needs to undergo certain format processing, which cannot be completed automatically. Features: From large data range to small data range.

int a = 10;
long b = 100L;
b = a; // int-->long,数据范围由小到大,隐式转换
a = (int)b; // long-->int, 数据范围由大到小,需要强转,否则编译失败
float f = 3.14F;
double d = 5.12;
d = f; // float-->double,数据范围由小到大,隐式转换
f = (float)d; // double-->float, 数据范围由大到小,需要强转,否则编译失败
a = d; // 报错,类型不兼容
a = (int)d; // int没有double表示的数据范围大,需要强转,小数点之后全部丢弃
byte b1 = 100; // 100默认为int,没有超过byte范围,隐式转换
byte b2 = (byte)257; // 257默认为int,超过byte范围,需要显示转换,否则报错
boolean flag = true;
a = flag; // 编译失败:类型不兼容
flag = a; // 编译失败:类型不兼容

Notice

  1. Mandatory type conversion may not be successful, irrelevant types cannot be converted to each other

2.4 String type

String s1 = "hello";
String s2 = " world";
System.out.println(s1);
System.out.println(s2);
System.out.println(s1+s2); // s1+s2表示:将s1和s2进行拼接

insert image description here

  1. Convert int type to String
int a=6;
         String str1=a+" ";   //方法1
         String str2=String.valueOf(a);   //方法2,记住就好O(∩_∩)O哈哈~
        System.out.println(str1);
        System.out.println(str2);

insert image description here

  1. Convert String to int
  String str ="666";
        int a=Integer.parseInt(str);
        System.out.printf("%d",a);    //这也是一种输出方式

insert image description here
Continue to learn slowly later

Guess you like

Origin blog.csdn.net/st200112266/article/details/127641554