Basic data types and reference data types in JAVA

1. Basic data types:

byte : 8 bits (bit), that is, 1 byte, the maximum amount of data stored is 255, and the range of stored data is -128~127. Default value 0

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

int : 32 bits, that is, 4 bytes, the value range is - 2147483648~2147483647, the default value is 0

long : 64 bits, i.e. 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 is 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

Java determines the size of each simple type. These sizes do not vary with machine configuration. The immutability of this size is one of the reasons why Java programs are so portable. The following table lists the simple types defined in Java, the number of bits they occupy, and the corresponding wrapper classes.

simple type boolean char byte short Int long float double void
binary digits 1 16 8 16 32 64 32 64 --
byte 1 2 1 2 4 8 4 8 --
Default initialization value false \ 0 0 0 0 0.0 0.0  
wrapper class Boolean Character Byte Short Integer Long Float Double Void

 

 

 

 

 

Second, the storage of Java data types in memory :

1) The storage principle of basic data types: all simple data types do not have the concept of "reference", the basic data types are directly stored in the memory stack in memory, the value of the data itself is stored in the stack space, and 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 have 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 .

Three, 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 is the representative of - 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 -128 in the computer is represented by 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. So pay attention to such issues;

Fourth, the conversion between data types

1). There are two ways to convert between simple types of data: automatic conversion and coercive conversion, which usually occur in expressions or when parameters of methods are passed.

automatic conversion

Specifically, when a smaller "small" data is operated with a larger "large" data, the system will automatically convert the "small" data into "large" data, and then perform the operation. When the method is called, the actual parameter is "smaller", and the formal parameter data of the called method is "larger" (if there is a match, of course, the matching method will be called directly), the system will also automatically set the "smaller" "Data is converted into "big" data, and then the method is called. Naturally, for multiple overloaded methods with the same name, the "closest" "big" data will be converted and called. These types from "small" to "large" are (byte, short, char)--int--long--float-double. Here we say "big" and "small", not the number of bytes occupied, but the size of the range that represents the value.

①The following statement can be passed directly in Java:

byte b;int i=b; long l=b; float f=b; double d=b;

②If the low-level type is char type, when converting to the high-level type (integer type), it will be converted to the corresponding ASCII code value, for example

char c='c'; int i=c;

System.out.println("output:"+i); 输出:output:99;

③ For the three types of byte, short, and char, they are equal, so they cannot be automatically converted to each other. The following forced type conversion can be used.

short i=99 ; char c=(char)i; System.out.println("output:"+c); 输出:output:c;

cast

When converting "big" data to "small" data, you can use casts. That is, you must use the following statement format: int n=(int)3.14159/2; As you can imagine, this conversion may definitely lead to overflow or loss of precision.

2) The data type of the expression is automatically promoted. Regarding the automatic promotion of the type, pay attention to the following rules.

① All byte, short, and char values ​​will be promoted to int;

②If one of the operands is of type long, the result of the calculation is of type long;

③ If one of the operands is of type float, the calculation result is of type float;

④If one of the operands is of double type, the calculation result is of double type;

For example, byte b; b=3; b=(byte)(b*3);// byte must be declared.

3) Wrapper class transition type conversion

In general, we first declare a variable, and then generate a corresponding wrapper class, and we can use various methods of the wrapper class for type conversion. E.g:

①When you want to convert float type to double type:

float f1=100.00f;

Float F1=new Float(f1);

double d1=F1.doubleValue();//F1.doubleValue() is the method of returning double value of Float class

②When you want to convert double type to int type:

double d1=100.00;

Double D1=new Double(d1);

int i1=D1.intValue();

Variables of simple types are converted to the corresponding wrapper classes, and the constructor of the wrapper class can be used. Namely: Boolean(boolean value), Character(char value), Integer(int value), Long(long value), Float(float value), Double(double value)

In each wrapper class, there is always a method of ××Value() to get its corresponding simple type data. Using this method, the conversion between different numerical variables can also be realized. For example, for a double-precision real type, intValue() can get its corresponding integer variable, and doubleValue() can get its corresponding double-precision real type. type variable.

4) Conversion between strings and other types

Conversion of other types to strings

①Call the string conversion method of the class: X.toString();

②Automatic conversion: X+"";

③ Method of using String: String.volueOf(X);

String as value, conversion to other types

① First convert to the corresponding wrapper instance, and then call the corresponding method to convert to other types

For example, "32.1" in characters converts to a double value in the format: new Float("32.1").doubleValue(). Also works: Double.valueOf("32.1").doubleValue()

②Static parseXXX method

String s = "1";

byte b = Byte.parseByte(s);

short t = Short.parseShort(s);

int i = Integer.parseInt(s);

long l = Long.parseLong(s);

Float f = Float.parseFloat(s);

Double d = Double.parseDouble(s);

③Character's getNumericValue(char ch) method

5) Conversion between Date class and other data types

There is no direct correspondence between the integer and the Date class, but you can use the int type to represent the year, month, day, hour, minute, and second, respectively, thus establishing a correspondence between the two. When doing this conversion, you can use three forms of the Date class constructor:

①Date(int year, int month, int date): Represents year, month, and day in int type

②Date(int year, int month, int date, int hrs, int min): Represents year, month, day, hour and minute in int type

③Date(int year, int month, int date, int hrs, int min, int sec): Int type represents year, month, day, hour, minute, second

An interesting correspondence between long integers and the Date class is to represent a time as the number of milliseconds from January 1, 1970 0:0:0:0 GMT. For this correspondence, the Date class also has its corresponding constructor: Date(long date).

To get the year, month, day, hour, minute, second and week of the Date class you can use the Date class getYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds(), getDay( ) method, you can also understand it as converting the Date class to an int.

The getTime() method of the Date class can get the long integer number corresponding to a time we mentioned earlier. Like the wrapper class, the Date class also has a toString() method that can convert it to a String class.

Sometimes we want to get a specific format of Date, such as 20020324, we can use the following method, first introduce it at the beginning of the file,

import java.text.SimpleDateFormat;

import java.util. *;

java.util.Date date = new java.util.Date();

 

// If you want to get the format of YYYYMMDD

SimpleDateFormat sy1=new SimpleDateFormat("yyyyMMDD");

String dateFormat=sy1.format(date);

 

// If you want to get year, month, day separately

SimpleDateFormat sy=new SimpleDateFormat("yyyy");

SimpleDateFormat sm=new SimpleDateFormat("MM");

SimpleDateFormat sd=new SimpleDateFormat("dd");

String syear = sy.format (date);

String smon=sm.format(date);

String sday=sd.format(date);

Summary: Only boolean does not participate in data type conversion

(1). Automatic type conversion: a. Constants can be automatically type converted within the range of the number of tables

b. Small data range can automatically convert large data types (note special cases)

Int to float, long to float, long to double will not be automatically converted, otherwise the precision will be lost

c. The reference type can be automatically converted to the parent class

d. Basic types and their wrapper types are convertible to each other

(2). Coercion type conversion: enclose the target type in parentheses and place it before the variable

5. Java reference types

Java has 5 reference types (object types): class interface array enumeration annotation

Reference type: the underlying structure is quite different from the basic type

JVM memory space: (1). Heap heap space: allocate object new Student()

(2). Stack stack space: temporary variable Student stu

(3).Code code area: class definition, static resource Student.class

eg: Student stu = new Student(); //new creates an object in the heap space of memory

stu.study(); // assign the address of the object to the stu reference variable

The implementation steps of the above example: a. JVM loads Student.class into the Code area

     b.new Student() allocates space in the heap space and creates a Student instance;

     c. Assign the address of this instance to the reference stu, stack space;

Guess you like

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