Java back-end knowledge map "JAVA basic" data type

table of Contents

  • A, java, there are two types of data: the built-in basic data types; reference data types
  • Second, the two types of data storage positions in each case
  • Third, the basic data types between type conversion: low level to high automatic type conversions; advanced to lower casts
  • Fourth, a reference type conversions between data types: upward transition, downcast
  • Five, corresponding to the type of basic object-oriented packaging: automatic packing, automatic unpacking
  • Six, Integer type, the cache will be -128 to 127, using the value of Comparative pits Integer object ==

 

 

A, java, there are two types of data: the built-in basic data types ; reference data types

1, Java language provides eight basic types. Six kinds of digital type (four integer, two floating point) A character type, there is a Boolean.

byte: 8 bits, signed integer two's complement code representation; the default value is 0;

Short: Short data type is 16-bit, signed two's complement integer representation; the default value is 0;

int: int data type is 32-bit, signed integer two's complement representation; the default value is 0;

Long: Long data type is 64-bit, signed two's complement integer representation; the default value is 0L;

a float: a float data type is single-precision, 32-bit floating-point number; the default value is 0.0f;

Double: Double data type is a double-precision, 64-bit floating-point number; the default value is 0.0d;

boolean: only two values: true and false; the default value is false;

char: char type is a single 16-bit Unicode character; char data type can be stored any character; the default value is '\ u0000'

In fact, JAVA, there are still another basic type void, it also has a corresponding wrapper class java.lang.Void, but we can not operate on them directly.

2, reference data types: a reference type variable pointer is very similar C / C ++ is. Point to the object variable is a reference variable (the variable value stored in the memory address, the memory address value is the value of the object we need inside the store). These variables are specified in the declaration of a particular type, such as Employee, Puppy like. Once the variable declaration, the type can not be changed. Interfaces, classes, data array is a reference type. The default for all reference types are null.

 

Second, the basic data types, data type stored in a reference position in each case

Value points to object reference variables are stored in the heap memory, but not necessarily reference variables.

1, local variable storage locations

Variables declared in the method, i.e., the variables are local

  • Whenever a program calling method, the system will establish a method for the method stacks, variables declared in their approach to the stack on the method, the method ends when the stack will release method, which corresponds to a variable declared in this method with the destruction and the end of the stack, local variables which can only be effective in the method of reason.
  • In the process variable declaration can be basic types of variables can also be a reference type variable:
  • 1) When the declaration is the basic type of time variable, the variable name and its value (variable name and value are two concepts) are on the JAVA virtual machine stack
  • 2) When the statement is a reference variable when the declared variable (the variable is actually stored in the method is the memory address value) on the stack is a JAVA virtual machine , the variable object pointed to is on the heap class deposit in the

2, members of the variable storage location
in the variable declared in the class member variables, also known as global variables

  • On the heap (because the global variable does not end with the execution of a method to destroy).
  • Also in the variable declared in the class can be basic types of variables, but also a reference type variable:
  • 1) When the declaration is the basic type of variable whose variable names and their values are on the heap memory
  • 2) a reference type , the variable will still be stored in its declaration of the value of a memory address, the memory address value pointing to the referenced object. Reference variable and corresponding object names are stored in a respective stack

3, static variable storage location

In fact, the constant static variables

  • They are stored in the method area of ​​the JVM
  • static variables are initialized, the method area of ​​the JVM, the entire memory is only a static variable copy when the class load store, use the class name to directly access and to be accessed through instances of object classes, is generally not recommended by instantiating Object access, the popular talk static variables belong to the class, does not belong to an object, any instance of an object is accessed with a static variable, to put any static variables can be accessed by class name.

 

Third, the basic data types between type conversion: low level to high automatic type conversions; advanced to lower casts

1, can not be a boolean type conversion.

2, the simple data type conversion from low to high: (byte, short, char) -> int -> long-> float -> double

3, lower type variables can be directly converted to advanced type variable, called the automatic type conversion. byte b; int i = b;

4, for a byte, short, char three types, which are the same level, can not be automatically converted to each other

5, convert the high level is lower variable variables need to use casts, int i = 99; byte b = (byte) i;

Basic data types of automatic transition characteristics tend to go wrong, the use of reference data type packaging

 

Fourth, a reference type conversions between data types (one JAVA polymorphism): upward transition, downcast

1, upcast : by subclass object (small) instantiating parent object (wide), which belongs to the automatic conversion, makes the code more beautiful abstract. Format: parent class type variable name = new subclass type ();

2, downcast: by the parent object (wide) instantiable subclass objects (small), which belongs to the cast, less use is to subclass the explicit call, before a certain downcast It is first on the transformation, otherwise it will run down the transition of error. Format: subclass type variable name = (subclass type) parent class variable name;

 

Five, corresponding to the type of basic object-oriented packaging: automatic packing, automatic unpacking

Java provides 8 kinds of packaging data types eight primitive data types and the corresponding type of packaging was to address the basic data types can not be provided by object-oriented programming.

Basic data types type of packaging
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double

1, autoboxing automatically convert basic data types into packaging type, Integer i3 = 8;

2, automatic unpacking automatically converts the package type to the base data, int i4 = i3;

In the method of reception parameters, boxing and unboxing can be automatically set when the object parameters.

 

Six, Integer type, the cache will be -128 to 127 , using the value of Comparative pits Integer object ==

But numerical comparison between the primitive type int == is no problem, so multi-purpose equals method, less == == is more variable reference value, if it is relatively between the object pointed to is the address of a reference type variable Comparison.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 74 original articles · won praise 48 · views 190 000 +

Guess you like

Origin blog.csdn.net/wangpeng322/article/details/104223947