Java Basic data types

Source: Internet
Author: User

The Java language is a language that emphasizes data types, and when you declare any variable, you must define the variable as a data type .

The data types in Java include basic data types and reference types.

Java programs have a total of 8 basic types, of which 4 are integer, 1 character, 2 float, 1 Boolean, except for these basic types, others are reference types of data.

Basic data type 1, numeric type
    • Integer type (data with no decimal point)
Integral type BYTE size How to declare Representation Range
byte byte type 1 byte byte num = 12 -128~127
Short Quick-integer 2 bytes Short num = 12 -32768~32767
int integral type 4 bytes int num = 12 -2147483648~2147483647
Long integer type 8 bytes Long num = 12L -9223372036854775808 ~9223372036854775807

    • Floating-point types

1, float single-precision floating-point type

    accounted for 4 bytes size , the effective number is 7 bits long, the effective number length includes the integer part and the fractional part.

float num = 255.32f;

After each single-precision floating-point data, there is an iconic symbol "F" or "F", with this flag representing the single-precision floating point data.

2, double dual precision floating point type

Double-precision floating-point data occupies 8 bytes and a valid number is up to 15 bits.

Floating-point data with a system default without an iconic symbol is a double-precision floating-point data.

About the accuracy of float and double data types in Java You can view this article: floating point numbers.

  Attention:

Because the floating-point number is inaccurate, do not use it for comparison . If you do not want to compare two numbers, you can use the following two classes of the Java.math package:BigInteger and BigDecimal. The former realizes the integer operation of arbitrary precision, and the latter realizes the floating-point arithmetic of arbitrary precision.

2. Character type (no decimal point data)

The character data accounts for 2 bytes size.

character variables are represented by single quotation marks .

double quotation marks represent strings , and strings in Java are represented by string literals. String is a class.

char s = ' a '; char s = ' Medium ';


When working with Char, thechar type is used to represent the characters in the Unicode encoding table, with an encoding between 0-65536, represented by the hexadecimal value between ' \u0000 '-' \uffff '. a prefix of u means Unicode.

3. Boolean type

The boolean data occupies one byte . True is True, False is false.

Reference data type

The reference data type is 4 bytes, and the address of the object is stored . In addition to the basic data types, all others are reference data types. For example: Class (Object) interface, array, and so on.

Data type conversion 1, automatic type conversion

Conversion relationships: Byte < short < int < long < float < double.

Low-precision data types can be automatically converted to high-precision data types. In Java, the level of precision can be interpreted as representing the size of the range of data that can be represented, not the size of the number of bytes that the data type occupies .

Therefore, automatic type conversion can also be understood as a small size of the data type can be automatically converted to a large capacity data type, where the capacity refers to the number of can be represented. For example, floating-point numbers can indicate that the number of data is greater than the integer data, so the integer data can be automatically converted to floating-point data.

Automatic type conversion diagram

2. Forced type conversion

Converts from a high-precision data type to a low-precision data type. The type of conversion that is used to display a numeric value, and conversions made with the possibility of losing information are obtained by forcing the type conversion. may result in reduced accuracy or overflow.

int num = (int) 3.14; // num = 3

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.