JavaSE - Basic Data Types

  • Some basics:
  1. Byte and character, byte and bit; 1 byte = 8 bits (bit) (the highest bit is the sign bit, 0 is positive and 1 is negative) bit is the smallest unit in computer memory. In a binary computer system, Each bit can represent a digital signal of 0 or 1. bps is short for bits per second. Generally, the transmission rate of data machine and network communication is in bps. Such as 56Kbps, 100.0Mbps, etc.
  2. The value of type Float will have the suffix F or f. The floating point number without this suffix defaults to the double type. Of course, the suffix D and d can also be added to the floating point number; the long integer type has a suffix L or lowercase l; hexadecimal Has a prefix 0X or 0x; binary prefixes 0b or 0B
  3. Starting from Java 7, underscores can be added to numeric literals. For example, one million can be represented as 1_000_000. The compiler will remove these underscores when compiling.
  • Primitive data types/reference data types
  1. Memory usage: Primitive data types are stored directly in stack memory. The reference type is that the content is stored in the heap memory, and the address is stored in the stack memory. When accessing, the content of the heap memory is found through the address of the stack memory.
  2. Variable assignment: The basic data type is direct; directly assign a value to a variable. The reference type is to copy the address of the value to the new variable, and then find the content through the address
  • pass-by-value problem

  

  • Basic data types (8 types in 4 types): integer type (4 types), decimal point type/float type (2 types), boolean type (1 type), character type (1 type)

    Integer types : byte, short, int, long

    The formula for range and bytes:

byte: 1 byte, -128-127, (it should be -127-127, because 0 has the difference between +0 and -0, so there is an extra -0, and -0 is regarded as -128, Contributions made by Indians)

short: 2 bytes, -32768-32767

int: 4 bytes, -2147483648-2147483647 just over 2 billion

long: 8 bytes, -2^63~2^63-1, about 9*10^18 Massive numbers can be solved by strings or character arrays

Decimal type : float, double

    Float: single precision type, 4 bytes, about 6-7 significant digits

             -3.403E38~3.403E38

Double: Double type, 8 bytes, about 15 significant digits

Note:

float a=1.3; this will report an error, because 1.3 defaults to double, and high precision to low precision will lose precision. The correct one should be float a=1.3f; the seventh digit of Float will result in rounding

Three special floating-point values ​​used to represent overflow and error conditions: positive infinity (double.positive_infinity), negative infinity (double.negative_infinity), NaN (Double.NaN), and the corresponding constants of type float. Where NaN is not a number, it cannot be used to determine whether a variable is ==NaN, the correct way to use it is double.isNaN(xxxx) returns a boolean value

    Boolean type : true and false

    Character type : char, string

      char: single character, 2 bytes, can store Chinese characters; string: multiple characters (String is not a basic data type, but a reference data type)

  • other:
  1. The Ascii code of "A" is 65, the Ascii code point of "a" is 97, and the Chinese character uses unicode code
  2. Coercion:

The data type can be automatically converted from low precision to high precision, and high precision to low precision requires coercion

Int a =(int)1.2;

Int a = (int)1.9; In the final result, a is 1, that is, regardless of the back, just intercept the front integer part      

Float a=1.2; //Error reporting, loss of precision, because decimals in Java are of type double by default

Float a =1.2f; //correct

Int a=3;

Int b=a+3.4;//In the computer, first calculate a+3.4 and turn to high precision, so 6.4 is obtained, but it cannot be stored in int type at this time (addition, subtraction, multiplication and division will all turn to high precision)

  3. Reference data types: arrays, classes, interfaces, enumerations

  4. Uninitialized variables cannot be used directly, and the compiler will report an error; the initialization and declaration of variables can be placed on the same line, that is, the declaration and assignment are in the same sentence of code; the use of final to indicate constants means that once assigned, it will be It can't be changed anymore. It is customary to use all capitals to represent constants. If you need a constant to be used by multiple methods of a class, you can add static in front of it.

 

Guess you like

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