Basic data types and reference types in Java

1. Basic data types:

byte : The smallest data type in Java, occupying 8 bits (bit) in memory, that is, 1 byte, the value range is -128~127, the default value is 0

short : short integer, occupying 16 bits in memory, that is, 2 bytes, the value range is -32768~32717, the default value is 0

int : Integer, used to store integers, occupies 32 bits in the inner, that is, 4 bytes, the value range is -2147483648~2147483647, the default value is 0

long : long integer, occupying 64 bits in memory, that is, 8 bytes -2^63~2^63-1, the default value is 0L

float : floating point type, occupying 32 bits in memory, that is, 4 bytes, used to store numbers with decimal points (the difference from double is that the effective decimal point of float type has only 6~7 digits), the default value is 0

double : Double-precision floating-point type, used to store numbers with a decimal point, occupies 64 bits in memory, that is, 8 bytes, the default value is 0

char : character type, used to store a single character, occupying 16 bits, that is, 2 bytes, the value range is 0~65535, the default value is empty

boolean : Boolean type, occupying 1 byte, used to judge true or false (only two values, namely true, false), the default value is false

Second, the basic concepts of Java data types :

In computer language, data type is an abstract expression for memory location, which can be understood as an abstract expression for memory. When you come into contact with each language, there will be a knowledge of data types. There are complex and simple. Various data types need to be understood in the early stage of learning. Java is a strongly typed language, so Java will be relatively strict in the specification of data types. The data type is the abstract atomic concept of the language, which can be said to be the most basic unit definition in the language. In Java, there are essentially two types of data types: basic types and reference data types.

  Primitive types: Simple data types are irreducible, built-in data types, defined by the programming language itself, which represent real numbers, characters, and integers.

  Reference data types: The Java language itself does not support the structure (struct) or union (union) data types in C++. Its composite data types are generally constructed through classes or interfaces. Classes provide a way to bundle data and methods, and at the same time Information hiding can be performed from outside the program.

Third, the relationship between data types and memory in Java

In Java, every variable that stores data has a type, such as:

char ch; float x; int a,b,c; 

ch is a character type, it will be allocated to 2 bytes of memory. Variables of different types have different numbers of bytes allocated in memory and different storage methods.

Therefore, before assigning a value to a variable, it is necessary to determine the type of the variable, determine the type of the variable, that is, determine the size of the memory space to be allocated for the data, and the storage method of the data in the memory.

Fourth, the storage of Java data types in memory :

1) Storage principle of basic data types: There is no concept of "reference" for all simple data types. Basic data types are directly stored on the memory stack in memory, and the value of the data itself is stored in the stack space. Eight data types in the Java language are this storage model;

2) The storage principle of reference type: The reference type inherits from the Object class (also a reference type) for data storage according to the memory model of storing objects in Java, and uses the Java memory heap and memory stack to store this type of data. , simply put, "references" are stored on the ordered memory stack, while the value of the object itself is stored on the memory heap;

Difference: The difference between basic data types and reference types is that basic data types are allocated on the stack, while reference types are allocated on the heap (requires stack and heap concepts in java) ,

The memory models of primitive types and reference types are fundamentally different.

Example 1: Let's analyze the difference between "==" and equals().

First, I set two String objects

Stringa="abc";

Stringb="abc";

Then

if(a==b){

System.out.println("a==b");

}else{

System.out.println("a!=b");}

The program outputs a!=b

Reason: the addresses of a and b are not the same, a==b compares the addresses of the two variables

Example 2: Defining two primitive types

int a=4;

int b=4;

if(a==b){System.out.println("a==b");}

else

{System.out.println("a!=b");}

output: a==b

Reason: == compares the contents of two variables

Conjecture: Whether it is a basic data type or a reference type, they will first allocate a piece of memory on the stack. For basic types, this area contains the content of the basic type; for object types, this area contains is a pointer to the real content, which is manually allocated on the heap .

Five, Java basic type value range calculation

From the perspective of computer composition principles, it can be explained:

Byte occupies 8 bytes in the computer, and byte is a signed integer. When expressed in binary, the highest bit is the sign bit. 0 represents a positive number and 1 represents a negative number.

The maximum value: 127 is 2 to the 7th power minus 1; the minimum value: that is, the 7th power of 2 is preceded by a negative sign: -128. (includes the beginning, does not include the end);

Positive numbers exist in the form of original codes in computers;

Negative numbers exist in the computer's complement form, that is, the original code of the absolute value of the negative number is converted into binary, and then inverted and then added 1.

The following 10 and -10 are introduced as an example: 10 original code: 00001010 Its storage in the computer is 0000 1010, -10 divides its absolute value by 10 according to the previous calculation, and converts it to binary 0000 1010 Bitwise negation After adding 1 to 1111 0101: 1111 0110, this is -10's complement. Okay, 1111 0110 in the computer represents -10.

 Let's look at the binary representation of the absolute value of -128: 128: 1000 0000 bitwise inversion 0111 1111 After adding 1: 1000 0000, that is to say, the representation of -128 in the computer is 1000 0000. Let's take a look at -129 in the computer The representation of the absolute value of 129 has exceeded the number of bytes in bytes. So pay attention to such issues;

Six, java type introduction

1 Overview:

Data types in Java are divided into reference data types and primitive data types.

   There are three types of reference data types: class, interface, and array;

   Basic data types are distributed and numeric types;

       Boolean type: boolean (logical type) true or false is false by default;

       Numerical types are divided into fixed-point types and floating-point types;

           Fixed-point types are divided into integer types and character types;

2. Default initialization of JAVA variables

Types of

Default initialization value

boolean

false

int

0

short

0

float

0.0

double

0.0

char

\

long

0

byte

0

object

null

3. Types are explained in detail:

1) Integer types: byte, short, int, and long all represent integers, but their value ranges are different.
byte (byte type) A byte with 8 bits, the value range is -128~127, occupying 1 byte (-2 to the 7th power of 2 to 2 to the 7th power -1) The default is 0
short (short integer) A short 16 bits, the value range is -32768~32767, occupying 2 bytes (-2 to the 15th power of 2 to 2 to the 15th power -1) The default is 0
int (integer) An int 32 bits, the value The range is (-2147483648~2147483647), occupying 4 bytes (-2 to the 31st power of 2 to 2 to the 31st power -1) default is 0
long (long integer) A long 64 bits, the value range is (- 9223372036854774808~9223372036854774807), occupying 8 bytes (-2 to the 63rd power of 2 to 2 to the 63rd power -1) The default is 0L or 0l. It is recommended to use uppercase;

It can be seen that the value range of byte and short is relatively small, while the value range of long is too large and takes up a lot of space. Basically, int can meet our daily calculations, and int is also the most used integer type. . Under normal circumstances, if an integer number such as 35 appears in JAVA, then this number is of int type. If we want it to be of byte type, we can add uppercase B: 35B after the data to indicate that it is of byte type. Yes, the same 35S means short type, 35L means long type, means int we can add nothing, but if we want to express long type, we must add "L" after the data.

1.1) Fixed-point constants

Fixed-point constants are integer constants, which can be represented in three ways: decimal, octal, and hexadecimal.

Decimal fixed point constants: such as 123, -456, 0.

Octal fixed point constant: leading 0, in the form 0dd...d. For example, 0123 represents the decimal number 83, and -011 represents the decimal number -9.

Hexadecimal fixed point constant: start with 0x or 0X, such as 0x123 for decimal number 291, -0X12 for decimal number -18.

1.2) Fixed-point variables

Fixed-point variables are integer variables, which can be subdivided into four types: byte variables, integer variables, short integer variables, and long integer variables.

Briefly describe the number of bytes of overhead memory and the range of values ​​for various fixed-point variables.

It should be noted that if you want to assign a certain point constant to a fixed point variable, you need to check whether the constant is within the expression range of the variable. If it exceeds the range, the program will compile an error.

2) char type (character type)

A character (char) in Java represents an element in the Unicode character set.

Unicode characters consist of 16 bits, so, there are (65535) different characters available,

The Unicode character set contains characters from all different languages, as well as common symbols in math, science, and writing, so it gives us a lot of flexibility.
Characters are expressed by a single character enclosed in single quotes, usually in hexadecimal,

The range is from '' to '?' (u tells the compiler that you are representing a Unicode character with two bytes of [16-bit] character information).

The data type used to store characters, occupies 2 bytes, uses unicode encoding, its first 128 bytes encoding is compatible with ASCII,
the storage range of characters is \~\?, pay attention to adding ' when defining character data ', such as '1' for the character '1' instead of the value 1.

2.1) Character constants

Character constants refer to single characters enclosed in single quotation marks, such as 'a', 'A', please pay special attention that the character delimiter is a single quotation mark, not a double quotation mark.

In addition to character constant values ​​of the form described above, Java allows a special form of character constant values,

This is usually used to represent characters that are difficult to represent with ordinary characters, this special form of character is a sequence of characters starting with a "\", called an escape character.

Commonly used escape characters in Java see the table

2.2) Character variables

Variables defined in char, such as char c='a';

It should be noted that the text encoding of Java adopts the Unicode set, Java character 16-bit unsigned data, and a character variable occupies 2 bytes in memory.

Note: char c = ' 1 ', we try to output c to see, System.out.println(c); the result is 1, and if we output like this, System.out.println(c+0); the result changes It becomes 49, this is because 0 is an int type, and the upward type conversion is performed, and the result is an int type.
       If we define c like this, char c = ' \1 '; the output result is still 1, because the character '1' corresponds to the unicode encoding which is \1.

 

3) Floating point type: Float and double are data types that represent floating point, and the difference between them lies in their precision.

3.1) Floating point constants

That is, a real value with a decimal point can be represented by two forms: a value with a decimal point directly and scientific notation:

Numerical form with decimal point: consists of a number and a decimal point, such as 0.123, .123, 123., 123.0.

Scientific notation representation: It consists of general real numbers and e±n (E±n), such as 12.3e3, 5E-3, which respectively represent 12.3 times 10 to the third power, and 5 times 10 to the -3 power. Note that there must be a number before e or E, and the exponent after e or E must be an integer.

3.2) Floating point variables

Floating-point variables are divided into single-precision variables and double-precision variables, and the number of memory bytes and the range of values ​​expressed by different precision overheads are different. The number of bytes of memory and the range of values ​​occupied by the two floating-point variables

There are also single-precision and double-precision floating-point constants. The constants listed above are all double-precision constants. If you want to specify that they are single-precision constants, you can add f or F as a suffix at the end of the data, such as 12.34f. If you want to specify that a floating-point constant is a double-precision constant, you do not need to add a suffix at the end of the data, or add d or D as a suffix at the end of the data, such as 12.34d.

float (single-precision floating-point type) a float of 32 bits, occupying 4 bytes, for example 3.2F, the default is 0.0f, 3.402823e+38 ~1.401298e-45 (e+38 means multiplied by 10 to the power of 38 , again, e-45 means multiply by 10 to the negative 45th power).
double (double precision floating point type) a dobule 64 bits occupies 8 bytes, for example 3.2, the default is 0.0, 1.797693e+308~4.9000000e-324 occupies 8 bytes

Note: The double type has a larger storage range and higher precision than the float type, so the usual floating-point data is double type if it is not declared.

If you want to indicate that a data is of float type, you can add "F" after the data. Floating-point data cannot be completely accurate, so sometimes the last few digits of the decimal point may float during calculations, which is normal.

Related introduction:

When using literal assignments for Java primitive types, there are a few simple features as follows:

1] When the data of integer type is assigned a literal value, the default value is the int type, that is, when 0 or other numbers are used directly, the value type is the int type, so when using the assignment method of long a = 0 , there is data conversion inside the JVM.

2) When the data of floating-point type is assigned a literal value, the default value is the double type, that is, when the literal two appears, the JVM will use the double type of data type.

3) Since JDK 5.0, the operation of automatic unboxing and unboxing has appeared in Java. Based on this, some instructions need to be made:

Corresponding to the original data type, each data type has a reference type encapsulation class, namely Boolean, Short, Float, Double, Byte, Int, Long, Character, these types are built-in encapsulation classes, these encapsulation classes ( Wrapper) provides a very intuitive method. What needs to be explained for the encapsulation class is that each encapsulation class has a xxxValue() method, through which the value in the object it references can be converted into the value of the basic variable, Not only that, each encapsulation class also has a valueOf(String) method to directly convert the string object to the corresponding simple type.

 Before JDK 5.0, there was no automatic unboxing operation, that is, the Auto Box operation, so the following assignment code could not be used before this: Integer a = 0;//This assignment method cannot be used in JDK 1.4 and The following JDK compilers pass, but JDK 5.0 has an automatic unboxing operation, so in the compilers above JDK 5.0, the above code can pass

 

Reference data type:

array

String: String type, used to store a string of characters

Java variable declaration and use:

data type variable name = value, expression;

Example: String name = "Conan";

    int a= 50;

Note: "=" is not an "equal sign" in mathematics, but an assignment operator

Java variable naming rules:

1: Must start with a letter, underscore "_", or "$" sign

2: Can include numbers, case sensitive

3: You cannot use keywords in the Java language, such as int, class, public, etc.

Six operators in Java:

· Arithmetic operators

· Assignment operator

· Relational operators

· Logical Operators

· Bitwise operators

· Ternary operator

 

Arithmetic operators:

+: addition operation, sum of operands

-: Subtraction operation, find the difference of the operands

*: Multiplication operation, find the product of the operands

/: division operation, find the quotient of the operands

%: remainder operation, find the remainder of the division of the operands

++: auto increment, the operand is incremented by 1

--: Decrement, the operand is decremented by 1

 

Assignment operator:

=: assign the value on the right to the left, for example: int a = 1;

+=: The sum of the left and right sides is added to the left, for example: int a = 1; a+=2; the value of the result a is 3

-=: The difference between the left and the right is assigned to the left, for example: int a =5;a-=2; the value of the result a is 3

*=: The value of the multiplication of the two sides is assigned to the left, for example: int a = 2; a*=2; the value of the result a is 4

/=: Divide the left side by the value on the right side and assign it to the left side, for example: int a = 6; a/=2; the value of the result a is 3

%=: The remainder of the left side divided by the right side is assigned to the left side, for example: int a =7;a%=2; the value of the result a is 1

 

relational operator

>: greater than, for example: int a = 1; int b = 2; System.out.print(a > b); the result is false

<: less than, for example: int a = 1; int b = 2; System.out.print(a < b); its result is true

>=: greater than or equal to, for example: int a = 1; int b = 2; System.out.print(a >= b); the result is false

<=: less than or equal, for example: int a = 1; int b = 2; System.out.print(a <= b); the result is true

==: equals, for example: int a = 1; int b = 2; System.out.print(a == b); its result is false

!=: not equal, for example: int a = 1; int b = 2; System.out.print(a != b); the result is true

  The result is a boolean type, that is, either true or false

 

Logical Operators

&&: AND, AND (short circuit), the result is true when both conditions are true at the same time

||: or, or (short-circuit), when one of the two conditions is true, the result is true

!: Not, (!+condition) When the condition is true, the result is false

 

Data Type Conversion in Java

1: Automatic data type conversion (enlargement conversion)

The automatic data type conversion conditions are met:

1) The two types should be compatible: such as numeric types (integer and floating point)

2) The target type is greater than the source type: for example, int type data can be automatically converted to double type

 

 

2: Force data type conversion (narrowing conversion)

Put parentheses before the variable, inside the parentheses specify the type to cast

例:double a = 40.9;

    int b= (int)a;

Note: Coercion will lose numerical precision. For example, the double type variable a, which is coerced to int type, becomes 40.

Guess you like

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