First, the meaning of data types

The data type is a classification of variables that is to say, how much memory should we allocate to the variable when the program is running? How much memory does each piece of data require? Can we all allocate 4 bytes? So there is the existence of data types, in order to save the use of memory space more reasonably, each amount we are allocated according to its type, the better use of memory space.

Data types in Java

Java is a strongly typed language. According to? Because one type is declared for each variable, and the number of bytes for all numeric types is platform-independent.

Data types in Java are divided into eight basic data types and reference types.

Basic data types

1. The integer

Integers are used to represent integers. (both positive and negative integers)

type Storage size Value range A wrapper class
int 4 bytes -2147483648 ~ 2147483647 (about 2.1 billion) java.lang.Integer
short 2 – – 32768 ~ 32767 java.lang.Short
long 8 bytes – 9223372036854775808 ~ 9223372036854775807 java.lang.Long
byte 1 byte – 128 ~ 127 java.lang.Byte

** In Java, the range of integers is independent of the machine on which the Java code is running. (2) Base representation

Into the system The prefix
binary   0b
octal   0
hexadecimal   0x

ToUnsignedInt (b) = 0; toUnsignedInt(b) = 0; toUnsignedInt(b) = 0;

2. Floating point

Floating point is used to represent values that have a fractional part.

type Storage size Value range A wrapper class
float 4 bytes The number of significant digits is 6 to 7 java.lang.Float
double 8 bytes The significant number is 15 bits java.lang.Double

Float values have a suffix f or f, and default to double. Float values are not suitable for financial calculations that cannot accept rounding errors. Floating-point numbers are binary and cannot accurately represent all fractions, so calculations are subject to error. If you must have no rounding error, you can use BigDecimal.

3. Character

① The char type is used to represent a single character. Literal values of the char type are enclosed in single quotes. ② It takes up 2 bytes in memory, and the Unicode encoding mechanism must be used to convert characters into binary storage. ③ Its wrapper class is java.lang.Character

Escape of special characters

Escape sequence of common escape characters Escape sequence name Unicode \b backspace \u0008 \t tabbed \u0009 \n Newline \u000a \r carriage return \u000d \” Double quotation marks \u0022 \’ Single quotation marks \u0027 \ backslash \u005c

4. Boolean type

Boolean types have two values: false and true, which are used for logical judgments. Integers and Booleans cannot be converted to each other.

Variables and constants

1. The variable

Variable name requirements: ⅰ Variable name see name think meaning ⅱ is not recommended to use underscore and $at the beginning or end ⅲ is the form of small hump ⅳ do not start with a number ⅴ it is best not to use pinyin to name. Vi cannot use Java reserved words as variable names

2. The constant

① Literal constants

② Final keyword modifier constant

The final keyword means that the variable can only be assigned once, and once assigned, it cannot be changed. Constants defined this way are generally expressed in all caps.

Static final is a class constant that can be used in multiple methods within a class, usually defined outside the main method. If the constant is a public variable, it can be used in methods of other classes.

5. Data type conversion

1. Automatic conversion

Automatic data type conversion: Automatically converts a small range of values to a large range of values.

public class DataDemo {
    public static void main(String[] args) {
        int a = 1;
        byte b = 2;
// byte c = a + b;
// Int and byte operations, resulting in an intint c = a + b; System.out.println(c); }}Copy the code

Conversion rules:

Small types are promoted to large types: byte, short, and char are promoted to int.

Byte, short, char‐ >int‐ >long‐ >float‐ >double.

2. Cast type

Cast: Casts a wide range of values to a small range of values.

public class DataDemo2 {
    public static void main(String[] args) {

        / * * * int a = 1.5; Failed to compile, unable to assign */
        int i = (int)1.5;
        double d=2.5;
        // Int and double. The result is double
        // Int is promoted to doubledouble e = d + i; System.out.println(e); }}Copy the code

Converting a floating point to an integer without the decimal point may result in a loss of precision.

Int is forcibly converted to short, which may result in data loss.