Java Basic data types

Source: Internet
Author: User

The Java language is a static type (statical typed), which means that all variables and expression types are fully determined when they are recompiled. Because it is a static type, the Java language is also strongly typed (strong typed). Strong typing means that each variable has a type, each expression has one type, and each type is strictly defined, and the type restricts what values the variable can receive and what value the expression ultimately produces. It also limits the types of operations these values can take and how they are manipulated. All assignment operations, whether explicit or passed by parameter in a method call, are checked for type compatibility.

Data type

In the Java source code, each variable must declare a type. There are two types: the base type (primitive type) and the reference type (reference type). The reference type refers to the object (reference to object), and the base type directly contains the value (directly contain value). The base type (primitive types) includes a Boolean type and a numeric type (numeric types). Numeric types are also classified as Integer (integer types) and floating-point (floating-point type). There are 5 types of integers: byte short int long char (char is essentially a special int). Floating-point types have float and double. The relationship is organized like this:

Null is a special type, but you cannot declare a variable to be a null type, and the only value of a null type is NULL. Null can be negative to any reference type or to any reference type. In practice, NULL is generally used as the literal value (literal), which is a literal reference type.

Other reference types are described later and are not covered here.

Basic type

The base type is a single value, not a complex object, and the base type is not object-oriented, primarily out of efficiency considerations, but also provides the basic type of object version, the basic type of wrapper (wrapper). You can use these basic types directly, or you can construct arrays or other custom types using the base type. The base type has a definite range of values and mathematical behavior.

Basic type Size Minimum value Maximum Value Packing class
Boolean -- -- -- Boolean
Char + Bits Unicode 0 Unicode 2-1 Character
Byte 8 bits -128 +127 Byte
Short + Bits -2^15 +2^15-1 Short
Int Bits -2^31 +2^31-1 Integer
Long + Bits -2^63 +2^63-1 Long
Float Bits IEEE754 IEEE754 Float
Double + Bits IEEE754 IEEE754 Double
void -- -- -- Void


Conversions between basic types

It is common to assign a value of one type to another type. In Java, aBoolean type cannot be converted from all other 7 types . Other 7 types of numeric values can be converted to each other, but there may be a loss of precision or some other change. Conversions are divided into automatic conversions and casts. For automatic conversions (implicit), no action is required, and coercion of type conversions requires an explicit conversion, that is, the conversion operator is used.

7 basic types of conversions are summarized as:

You can assign an int to a byte short char int in the process of assigning a value to an integer using literal values, as long as the range is not exceeded. This process is done automatically when the type is converted, but if you try to assign a long to byte, you must also force the type conversion, even if you are not out of range. For example, byte B = 10L; it is wrong to make a cast.

In addition to assignment, some type conversions can occur during expression evaluation. In an expression, the type promotion rule is as follows:

    • All Byte/short/char are promoted to int
    • If there is an operand of long, the entire expression is promoted to long. The same is true for float and double.

Java Basic data types

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.