Home > Java > javaTutorial > Java's eight basic data types

Java's eight basic data types

(*-*)浩
Release: 2019-06-06 14:02:55
Original
5087 people have browsed it

Data types in Java are divided into reference types and basic data types. Basic types are divided into 8 types. Today I will introduce these 8 basic data types to you:

Java's eight basic data types

1. Integer type

Integer types include byte, short, int, and long, all of which are signed (complement) integers (that is, they can represent negative numbers).

Integer literals (127-128) default to int type, if Without exceeding the scope of the declared type, you can directly assign a small type (you don’t need to memorize it, you will gradually understand it in the process of writing the program). (Recommended learning: Java Video Tutorial)

When using integer types, pay attention to the value range. Integer literals between (-128~127) can be directly assigned to the byte type, and Java will The sign bit is automatically processed. Similarly, the short type is the same (rarely used).

Java underlying byte and short are calculated as 32 bits (note that the int type range is also 32 bits).

Note: Long type literals need to use "L", "l" suffixes, otherwise the compilation will not pass. Note that as shown below, because the default literal of an integer value is of type int, 12123123123 is obviously beyond the range of int, so a compilation error will occur. However, if the suffix "L" is added after it, it means that the number is of type long.

2. Floating point type

A data type used to represent decimals. Principles of floating point numbers: binary scientific notation.

Scientific notation for decimal floating point numbers: 219345=2.19345*(10^5)

Scientific notation for binary floating point numbers: 10111=1.0111*(2^100)

Take 2.19345* (10^5) as an example to introduce the following concepts:

Mantissa: .19345

Exponent: 5

Base: 10

The float type has a total of 32 bits (same as int), of which 1 bit is the sign bit, the exponent is 8 bits, and the mantissa is 23 bits. It should be emphasized that the precision of float is 23 digits (that is, it can accurately express 23 digits, and it will be truncated if it exceeds).

The small tree uses the length of the mantissa to express the accuracy. For example, pi=3.14, its accuracy is 2 digits, and pi=3.1415, its accuracy is 4 digits.

What is interesting is that the precision of int is greater than that of float, because the precision of int is 31 bits, which is greater than float.

Because the float type has low precision, we generally use the double type more often.

The double type can represent 64 bits, including 1 sign bit, 11 bits of exponent, and 52 bits of mantissa (you don’t need to remember the storage format, it is enough to know that generally decimals are represented by double).

The precision of double is more accurate than int, and the range it can represent is larger than float, but not as good as long.

It should be noted that the literal value of floating point number is double by default.

3. Character type char

The character type is a 16-bit unsigned integer, which is a binary number. This value is the Unicode encoding value of a character.

What is the encoding? We cannot write words in a computer and can only use 0 and 1 to represent numbers. So we have made artificial regulations. In addition to representing a number, a certain number can also be expressed as a character. The character represented by a decimal number 65 is the capital letter A

. All of this is to display and output according to human habits as much as possible. Inside the computer, 0 and 1 are always stored and operated.

The char type is an unsigned 16-bit integer. The minimum value is 0 and the maximum value is 65535=2^16-1. When assigning a character in the program, use single quotes for character literals, which can be char Assigned values ​​include characters, numbers, and symbols.

It should be noted that not all characters are visible, such as /u0000, which is generally used as the end character of a string in C, not "0", as shown below, the small box displayed on the console represents Characters are not visible.

4.boolean Boolean type

Expression: true (true)/false (false).

Generally used in judgment statement blocks:

public class demo{
    boolean b=false;
    if(b==true){
        System.out.println("回家睡觉");
    }else{
        System.out.println("熬夜加班");
    }
}
Copy after login

For more Java-related technical articles, please visit the Java Development Tutorial column to learn!

The above is the detailed content of Java's eight basic data types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template