Start with a mind map:

Integer variables

Code examples:

Matters needing attention:

  • Int indicates that the type of the variable is an integer
  • A variable name is the identification of a variable. This name is used for the subsequent use of variables
  • In Java, = means assignment (unlike math), which means to set an initial value to a variable.
  • Initialization is optional, but it is recommended that variables be explicitly initialized when they are created.
  • Finally, don’t forget the semicolon, or you will fail to compile.
  • In Java, an int variable is four bytes long, and the data range is -2^31 -> 2^31-1.

Long integer variable

Code examples:

Matters needing attention:

  • The basic syntax is basically the same as creating int variables, except that the type is changed to long
  • The initial value is 10L, indicating a long integer. 10L is also acceptable.
  • 10 is of type int and 10L is of type long. It is better to use 10L or 10L.
  • In Java, the long type is 8 bytes. The range of data represented is -2^63 -> 2^63-1

A double precision floating point variable

Code examples:

Example code:

Note here:

  • A Double in Java is also 8 bytes, but floating-point numbers have a very different memory layout than integers, and cannot be represented as a 2 ^ n range.
  • Java’s double memory layout complies with the IEEE 754 standard (as in C), which attempts to represent potentially infinite decimals with finite memory space, resulting in certain precision errors.

A single-precision floating point variable

Code examples:

Note:

float type accounts for four bytes in Java and also complies with the IEEE 754 standard. Due to the small range of represented data accuracy, in general engineering applications using floating point numbers are given priority to double and the use of float is not quite recommended.

Character type variable

Code examples:

Note:

  • Java uses single quotation marks + single letters to represent character literals.
  • A character in a computer is essentially an integer. ASCII is used for characters in C, while Unicode is used for characters in Java. So a character takes up two bytes and represents a wider variety of characters, including Chinese.

Byte type variable

Code examples:

Note:

  • Byte types also represent integers. Only one byte, indicating a small range (-128 -> +127)
  • Byte types and character types are unrelated

Short integer variable

Code examples:

Note:

  • Short takes up two bytes and represents data in the range of -32768 -> +32767
  • This is not recommended because it has a small range

Boolean type variable

Code examples:

Note:

  • Boolean variables have only two values, true for true and false for false
  • Java Boolean types and ints cannot be converted to each other. There is no such thing as 1 for true and 0 for false
  • Boolean Type Some JVM implementations are 1 byte, some are 1 bit, this is not specified

String variable

Code examples:

Note:

  • Java uses double quotes + several characters to represent string literals.
  • Unlike the above type, String is not a primitive type, but a reference type
  • Certain characters in a string that are not easily represented directly need to be escaped.

String + operation to indicate string concatenation:

It is also possible to concatenate strings and integers:

It should be noted that if the number is first, the operation is performed before the splicing

Type conversion

When the value of a lower-level variable is assigned to a higher-level variable, the system automatically converts the data type

When assigning a value from a higher-level variable to a lower-level variable, you must use the type conversion operation format: (type name) the value to be converted; Here’s an example:

Be careful if values overflow during type conversions

The last

At the end of the article the author sorted out a lot of information for you! Including Java core knowledge + a full set of architect learning materials and video + first-line factory interview treasure dictionary + resume template interview ali Meituannetease Tencent Xiaomi IQiyi Quick hand bilibili bilibili interview questions +Spring source code collection +Java architecture actual combat e-books and so on! Welcome to pay attention to the public number: the future has light, receive!