Java basic data Type storage scope

Source: Internet
Author: User

Data types can be divided into two main categories: 1) basic type; 2) extension type.

First look at the basic data types of the Java language. It includes a type description value range Boolean Boolean with only two values true, false char character type

Byte 8-bit signed integer-any integer between 128 and 127

Any integer between short 16-bit unsigned integer -32768~32767

An int 32-bit signed integer -2^31 to 2^31-1 any integer

Long 64-bit signed integer -2^63 to 2^63-1 any integer

Float 32-bit single-precision floating-point number according to ieee754-1985 standard

Double 64-bit dual-precision floating-point number according to the ieee754-1985 standard some hints:

Unlike other programming languages, the number of integers in the Java language is fixed and not different depending on the hardware platform and the operating system.

You can see that the value range of byte and short is small, and Long's range is too large, occupy more space, basically int can meet our daily calculations, and int is the most used integer type.

Float Float 4 bytes

Double Double 8 bytes

The difference between them is that their accuracy is different .

Double type more than float type storage range, more accurate, so the usual floating-point data is not declared in the case of double type, if you want to indicate that a data is float type, you can add "F" after the data.
The floating-point data is not completely accurate, so sometimes it is normal to have floating in the last few decimal places when calculating.

Character type :

Char Character 2 bytes

The data type used to hold characters, 2 bytes, Unicode encoded, and ASCII compatible with the first 128 bytes of encoding
Characters are stored in \u0000~\uffff, when defining character-type data, be careful to add ' ', such as ' 1 ' for the character ' 1 ' instead of the value 1

The base data type is placed on the stack or in the heap, depending on the location of the base type declaration.

A: The variable declared in the method, that is, the variable is a local variable, each time the program calls the method, the system will establish a method stack for the method, the variables declared in the method is placed in the method stack, when the method end system will release the method stack, corresponding to the method declaration of the variable with the destruction of the stack end, This is why local variables can only be valid in a method

A variable declared in a method can be a variable of a primitive type, or a variable of a reference type.

(1) When the declaration is a basic type of variable, its variable name and value (variable name and value are two concepts) is placed in the method stack

(2) When a reference variable is declared, the declared variable (which is actually stored in the method as a memory address value) is placed in the stack of the method, and the object to which it points is placed in the heap class store.

Two: A variable declared in a class is a member variable, also called a global variable, placed in the heap (because the global variable is not destroyed with the end of a method execution).

A variable declared in a class can be a variable of a primitive type and a variable of a reference type.

(1) When declaring a variable of the base type whose variable name and its value are placed in heap memory

(2) When referencing a type, the variable it declares will still store a memory address value that points to the referenced object. Reference variable names and corresponding objects are still stored in the appropriate heap


In general, if there is an integer number such as 35 in Java, then this number is the int type, if we want it to be a byte type, we can add the uppercase b:35b after the data, indicating that it is byte type, the same 35S means short type, 35L means long, which means int we can do nothing, but if you want to represent a long type, be sure to add "L" after the data.

Java basic data Type storage scope

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.