Java thoroughly understands byte char short int float long double

Source: Internet
Author: User

We have encountered a lot of questions about the range of numerical types. Here we can make a conclusion that we can understand the range of numerical values that different numerical types can represent in many ways.

Here we only talk about the value type in Java

First, byte:

This section is taken from the source code in JDK byte. Java:

/** <Br/> * a constant holding the minimum value a <code> byte </code> can <br/> * have, -2 <sup> 7 </sup>. <br/> */<br/> Public static final bytes min_value =-128; </P> <p>/** <br/> * a constant holding the maximum value a <code> byte </code> can <br/> * have, 2 <sup> 7 </sup>-1. <br/> */<br/> Public static final byte max_value = 127;

The value range of byte is-128 --- 127;

From the perspective of computer composition principle, we can explain that byte occupies 8 bytes in the computer and the byte is a signed integer. In binary representation, the highest bit is the sign bit. 0 represents a positive number. 1 represents a negative number.

Maximum Value: 127 0111 1111 equals the 7th Power of 2 minus 1;

Minimum value:-128 this number has plagued me for a long time. You need to know that a positive number exists in the original code form in the computer, and a negative number exists in the form of its complement code in the computer, so how is the complement of a negative number calculated? That is, the original code of the absolute value of a negative number is converted into a binary value, and then 1 is added after bitwise inversion,

The following 10 and-10 are used as an example to describe: 10 original code: 0000 1010. Its storage in the computer is 0000 1010. What about-10? According to the preceding calculation, except that the absolute value is 10, it is converted to binary 0000. 1010 is bitwise. 1111. 0101. 1111. 0110. plus 1... this is a-10 complement. OK, in the computer, 1111 0110 represents-10.

Let's take a look at the binary representation of-128 absolute value 128: 1000 0000 bitwise decimal 0111 plus 1: 1111 1000 0000, that is to say,-128 represents 1000 0000 in the computer, let's take a look at-129 in the computer. The absolute value of 129 has exceeded the bytes.

You can also use

System. Out. println (byte. max_value); // maximum value <br/> system. Out. println (byte. min_value); // minimum value

Output the maximum and minimum values of byte.

In summary, the value range of byte can only be-128 -- 127, that is, the value of negative 2 is 7 to the power of 2 minus 1.

The short is used as the 16-bit signed integer, the int is used as the 32-bit signed integer, and the long is used as the 64-bit signed integer.

 

As a 16-bit unsigned integer, char ranges from 0 to the 15th power of 2.

 

Source code from character. Java:

/** <Br/> * the constant value of this field is the smallest value of Type <br/> * <code> char </code>, <code> '/u000000' </code>. <br/> * @ since 1.0.2 <br/> */<br/> Public static final char min_value = '/u0000 '; </P> <p>/** <br/> * the constant value of this field is the largest value of Type <br/> * <code> char </code>, <code> '/uffff' </code>. <br/> * @ since 1.0.2 <br/> */<br/> Public static final char max_value = '/uff ';

Float is a 32-bit floating point:

From float. Java source code:

/** <Br/> * a constant holding the largest positive finite value of Type <br/> * <code> float </code>, (2-2 <sup>-23 </sup>) · 2 <sup> 127 </sup>. <br/> * It is equal to the hexadecimal floating-point literal <br/> * <code> 0x1. fffffep + effecf </code> and also equal to <br/> * <code> float. intbitstofloat (0x7f7fffff) </code>. <br/> */<br/> Public static final float max_value = 3.4028235e + 38f; // 0x1. fffffep + effecf </P> <p>/** <br/> * a constant holding the smallest positive nonzero value of Type <br/> * <code> float </code >, 2 <sup>-149 </sup>. it is equal to the <br/> * hexadecimal floating-point literal <code> 0x0. 000002p-126f </code> <br/> * and also equal to <code> float. intbitstofloat (0x1) </code>. <br/> */<br/> Public static final float min_value = 1.4e-45f; // 0x0. 000002p-126f

 

Double as 64 Float Type

Double. Java source code:

/** <Br/> * a constant holding the largest positive finite value of Type <br/> * <code> double </code>, <br/> * (2-2 <sup>-52 </sup>) · 2 <sup> 1023 </sup>. it is equal to <br/> * The hexadecimal floating-point literal <br/> * <code> 0x1. fffffffffffp + 1023 </code> and also equal to <br/> * <code> double. longbitstodouble (0x7fefffffffffffl) </code>. <br/> */<br/> Public static final double max_value = 1.7976931348623157e + 308; // 0x1. fffffffffffp + 1023 </P> <p>/** <br/> * a constant holding the smallest positive nonzero value of Type <br/> * <code> double </code >, 2 <sup>-1074 </sup>. it is equal to the <br/> * hexadecimal floating-point literal <br/> * <code> 0x0. required bytes must 1p-1022 </code> and also equal to <br/> * <code> double. longbitstodouble (0x1l) </code>. <br/> */<br/> Public static final double min_value = 4.9e-324; // 0x0. 20171000000001p-1022

 

 

Related Article

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.