I. Overview
Data types in java are divided into two categories: [basic data types] and [reference data types]
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 int
type. There 452422421123
is actually no exceeding long
range below, but it has already exceeded int
the range, so if it is not added at the end l/L
, it will be considered as int
type 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 double
types by default, so if you assign double
the type of decimal to float
, an error will be reported, and you need to add f/L
a statement that the decimal is a single-precision float
type 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 long
it occupies 8 bytes, but the value range is relatively float
small, so long
it can still be assigned to float
the 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:
-
Cannot
boolean
cast to type. -
Cannot convert an object type to an object of an unrelated class.
-
Casting must be used when converting a large-capacity type to a small-capacity type.
-
may result in overflow or loss of precision during the conversion,
2. Mandatory type conversion
-
The condition is that the converted data types must be compatible.
-
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.
byte、short、char->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
,char
are directly converted intoint
types 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