Java Data Type Understanding

Source: Internet
Author: User

1, the data type is divided into the built-in type and the reference type, and so on the basic concept, no longer elaborated, many blogs above has carries on the introduction.

2, Something about Integer

When you see a number without a decimal point (that is, an integer) in Java (JVM), the value is broken into int by default. If you need to store large enough integers,=> long number = 88888888L;

The q1:byte accounted for 1 Btye = 8 bit, which is the range: 2 7 times the 7 square-1 (1111,1111~0111,1111), namely -128~127?

A1: Integer is a signed number, the highest bit in 0, 1 code to denote the sign bit (0 positive 1 negative), the remaining 7 bits to the decimal with the binary relationship, to hold the absolute value of the number.

Q2: Since all integers are considered int, is it necessary to introduce different data types for the hairs?

A2:例如,byte类型虽然在语义(逻辑)上是占用1字节,但实际上,JVM中是将其当做int看的,也就是事实上是占用了32位,4字节的,所以其运算效率和int没区别,short也一样。

之所以要有byte/short类型,一是因为某些地方要明确使用这些范围类型(个人理解为给人看的,而非给机器声明,因为机器为默认分配4 byte.)。二是,在byte[]数组中,JVM存储的则是真的1字节,short[]2字节。(但也有的JVM其byte[]数组也是4字节1位)

Q3: Decimal, octal, hexadecimal integer difference?

A3: For the same number (10), the Three (decimal: 10, 16 binary: A, octal: 12) In memory are 0, 1 code, but according to their corresponding rules of the system, the effect of the show is different. The default int a= 10, which is the input output in decimal format. You can also output octal int a = 012 (preceded by 0 for octal format), hexadecimal int b =0xa (corresponds to a number preceded by 0x or 0X, which represents hexadecimal format).

Q4:

char b = ' a ' + 18;  

char a = ' a ';  Char B = A + 18;    

  

The former statement does not error, the latter statement error: Wrong: incompatible type. For Mao?

A4: About the first sentence character constant ' a ' using Unicode, the actual storage is converted to an integer specific value to represent the character, the integer can be calculated between the results of the operation is converted to char corresponding character output. (Logically, it can be understood)

The first sentence, the other understanding is: Javac at the time of compiling the char B = ' a ' + 18 compiled into char b = ' s '. (P.S. Two ways of understanding, I have not learned the assembly, the specific machine implementation process is not clear, the second more accurate and deeper. )

About the second sentence: A is a variable, the JVM does not know what the specific a is, so when the arithmetic operation, it will automatically convert the Char type ' a ' to the int type of 97, and then the arithmetic operation, after the strong conversion to char.

If you need to resolve the second inning error, make a forced type conversion.

char a = ' a '; char b = (char) (A+18);

3, specific examples show

1)    long number = 0xfffffffffff;//0x表示这个数是16进制数,0表示8进制。    //编译器报错,因为右边默认是int,但其超出了范围(没超出int范围的话    //编译器会隐式将int转为long),故报错(同样的错误也会出现在float)。A claim type is a byte that gets the corresponding type in memory to store the number of inputs. To resolve the problem, add l to the right end of the equation. The explicit prompt is a long type. 2)short s = 123;//(这个123也是int类型,这里,= 操作编译器能隐式转换)    s = s + 123;//编译器报错,那是因为s+123是int类型(编译器先将s转化为int,再+123),+ 运算符等算数运算符,遇到不同类型的会自动转换。    //这里,+ 操作编译器不能隐式转换(会提示失真,即精度可能会受损)。正确的做法:    s = (short)(s + 123)//注意,不是(short)s + 123。

Java Data Type Understanding

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.