Java Eight basic data types

Source: Internet
Author: User

There are eight basic data types in Java

BYTE type:

A byte type that uses one byte to hold one data and one byte for eight bits, so it takes a range of values:

1000 0000 ~ 0111 1111 (-128-127)

why not 0000 0000 ~ 1111 1111?

We all know that 1111 1111 is the maximum value of a negative number, because negative numbers are stored in the computer (denoted by an absolute value of +1) and the highest bit is the sign bit, 1 for negative numbers, and 0 for positive numbers. So 0111 1111 indicates a positive maximum value (converted to decimal 127) and a negative value of 1111 1111 (converted to decimal after the inverse code to 1), the negative value of the minimum is 1000 0000 (converted to 128)

Negative number conversion rule: binary with negative number = negative absolute value the inverse code +1.

Example:-5

-Absolute value of 5:5

5 binary is: 0000 0101

0000 0101 Anti-code: 1111 1010

1111 1010 Plus 1:1111 1011


Short type

The short type, which uses two bytes to hold one data, two bytes and 16 bits binary representation, then its value range is:

1000 0000 0000 0000 ~ 0111 1111 1111 1111 (-32768-32767) conversion with the above byte type. In addition to being aware of the value range when using the short type, it is important to pay attention to the following code:

Short number1,number2,sum; Number1 = 120; Number2 = 300; sum = Number1+number2;

Look at it, you'll find it's no problem, but you know that when the short type is converted to an integer type at the time of the operation, there will be an error, and the Java compiler will give you a chance to try, including other operations. The correct approach should be as follows:

sum = (short) (number1+number2);//(short) for coercion type conversion.

Short is a smallnumber type, but the result of the right side of the calculation is an integer type, so an implicit conversion is not possible.


int type

The int type, which is used in Java, is four bytes to hold a data, altogether 32 is a binary representation, as in the above, the value range: 1000 0000 0000 0000 0000 0000 0000 0000-0111 1111 1111 1111 1111 1111 1111 1111

( -2^32~2^31)


Long type

The long type is the underlying type of Java and uses 8 bytes to store a numeric value, which is a total of 64 binary digits. Value range is ( -2^64-2^63)


Float type

The float type is one of the underlying data types of java. Represents a floating-point number with four bytes. The first bit is the sign bit, the second to the nineth digit represents the exponent, altogether eight bits, of which eight bits have a sign bit of the exponent, so the index value range is (-128-127) and because all 0 and all 1 as special processing, so the final range is (-127-126). Remove the first nine bits, then the remaining 23 bits, then these 23 bits represent the fractional part. Here the 23 bits represent 24 digits, because there is a default leading 1 (this feature is only available in binary). The precision is 7 bits. (The number of specific indices, the number of mantissa, determined by the computer system.) )

The final result is: ( -1) ^ (sign) * 1.F * 2^ (exponent)

Here: sign is the symbol bit, F is the fractional part of 23bit, exponent is the exponential portion, and finally the range is (because positive and negative numbers are symmetrical, only positive values are concerned here)
2^ (-126) ~ 2 (1-2^ (-24)) * 2^127

This is not the value range of float, as the standard also provides for the non-normalized notation, there are some special provisions.

Non-normalized representations:
A non-normalized floating-point number when the exponent part is full 0 and the fractional part is 0, because there is no leading 1, but 0.
The value bit 0.F * 2^ (-126), indicating the range bit 2^ (-149) ~ ~ (1-2^ (-23)) * 2^ (-126) There is no consideration of the symbol. Why is this-126 instead of-127? If it is 127, then the maximum is 2^ (-127) -2^ (-149), obviously 2^ (-127) ~~2^ (-126) can not be expressed.

Other special representations
1. When the exponential and fractional portions are all 0 o'clock, representing 0 values, +0 and 0 (the symbol bit is determined), and 0x00000000 indicates that positive 0,0x80000000 represents negative 0.
2. The index portion is 1, the fractional part is full 0 o'clock, which indicates infinity, positive infinity and negative infinity, 0x7f800000 represents positive infinity, and 0xff800000 represents negative infinity.
3. The index portion is full 1, fractional part of the 0 o'clock, indicating nan, divided into Qnan and Snan,java are both Nan.

You can see that the value range of floating-point numbers is: 2^ (-149) ~ ~ (2-2^ (-23)) *2^127, that is, Float.min_value and Float.max_value.


Double type

The double type occupies 8 bytes, which is a total of 64-bit binary representations. The number of multibyte mantissa accounted for 48 bits, the index multibyte index accounted for 16 bits. The conversion method is the same as the float. However, when using float and double, it is best to analyze the accuracy and performance requirements of the target data first, if you can use float to satisfy the resolute not double, because the double type uses the memory consumption is twice times the float, the operation speed is far inferior to the float.


Char type

Char occupies two bytes in Java, that is, 16 bits of data representing a char type. Since char is unsigned, its representation range is 0-65536. When the calculation exceeds its representation range, the system automatically converts the result to the int type.


Boolean type:

The Boolean type occupies a byte, eight-bit binary representation. The Boolean type has only two values of true and flase.


This article is from the "Love Coffee" blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1585057

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