Java Data types

Source: Internet
Author: User
Tags naming convention

Java is a strongly typed programming language that shows that all the variables used in a Java program must be well-defined data types.

Java data types can be categorized into basic data types and reference data types.

Basic data type:

Numeric type: integer type (byte, short, int, long), floating-point type (float, double)

Character type: Char

Boolean Type: Boolean

Reference data type:

Classes (Class), arrays, interfaces (interface)

The difference between the basic data type and the reference data type is that the basic types of variables and object handles are stored in the stack memory, occupy a fixed amount of space, and can access their values directly through variable names; All Java objects are stored in heap memory, and heap memory is a runtime data area that occupies any size space. It is necessary to directly access its value through an object reference stored in the stack memory.

This first introduces the basic data types.

There are 4 classes and 8 basic data types defined in the Java language.

Integer type: Byte, short, int, long

Float type: float, double

Character type: Char

Boolean Type: Boolean

basic data types for the Java language
Data type Amount of memory space (bytes) Range of values Default value
Byte 1 -128~127 0
Short 2 -32768~32767 0
Int 4 -2147483648~21474881647 0
Long 8 -9223372036854775808~9223372036854775807 0L
Float 4 1.4e-45~3.4028235e38 0.0f
Double 8 4.9e-324~1.7976931348623157e308 0.0d
Char 2 ' \u0000 ' ~ ' \uffff ' ' \u0000 '
Boolean 1 True,flase Flase

In the Java language, the base data type occupies a fixed memory length, independent of the specific hardware and software platform environment.

Constant

A constant is the amount of value that cannot be changed during the execution of a program. Declare constants to be final with the keyword.

Description

(1) The naming convention for constants is that each letter is capitalized.

(2) If you want to re-assign a value to a constant during execution, the compilation will go wrong.

In the Java language, constants are generally divided into integer constants, floating-point constants, Boolean constants, character constants, and string constants.

1. Integral type Constants

In the Java language, integer constants can be divided into the following three types:

(1) Decimal integer: A decimal integer constant is a sequence of numbers consisting of 0~9, and the first digit of the sequence cannot be 0 (except for a single 0).

(2) Octal integer: The first digit of an octal integer constant is 0, followed by a sequence of numbers consisting of 0~7.

(3) Hexadecimal integer: The hexadecimal integer constant begins with "0x" or "0X" followed by a sequence of hexadecimal digits. The hexadecimal sequence of numbers is made up of digital 0~9 and letter a~f.

2. Floating-point constants

Floating-point constants are numeric constants that can contain fractional parts. Floating-point constants can be divided into single-precision floating-point constants and double-precision floating-point constants, depending on the length of memory consumed. A single-precision constant occupies 4 character sections, followed by an F or F after its number, and a double-precision constant of 8 bytes, followed by D or D in its number. where D or D after double constants can be omitted, if there is no letter after a floating-point constant, the default is a double-precision floating-point constant.

Floating-point constants can only be represented by decimal notation, with traditional counting and scientific notation.

(1) Traditional counting method: It consists of integral parts, decimal point and fractional part.

(2) Scientific counting method: When a number is large or very small, use scientific notation. Example: 1.23e3

3. Boolean Constants

A Boolean constant has only two values: True and False.

4. Character constants

Character constants are single characters enclosed in a pair of single quotes. Like ' Q ', character data in Java is a 16-bit unsigned data type, using the Unicode character set.

5. String constants

A string constant is a sequence of 0 or more characters enclosed in double quotation marks, which can contain escape characters in a string constant.

Variable

A variable is the amount of value that can be changed during the execution of a program. Variables play a very important role in the program. Can be divided into: integer variable, floating-point variable, character type variable and Boolean variable.

Conversions between basic data types

Data type conversions are constants or variables that are converted from one data type to another. In Java, conversions for basic data types mainly include two cases: automatic conversion and coercion of type conversions.

  1. Automatic conversion

Automatic conversion refers to the system's automatic conversion of data types, from low-precision data to high-precision data conversion. The relationship is as follows;

Byte-->short-->int-->long-->float-->double

Char-->int

Note: When the data is large, there may be a loss of precision when the int and long types are converted to float type and long to double type.

  2. Forcing type conversions

Forcing a type conversion is a mandatory conversion of the type of data, from high-precision to low-precision conversions that require the coercion type conversion character (type). The syntax is as follows:

(target type) expression;

For example: float f = 88.88f;

int x = (int) F; Casts the float type to the int type.

Java 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.