Look Back, Java restudying (c): The Java basic data type of the integral type

Source: Internet
Author: User

Note: Because this study intends to read the relevant Java source code, the update speed will be relatively slow at the beginning. There is have to admire that group of Daniel Ah, algorithm is not 1:30 will be able to understand, was intended to directly understand, and then feel so inefficient, so decided to read it in general, after all, will definitely come back to learn again.

Java, as a strongly typed language, means that each variable will have a specific type, there are 8 basic types of Java, there are 4 types of integers (byte, short, int, long), two floating-point types (float, double), 1 character types (char) and a Boolean (Boolean), it is worth noting that although string strings are very common, it is not a basic type.

  First, the integral type

 The integer represents a number that has no decimal part, allows negative numbers (there is no unsigned type in Java, that is, unsigned), the smallest unit of storage information in a computer is a bit (bit), the basic unit of measurement for storage capacity is byte (byte), and one byte occupies 8 bits. The range of integer data is calculated based on the number of bits it stores, that is, 2 of the N-square, such as Byte type 1 bytes, 8 bits, so there are 256 values, negative numbers include -128~-1, positive numbers include 0~127. In the same vein, the storage requirements and range of values for integer data are as follows:

Type Storage requirements (bytes) Range of values
Byte 1 -128 ~ 127
Short 2 -32768 ~ 32767
Int 4 Approximately equals-2.1 billion ~ 2.1 billion
Long 8 Not likely to overflow in normal use, with a range of ~ 9.2 billion ~ 9.2 billion

In addition, in the process of processing integer data may be used in various types of writing, the following list of several commonly used:

Writing Meaning Note
12345 Decimal Same as written
12345L Decimal, Long Integer If the value is greater than the maximum value of the int type data, the error is not added.
012345 Octal Not recommended, easy to confuse
0x12345 Hexadecimal Alternative representations of binary data for easy writing and memory
0b10010101 Binary Java 7 new Add-on notation, previous versions not supported
1_000_000 Literal segmentation As written in 1,000,000, Java 7 is newly added, and the compiler automatically ignores the underscore

Here are the questions and answers about the source code in general:

What's the difference between 1.integer.parseint and integer.valueof?

parseint returns int data, and valueof returns an integer wrapper type, which can call the API in integer for other operations.

What is 2.integer.getinteger for?

In the daily development of less contact, used to obtain the system parameters, the source calls System.getproperty (String key), and then use decode to parse

What's the difference between 3.integer.decode and integer.valueof?

The former can parse 0x, #等表示的其他进制, while the latter only converts decimal

It is also important to note that the CompareTo of Byte and short is the direct calculation of the difference, that is, a-B, while the CompareTo of Integer and Long is the return-1, 0, and 1;byte, short, integer hashcode () To return the int value of the current value, and the long type is returned (int) (value ^ (value >>> 32));

Look Back, Java restudying (c): The Java basic data type of the integral type

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.