The "Java" basic data type and its conversion

Source: Internet
Author: User
Tags wrapper

Sort out the Java basic data types and the knowledge that the interview might involve.

Number of bytes (byte) Number of bits (bit) Range of values
Integral type Byte 1 8 -2^7 ~ 2^7-1
Short 2 16 -2^15 ~ 2^15-1
int* 4 32 -2^31 ~ 2^31-1
Long 8 64 -2^63 ~ 2^63-1
Floating point Type Float 4 32
double* 8 64
Character type Char 2 16 0~2^16-1
Boolean type Boolean 1

The range of values for integral types:

The highest bit is the symbol, plus or minus each 2^ (bit-1) number, 0 is positive, so the positive range is 0~2^ (bit-1)-1, negative is -2^ (bit-1) ~-1

Range of values for floating-point types:

The range of float and double is determined by the number of digits of the exponent. Do not understand this, later review and then look.

Https://www.cnblogs.com/BradMiller/archive/2010/11/25/1887945.html, this is a good piece of writing.

1. Conversions between basic data types

Reference: https://www.cnblogs.com/liujinhong/p/6005714.html

Low Precision →→ Automatic Conversion →→ High Precision
Byte, char, short,int, long, float,double
←← Cast ←←

Because the default integer data in Java uses int, the floating-point type uses double, so the writing specification:

byte a = 1;    Auto Convert byte B = (byte) 128;  Values out of range need to be cast long C = 10000000000L;      Cast float d = 3.5f; Cast double e = 3.5;

When you perform a mathematical operation, the data type is converted to the largest form of data involved

int a = 2;byte b = 2;byte c = (byte) (a+b);d ouble d = 5;char ch = (char) (' a ' + 1);

Char type is automatically promoted to int

char a = 55;char B = ' a '; char C = (char) (a+b); char d = ' a ' + ' a '; System.out.println (a);//7system.out.println (b);//a

2. Wrapper classes for basic data types and their conversions

  basic data type   boolean char byte SHORT  int long float double
Packing class Boolean Character Byte Short Integer Long Float Double

Box packing and unpacking

Integer i = 10;     Boxing base type → wrapper type int n = i; Unboxing wrapper type → basic type

View the corresponding. class file to discover the method that the above code actually calls:

Integer i = integer.valueof (ten); int n = I.intvalue ();

The ① Integer ( pointing to Cache/Chang) and the new integer(pointing to the heap) are different when compared to the memory location.

When ②integer (non-new) is compared to each other, the value is within the Int value range, the result is true, the range is false

③int and integer/new integer comparisons, the result of the comparison must be true, since the integer will first be unboxing and int.

Note: Double comparison is different, all double comparisons are false

Details: https://www.cnblogs.com/dolphin0520/p/3780005.html

The "Java" basic data type and its conversion

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.