JAVA Summary Part II (Data Types and Strings)

1. Eight data types

Note: The eight basic data types are divided into 4 categories: integer types, decimal (floating point) types, Boolean types, and character types; String does not belong to the eight basic data types; remember not to confuse the eight data types with their corresponding encapsulation classes , their encapsulation classes cannot be defined as eight data types.

1. Bytes and characters:

1. English letters, numbers and symbols all occupy one byte, while Chinese characters occupy two bytes, also known as characters.

2. One byte = 8 bits.

2. Character type byte:

The smallest integer type, occupying one byte, that is, a signed 8-bit type; its range is: -128~127.

Definition of variables: byte a=1, byte a='q', byte=','.

JAVA uses byte as an integer for operation, and directly uses ASCII for operation. Chinese is Unicode code.

3. Short integer short:

Signed 16-bit type, occupying 2 bytes; its range is: -32768~32767.

Definition of variable: short a = 3.

Four.int:

The most commonly used basic data type occupies 4 bytes, that is, a signed 32-bit type; its range is: -2^31~2^31-1.

Definition of variable: int a = 11.

Five. Long integer long:

It occupies 8 bytes, which is a signed 64-bit type; its range is: -2^31~2^31-1.

Definition of variables: long a = 222, long a = 222L (the end with L is used to distinguish the int type, indicating that a long type is defined).

6. Single-precision floating-point float:

Use 32 bits to store a value.

Definition of variable: float a = 3.3f (the end with f is used to distinguish the double type, indicating that a float type is defined).

Seven. Double-precision floating-point double:

Use 64 bits to store a value.

Definition of variable: double a=3.333.

Eight. Character char:

JAVA uses Unicode to represent characters, and the smallest unit is a word, that is, two bytes.

Char is a 16-bit type that occupies one character, that is, two bytes; the range is 0~65536, there is no negative number, and only one Chinese character can be stored.

char is operated according to the integer type of ASCII code at runtime, but when output, the output is the character corresponding to the ASCII code corresponding to the output value.

9. Boolean boolean:

boolean is the basic type that represents logical values, its value is only true or false (ie true or false), it is the return type of all relational operations; it is also the type required for conditional expressions (used to dominate control statements: if and while ).

10. Conversion of basic data:

1. Automatic conversion: low-precision values ​​can be automatically converted to high-precision values, but not vice versa;

Error listing: int i=1.2;

Correct listing: double i = 1;

精度顺序:byte<short<int<long<float<double。

2. Force conversion:

Display: int i = (int)1.2;

Note: Casting does not round, but discards the extra fractional part.

3. Remember not to violate the principle of automatic conversion during operation:

Such as int a = 1;

int i=a + 1.2;

This violates the principle of automatic conversion.

2. String

Strings are mainly defined by String variables.

如:String name="tom";

Strings can be directly linked together to form a new string,

如:String a = "a";

    String ab = a+"b";

    Then the value of ab is "ab";

Note: There are two special existences in the definition of strings, namely: String a=null;String b="";

Where a reference is empty, no space is allocated, and it is not an instantiated object;

The b reference is an empty string, allocated space, and is an instantiated object.

3. ASCII table and URLs that can be encoded in Unicode:

ASCII table URL:

http://www.asciima.com/ascii/12.html


Unicode encoded URL:

http://www.bangnishouji.com/tools/chtounicode.html


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117221&siteId=291194637