Basic data types

Source: Internet
Author: User
Tags ranges


The basic data types in Java are divided into four classes, integer types, floating-point types, character types, and Boolean types.

1), the integer type contains a byte (a byte), short (two bytes), an int (four bytes), and a long (8 bytes).

2), floating-point type contains a float (four bytes), double (eight bytes).

3), the character type contains a char (two bytes).

4), the Boolean type contains a Boolean (typically eight-bit bytes).


1, Plastic

Commonly said integral type, generally contains the following 4 types, byte (one byte), short (two bytes), int (four bytes), Long (8 bytes).

int is the most commonly used integer type, here to pay attention to the value range of int, -2147483648->2147483647, in order to facilitate memory, can be easily remembered later: Int is a 10-digit number beginning with 2 .

Typically, the default value of an integer value is the int type. In addition, as in the following two situations, it must be noted that:

1), If you assign a small integer value (within the representation range of byte and short) to a byte and short variable, the system automatically treats the integer as a byte or short type .

2), Java does not automatically treat this integer value as a long if it uses a large integer (which exceeds the representation range of int) .

If you want the system to treat an integer value as long, you should add L or L as a suffix after the integer value, and L is recommended.

Note: You can assign a small integer value (within the range of the int type) directly to a long variable, not because Java treats the smaller integer value as a long type.

Java still treats this integer value as an int type, just because the value of the int type is automatically converted to the long type.

public static void Main (string[] args) {//The following statement is correct, 5 within the range of byte type, the system automatically treats 5 as a byte type. byte A = 5;//The following statement error, 12345678912 is outside the range of the type (2 begins with 10 digits). int B = 12345678912;//The following statement error, the system does not automatically treat 12345678912 as a long type to handle long c = 12345678912;//The following statement is correct, long type to add L after the number. Long d = 12345678912l;//The following 2 lines of statements are correct, and the int type in Java automatically goes to a long type int e = 123;long f = e;}


2, character type

character types are typically used to represent individual characters, and character values must be enclosed in single quotes .

Java uses a 16-bit Unicode character set as the encoding, and Unicode is designed to support characters in all the world's written languages, including Chinese character, so Java programs support characters in a variety of languages.

There are three representations of character values:

1), the character type value is specified directly by a single character, such as ' A ', ' 9 ', ' 0 ' and so on.

2), a special character type value is represented by an escape character, such as ' \ n ', ' \ t ', and so on.

3), using Unicode values directly to represent the character value, the format is ' \uxxxx ', where xxxx represents a hexadecimal integer.


Because the computer's underlying character is saved, the character's corresponding number is actually saved, so the value of the char type can also be used directly as an integer value, which is equivalent to a 16-bit unsigned integer, ranging from 0->65535.

If an int integer in the 0->65535 range is assigned to a char type variable, the int integer is automatically treated as a char type.

public static void Main (string[] args) {char charvalue = ' jiang ';//The following code outputs Jiang System.out.println (charvalue); int intvalue = Charva lue;//The following code output 33931SYSTEM.OUT.PRINTLN (intvalue);}

instead of providing a basic data type that represents a string, Java represents a string by using a string class, because the string consists of more than one character, so the string is enclosed in double brackets.。

There are two points to note:

1), the char type is enclosed in single quotation marks, and the string is enclosed in double brackets. I'll make a special arrangement on the use of string.

2), single quotes In the Java language, double quotes and backslashes have special uses, and if a string contains these special characters, you should use the representation of the escape character.

For example, write an absolute path in Java: "C:\codes" is not the desired result, because Java will take the backslash as an escape character , so it should be written as "C:\\codes".


3, floating-point type

There are two kinds of floating-point types in Java, float and double. The floating-point type of Java has a fixed number of table ranges and field lengths, and the field length and number of table ranges are machine independent.

Floating-point types contain a 32-bit memory space of float (four bytes), and double (eight bytes) occupies 64 bits of memory space.

There are two representations of floating-point numbers in the Java language.

1), in decimal form: This form is a simple floating-point number, such as 3.1415. A floating-point number must contain a small number, or it will be treated as an int type.

2), scientific notation form: such as 3.14e2/3.14e2, that is, 3.14*10*10, forgive me Mac does not hit the index.

Note: only numeric values of floating-point types can be represented by scientific notation. For example, 31400 is a value of type int, and 314E2 is a value of floating-point type .


The Java language's floating-point type defaults to a double type, and you can add D or D after a floating-point number to force the specified variable to be a double type.

If you want Java to treat a floating-point type value as a float type, you should immediately follow the value of F or F after this floating-point type.


Java also provides three special floating-point values, positive infinity, negative infinity, and non-number, used to indicate overflow and error.

1), positive infinity is represented by the positive_infinity of double and float classes, such as dividing a positive number by 0 to get positive infinity

2), negative infinity is represented by the negative_infinity of double and float classes, such as using a negative number divided by 0 to get a negative infinity

3), the non-number is represented by the Nan of the double and float classes, 0 divided by 0, or a negative number is given to a non-numeric

There are 2 points to note:

1), all positive infinity values are equal, all negative infinity values are equal, and Nan is not equal to any numeric value, even to Nan.

2), only the floating-point number divided by 0 can get positive infinity or negative infinity . The Java language automatically treats 0 of the floating-point arithmetic as 0.0.

If an integer value is divided by 0, an exception is thrown, Arithmeticexception:/by zero (divided by 0 exceptions).

The following code is the JDK in the Double class infinity, negative infinity, non-number source:

Public final class Double extends number implements comparable<double> {    /**     * A constant Holding the Positi ve infinity of type     * {@code double}. It is equal to the value returned by     * {@code double.longbitstodouble (0x7ff0000000000000l)}.     */public    static final double positive_infinity = 1.0/0.0;    /**     * A constant holding the negative infinity of type     * {@code double}. It is equal to the value returned by     * {@code double.longbitstodouble (0xfff0000000000000l)}.     */public    static final double negative_infinity = -1.0/0.0;    /**     * A constant holding a not-a-number (NaN) value of type     * {@code double}. It is equivalent to the value returned by     * {@code double.longbitstodouble (0x7ff8000000000000l)}.     */public    static final double NaN = 0.0d/0.0;}

Now we output a positive infinity, negative infinity, non-number, look at the console display.

public static void Main (string[] args) {System.out.println ("positive infinity:" + double.positive_infinity); SYSTEM.OUT.PRINTLN ("Negative infinity:" + double.negative_infinity); SYSTEM.OUT.PRINTLN ("Non-number:" + Double.NaN);}
The console output is as follows:

Positive Infinity: Infinity negative Infinity:-infinity non-number: NaN


4, type bool

The Boolean type has only one Boolean, which is used to represent logical true or FALSE.

In Java, a Boolean value can only be true or false, cannot be represented by 0 or nonzero, and the values of other base data types cannot be converted to a Boolean type.

boolean-type values or variables are primarily used to flag for process Control , and the process of using Boolean variables or values in the Java language is dominated by the following types of processes:

1), if condition control statement

2), while loop control statement

3), Do loop control statement

4), for loop control statement

5), (? :) Three mesh operator


5, the values are separated by an underscore

It is not easy to see how many digits the variable has in the program when the number of digits in the application is particularly long.

To solve this problem, Java7 introduces a new feature that allows the programmer to use underscores in numeric values, whether they are integer values or floating-point values, and are free to use underscores.

by underlining, it is more intuitive to tell how many bits are contained in a number, and it is recommended that 3 digits be underlined , such as 1_234_567_890.

public static void Main (string[] args) {int a = 123_456_789; SYSTEM.OUT.PRINTLN ("Use underline to open int value:" + a); long B = 1_234_567_890_987_654_321l; SYSTEM.OUT.PRINTLN ("Use underscore to open Long value:" + b);}



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.