Java data Type--face question

Source: Internet
Author: User

1, short s1 = 1; s1 = s1 + 1; what's wrong? short S1 = 1; S1 +=1; what's wrong?

A: For short s1=1;s1=s1+1, the type of the expression that is automatically lifted when the s1+1 operation is int, the type conversion error occurs when you assign an int to a variable of type short S1.

For short s1=1;s1+=1 + + is a Java-language operator, and the Java compiler handles it specially, so it compiles correctly.

2, char type variable can not save a Chinese man, why?

Char type variables are used to store Unicode encoded characters, and the Unicode character set contains Chinese characters, so char types can of course store Chinese characters, and there is a special case where an uncommon word is not contained in

Unicode encoding character set, then the char type cannot store the uncommon word.

3. The difference between integer and int

int is 8 of the built-in raw data types for Java. Java provides a wrapper class for each primitive type, and integer is the encapsulated class of int.

The default value of the int variable is NULL for the default value of the 0,integer variable, which means that the integer can distinguish between unassigned and a value of 0, such as when a student does not come to the exam and another student participates in the test.

Try to answer the wrong, then the first candidate's results should be null, the second candidate's score should be 0 points. This is a very large application of the integer.

Some methods of integer manipulation are provided within the integer class, such as the maximum and minimum values used to represent integers.

4. Can the switch statement function on a byte, can it function on a long string?

Byte has a storage range of less than int and can be implicitly converted to type int, so switch can act on a byte

Long has a storage range greater than int, cannot be implicitly converted to int, can only be cast, so switch does not function on long

String is not available until version 1.7, and after version 1.7, switch can function on string.

5. Can I assign a double value to a variable of type long without casting?

A: No, we cannot assign a double value to a variable of type long without forcing the type conversion, because the range of the double type is wider than the long type, so you must cast it.

6. What will be returned in Java 3*0.1 = = 0.3? True or False?

A: false, because the floating-point number can not be completely accurate expression, generally will lose precision.

7. Float F = 3.4 in Java; Is it correct?

A: Incorrect, 3.4 is a double precision number, the double type (double) assignment to float (float) is a downward transformation (down-casting, also known as narrowing) will result in a loss of precision, so you need to force type conversion float F = (float) 3.4; or write float f = 3.4F; Can.

Java data Type--face question

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.