Thinkeing in Java--java Basic object types and how they are stored

Source: Internet
Author: User

I. Classification of objects in Java

1. Basic type

Basic type Size Minimum value Maximum Value Wrapper type
Boolean --- --- --- Boolean
Char 16-bit Unicode o Unicode 216-1 Character
Byte 8 bits -128 +127 Byte
Short + Bits -215 +215-1 Short
Int Bits -231 +231-1 Integer
Long + Bits -263 +263-1 Long
Float Bits IEEE754 IEEE754 Float
Double + Bits IEEE754 IEEE754 Double
void --- --- --- Void

Ieee754:s: Sign bit; EXP: digit; fraction: active digit

32-bit single-precision, single-precision binary decimals, using 32-bit storage. 1 8 23 bit long +-+--------+-----------------------+| s| Exp | Fraction |+-+--------+-----------------------+31 30 23 22 0-digit number (starting from the right to 0) bias positive + 12,764-bit double-precision binary decimal, using 64-bit storage. 1 11 52 bit long +-+--------+-----------------------+| s| Exp | Fraction |+-+--------+-----------------------+63 62 52 51 0-digit number (starting from the right to 0) bias positive +1023

http://baike.baidu.com/link?url=8- Nwanonlarfd3gh2lzl-iuaqe70tzyfeqhrbk6lm3a2wiev7nptoqn936tan7kgit3g-8cpnyr7lvjeohtwrlglivefcdce3nokgsnoz40fgfjrlk6tfvxz93i T_7sfwtwpwsbmvli6kibif2k5t8jzol80lyw_zszrlbfben3

VOID type: Class only, used to identify no return object

Wrapper:

Java can handle basic types directly, but in some cases we need to treat them as objects, and then we need to convert them into wrapper classes. All packaging classes (Wrapper Class) have common methods, and they are:

(1) A constructor that has a basic value parameter and creates a wrapper class object. If you can create an object with an integer wrapper class, Integer obj=new integer (145);

(2) A constructor that takes a string argument and creates a wrapper class object. such as new Integer ("-45.36");

(3) Typevalue method, such as Obj.intvalue (), which can generate the base value of the object;

(4) A Parsetype method that converts a string to a base value, such as Integer.parseint (Args[0]);

(5) Hashcode method for generating the code of the halon table, such as Obj.hascode ();

(6) The Equals () method that compares two objects of the same class, such as Obj1.eauqls (OBJ2);

(7) The ToString () method that generates the string notation, such as obj.tostring ().

Transition Relationship:

Basic type------> wrapper class
Integer obj=new integer (145);

Wrapper class------> Basic Types
int Num=obj.intvalue ();

string------> Wrapper class
Integer obj=new Integer ("-45.36");

Wrapper class------> String wrapper class

String str=obj.tostring ();

String------> Basic Type
int Num=integer.parseint ("-45.36");

Basic type------> String wrapper class

String str=string.valueof (5);

2. Arrays

An array in Java is a set of reference arrays, that is, when an array is created, it is the object's reference, not the specific object of some column;

After it is created, the default value is NULL, so it cannot be compiled, so it must be assigned to be initialized for normal compilation;

Create an int array of length 5;

int[] Testintarray = new INT[5];

3. Class

Used to create custom objects in Java, composed of members (attributes), methods;

Ii. how objects are stored

1, 5 kinds of storage media

1) Register

The registers inside the CPU, have no sense to the user and the program, cannot control;

2) stack

In general RAM, it is mainly used to store variables in the code, that is, reference addresses (like pointers);

Java allocates memory space to the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees up the allocated memory space for that variable, which can be used immediately by another.

3) Heap

Used to store objects and arrays created by new, and objects referenced by variables in the stack are generally stored in the heap;

The memory allocated in the heap is controlled by the garbage collector;

4) constant storage, typically in read-only memory (ROM), such as String constants

5) non-RAM storage

such as hard disk, db, MySQL, etc.;

Third, object scope

1, the Java object and the basic line of the life cycle is different ; new objects can live outside the object, see the following memory allocation mechanism; and garbage collector;

Iv. allocation of variables in memory in Java

1. Class variable (static modified variable): When the program loads, the system opens up memory in the heap, and the memory address in the heap is stored on the stack for high-speed access. The life cycle of a static variable--continues until the entire system shuts down

2, instance variables: When you use the Java keyword New, the system in the heap is not necessarily a continuous space allocated to variables (such as class instances), and then based on the scattered heap memory address, the hash algorithm is converted to a long series of numbers to characterize the variable in the heap "physical location." Life cycle of instance variables-when a reference to an instance variable is lost, it is included in the recyclable "list" by the GC (garbage collector), but the memory in the heap is not released immediately

3, local variables: local variables, by declaring in a method, or a code snippet (such as a For loop), execution to it in the stack to open up memory, when the local variable one but out of scope, memory immediately released

V. Memory mechanism of Java

Java divides memory into two types: one is stack memory and the other is heap memory. Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function, and when a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable. The memory space can be used immediately by another.

Heap memory is used to store the objects and arrays created by new, and the memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. The reference variable in the stack can then be used in the program to access an array or an object in the heap, which is equivalent to a name that is an array or an object. A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or object's statement, and the array and object become garbage when no reference variable points to it, not in use, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time.

This is the reason why Java compares memory, in fact, the variables in the stack point to the variables in the heap memory, this is the pointer in Java!

Thinkeing in Java--java and how to store the base object type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.