3. Java Introductory Tutorial [Data Type]

I. Overview

Data types in java are divided into two categories: [basic data types] and [reference data types]

image-20230715145246736

2. Basic data type

type of data meaning Defaults Ranges storage size (bytes)
integer byte Byte 0 -128 to 127 1
integer short short integer 0 -2^15 to 2^15-1 2
integer int [default] plastic surgery 0 -2^31 to 2^31-1 4
integer long long integer 0L -2^63 to 2^63-1 8
floating point float single float 0.0f 1.4e-45 to 3.4e+38 4
floating point double [default] double float 0.0d 4.9e-324 to 1.798e+308 8
character type char character type ‘u0000’ '\u0000' to '\uFFFF' 2
Boolean boolean Boolean false 0(false) to 1(true) 1

It should be noted that in the actual development process, the default type of integer is int, and the default type of floating point is double.

package com.itfeiniu.hello;

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("Hello World!");
        byte b = 100;
        short s = 20;
        int i = 30;
        long l = 452422421123L;//长整型赋值时,值的最后需要添加L/l符号

        float f = 3.14f;//浮点型赋值时,值的最后需要添加F/f符号
        double d = 56.45;

        char ch1 = 'a';
        char ch2 = '中';
        char ch3 = 'A';
        int ch4 = ch3;

        boolean be = true;

        System.out.println(ch4);
    }
}

The default type of any number is inttype. There 452422421123is actually no exceeding longrange below, but it has already exceeded intthe range, so if it is not added at the end l/L, it will be considered as inttype by default, and an error will be reported because it exceeds the range.

long l = 452422421123L;//长整型赋值时,值的最后需要添加L/l符号

All floating-point numbers with a decimal point are doubletypes by default, so if you assign doublethe type of decimal to float, an error will be reported, and you need to add f/La statement that the decimal is a single-precision floattype at the end.

float f = 3.14f;//浮点型赋值时,值的最后需要添加F/f符号

3. Reference type

  • In Java, variables of reference type are very similar to C/C++ pointers. A reference type points to an object, and a variable pointing to an object is a reference variable. These variables are specified as 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")

4. Type conversion

1. Automatic type conversion

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

A variable with a small type range can be directly copied to a variable with a large type range . The range here refers to the value range, not the storage size of the memory.

The specific rules are as follows:

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

It can be seen that longit occupies 8 bytes, but the value range is relatively floatsmall, so longit can still be assigned to floatthe type.

The following is a demonstration that short can be converted to int, but cannot be assigned to byte.

short a = 10;
byte b = a;	//提示报错
int c = a;	//正常

Data type conversion must meet the following rules:

  1. Cannot booleancast to type.

  2. Cannot convert an object type to an object of an unrelated class.

  3. Casting must be used when converting a large-capacity type to a small-capacity type.

  4. may result in overflow or loss of precision during the conversion,

2. Mandatory type conversion

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

  2. Format: (type)value type is the data type instance to be cast:

int i1 = 123;
byte b = (byte) i1;//强制类型转换为byte
byte c = i1;	//报错

3. Automatic type conversion of expressions

In an expression, variables of a small-range type will be automatically converted into a larger-range type in the expression, and then participate in the operation.

byteshortchar->int->long->float->double

Precautions:

  • The final result type of an expression is determined by the highest type in the expression.
  • In the expression, byte, short, charare directly converted into inttypes to participate in the operation.
byte a = 10;
int b = 20;
double c = 30.0;
double d = a + b + c;
System.out.println(d);
//输出:60.0

Guess you like

Origin blog.csdn.net/bobo789456123/article/details/131755605