Java basics Section 1: Data Types, java Data Types

Source: Internet
Author: User

Java basics Section 1: Data Types, java Data Types

The basic type, or built-in type, is a special type different from the class in JAVA. They are the most frequently used types in programming. Java is a strongly typed language. The first declaration of a variable must describe the data type. The first assignment of a variable is called initialization of a variable, however, you must assign a value before using it ).

1. Data Type Classification

Basic data type and Reference Data Type

Basic data types (4 types and 8 types)

Integer:

Byte 1 byte-128 ~ 127

Short 2-byte length: 65535

Int 4 bytes (Java default data type)

Long 8 bytes

Floating point type:

Float: 4 bytes

Double: 8 bytes (Java default data type)

 

Character Type: char 2 bytes

Boolean Type: Boolean 1-byte result true/false

 

Reference data type:

Class, array, Interface

    

2. Packaging of Basic Data Types

Basic bytes binary digits: byte. SIZE minimum value: Byte. MIN_VALUE maximum value: Byte. MAX_VALUE

Basic Type short binary bits: Short. SIZE minimum value: Short. MIN_VALUE maximum value: Short. MAX_VALUE

Basic int binary digits: Integer. SIZE minimum value: Byte. MIN_VALUE maximum value: Byte. MAX_VALUE

Basic char binary digits: Character. SIZE minimum value: Character. MIN_VALUE maximum value: Character. MAX_VALUE

Basic type double binary digits: Double. SIZE minimum value: Double. MIN_VALUE maximum value: Double. MAX_VALUE

 

Java basic types are stored in the stack, so their access speed is faster than the instance objects of the corresponding packaging class stored in the heap. From Java5.0 (1.5), Java Virtual Machine (VM) can automatically convert the basic types and their corresponding packaging classes. Therefore, when assigning values, passing parameters, and performing mathematical operations, we use their packaging classes like using basic types, but this does not mean that you can call their packaging classes through basic types. In addition, the packaging classes of all basic types (including void) use final modifier, so we cannot inherit from them to extend new classes or override any of their methods.

Advantages of basic types: relatively simple data storage and high computing efficiency

Advantages of the packaging class: some are easy. For example, the element of the set must be of the object type, satisfying the idea that java is an object.

 

3. Data type conversion: automatic and forced conversion (Boolean is not involved in data type conversion). Here only the conversion of basic data types is considered.

Automatic conversion: (small scope ----> large scope)

Automatic type upgrade (Java default int, double type)

Forced conversion: (large scope ----> small scope)

Converted data type variable name = (converted data type) data type to be converted;
For example: double ff = 100.0;
Int mm = (int) ff;

 

Note: forced conversion is not required by the customer and data is lost.

 

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.