Java data type learning Notes

Source: Internet
Author: User

Data type Division

There are two types of data:

1: Basic data type

1) integer
Including: byte, short, int, long
2) floating point
Including float and double
3) balanced
Including: char
4) Boolean
Including: boolean

2: Reference data type

1) class
Including: class
2) interface
Including: interface
3) array


Logical type

The boolean type is applicable to logical operations and is generally used for program flow control.

Values of the boolean type can only be true or false. Values of true and false cannot be replaced by integers of 0 or not 0, and must be in lower case.

Usage:

Boolean b1 = false; // assign values directly
Boolean b2 = 3> 4; // assigned by the conditional expression
Boolean b3 = b1; // value assigned by another boolean variable
Boolean b4 = list. ready (); // The value is assigned by the method. If the ready method returns a boolean value
Boolean is a boolean variable in the program.

Instance usage:

Boolean flag = false;
If (! Flag ){
// Do something
}

Character type

Char data is used to represent the "character" in the general sense ".

The character constant is a single string enclosed in single quotes. For example

Char eChar = 'a ';
Char aChar = '中 ';
Char is two bytes in java. Java uses unicode and two bytes (16 bits) to represent a character. Unicode of a Chinese character is 2 bytes.

Note: it is not recommended to store character data of the char type in Java, which is usually stored in string or stringbuffer.


Integer

The integer type includes four middle types: int (integer), long (long integer), short (short integer), and byte (byte type)

They all define an integer, and the only difference is the range of data they can represent.

They all have a fixed table number range and field length, and do not accept the impact of the specific operating system, to ensure the portability of Java programs.

Value range:

Type occupied storage space value range
Byte 1 byte [-2 (7 )~ Any integer between 2 (7)-1]-128 and 127
Short 2 bytes [-2 (15 )~ 2 (15)-1]-32768 ~ Any integer between 32767
Int 4 bytes [-2 (31 )~ Any integer between 2 (31)-1]-231 and 231-1
Long 8 bytes [-2 (63 )~ Any integer between 2 (63)-1]-263 and 263-1
The larger the value range can be expressed, the larger the memory space occupied. Therefore, the most appropriate type should be selected in the program design to define integers.


Floating point type

Similar to the integer type, the Java Floating point type has a fixed table number range and field length, and is not affected by the platform.

The data type of the variable that stores decimals-floating point number. This name is relative to the number of points, which is the decimal point. The decimal point can be changed as needed.

There are two types of floating point numbers in Java: float and double.

Float is a single-precision model that occupies 32-bit memory space, while double is a dual-precision model that occupies 64-bit memory space.

The value contains the decimal point or index, or the letter F or f (float), D or d (double) after the number)

Float can be precise to 7 valid digits, and 8th digits are rounded to the nearest digit;

Double can be precise to 16 valid digits, and 17th digits are rounded to 18th digits.

Double a = 3.4;
Float f = 4.3f; // compilation error without f, loss of precision

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.