Java notes-Pitfalls of extremely error-prone expressions in written examinations

Source: Internet
Author: User

I believe that everyone who has learned Java has been abused by the Java expression, all kinds of "must be it, I can not be wrong!", and then all kinds of "funny, true and false, how can?", although in actual development seldom really let you use those knowledge, But familiar with the expression of the trap to understand the Java data type in memory storage and operation and the principle of the JVM work a lot of help, the most important, the question too can test these things, some pits at that time crawled out, after a few days to do and will not hesitate to jump in, so I tidied up their own do miss some problems, I also collected some small topics of malicious expressions, and put them here, cautionary:

Problem Results necessary to remove pits
System.out.println ( -11%-7.1); -4

A. The first operand provides the symbol for the final result

B. Calculation of the absolute value of two digits provides the final calculation result

int a = 1;
int b = 2;
System.out.println ((a=3) + (b=4) + a*b);

19

The JVM executes this expression on a left-to-right basis, although the priority of the multiplication is higher than the plus and minus, but there is no first multiplication after the addition and subtraction, the problem is simpler.

int a = 10;
int B = 10;
Double c = 10.0;
System.out.println (A==B);
System.out.println (A==C);

True

True

The first true is simple, does not explain

The second true is because:

First, the value of a is int->double and then the value of a and C is compared.

conclusion : = = When comparing the basic data type, it will first be the same type with higher precision, then compare its value

System.out.println (3.0*10==30.0);
System.out.println (3.14*10==31.4);
System.out.println (3.1415*10==31.415);

True

False

False

This problem is not too tangled, just remember,

The computer uses binary to represent decimals, and for some decimals, the binary does not accurately represent

Integer I=1;
Integer j=1;
System.out.println (I==J);

i=127;j=127;

System.out.println (I==J);

i=128;j=128;

System.out.println (I==J);

True

True

False

Java Auto-boxing feature:

The first thing to judge is whether the range of the current value is within the representation range of byte -128~127

If in this scope, the JVM creates an array in memory that has 256 numbers, the creation of values in this range is taken directly from the array, and if it is out of range, it is created by itself, so the first two outputs are true, and the third output is false.

int a = 2;
int b = 1;
int c = a > B? (A = 4): (b = 3);
System.out.println (a);
System.out.println (b);
System.out.println (c);

4

1

4

When a three-mesh operation is made, if the conditional expression confirms which value to take, the other value
Operations will no longer be performed

System.out.println (true? False:true false:true);

False

JVM operation Process: Read to true-> read? (learned that this is a three-mesh operator), read to false-> read ":" (knowing that false is the first data), read ":" After the content (regardless of what is considered to be the second data).

So the form of operation is: True?false: (true?false:true).

int a = 1;
a+= a+= a++;
System.out.println (a);

3

JVM Operation Process: 1.a=a+ (a+=a++)->2.a=1+ (a=a+ (a++))->3.a=1+ (a=1+ (a++))->4. a=1+ (a=1+1)->5.a=1+2->6.a=3.

Short S1 = 1; S1 + = 1;
System.out.println (S1);
Short S2 = 1; S2 =s2+1;
SYSTEM.OUT.PRINTLN (S2);

2

Compile error

for short S1 = 1; S1 + = 1; since + = is an operator in the Java language, the Java compiler will treat it specially, so it can compile correctly

for short s2 = 1; S2 = s2 + 1; Because the type of the expression is automatically promoted by the s2+1 operation, the result is of type int, and when assigned to the short type S2, the compiler will report an error that requires a cast type

First one
int a = 10;
Double d = 9.5;
System.out.println (A > D a:d);
A second
System.out.println (3>2? ') A ': true);
A third
char a = ' a ';
int c = 0;
System.out.println (true? a:0);
System.out.println (false? c:a);

10.0

A

A

97

The data type of the second and third numbers of the trinocular operator is cast if it is inconsistent, here int->double

The first: The return type is 10 of type Double:int to 10.0 of type double

Second: The return type is the Object:char type and the Boolean type cannot be unified, then it is converted to an object and does not throw an exception

The third one:

1: The return type is char:0 and I are both int types, but 0 is constant, and 0 of the constant int can be represented by Char

2: The return type is int:i is a variable, so type uniform for char and int type, and int after unification

I will arrange it in the future.

Java notes-Pitfalls of extremely error-prone expressions in written examinations

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.