The mystery of expression

Source: Internet
Author: User

All of the following are from the Java FAQ

1, the odd sex

This puzzle is mainly about reminding us how to judge whether a number is odd or not.

Like what:

I% 2 = = 1

Can this be judged successfully?

What if I is a negative number? It's obviously going to be-1.

So I prefer: I% 2! = 0

There's another one that looks higher: i&1! = 0

2. Change Time

This puzzle is currency calculation, because the floating point number in the computer does not accurately save, so, sometimes there is an error, so the problem arises

such as: System.out.println (2.00-1.10);

The output is not 0.90 of what you think, but an approximate value of 0.8999999999999999

It is best to use the BigDecimal data type when working with these very accurate data.

The BigDecimal type defines the Add,subtract,multiply,divide method for accurate four calculations.

As in the above example:

System.out.println (BigDecimal ("2.00"). Subtract (BigDecimal ("1.10 ')));

That's OK.

Tip: To avoid the use of float, double, for currency, use Int,long,bigdecimal in the exact place you want.

3, Long divide

This puzzle is about a mixed calculation of long and int.

such as: long tmp = 24*23*34*4*5000000;

and long tmp= 1L * 24*23*34*4*5000000;

Do the two expressions have the same TMP? Believe you know it!

Tip: When working with big data, be sure to watch out for spills!

4. Primary issues

System.out.println (12345+5432L);

equals how much? Is 66666?

Note the difference between L and 1, in fact, this question is in the tip of us, when we want to express the long integer to use L, rather than l

5, Hex of interesting

System.out.println (long.tohexstring (0x100000000l + 0xcafebabe))

How much will it output? Cafebabe

Why is it?

Decimal can clearly represent a negative number, and for octal or hexadecimal, negative representation is not so clear, such as the above 0xcafebabe is positive or negative?

We need to know how to represent positive negative numbers when memory is stored: if the highest bits of hexadecimal and octal literal constants are set, they represent negative numbers. So 0xcafebabe is a negative number.

Second, we need to know: the symbol extension, because the first operand is a long type, so int should be extended, and because the int is signed, so to have a signed extension, that is negative, the preceding expansion bit is 1

Therefore, the following calculation is available:

0xffffffffcafebabe

+ 0x0000000100000000

= 0x00000000cafebabe

Tips:

1. For hexadecimal or octal operations, be aware of the sign bit

2, the extension of the type of time note is not a symbolic extension

3, when the operation, it is best to avoid two different types of data to operate

6. Multiple transformations

SYSTEM.OUT.PRINTLN ((int) (char) (byte)-1);

Output what? 65535

Int->byte: The conversion of the time to intercept the eighth position, is still-1

Byte->char: They cannot go directly because Byte is signed and char is unsigned and can be converted into two actions, Byte->int->char,byte->int, signed extensions ( -1), Int->char, The direct intercept of the lower 16 bits is 0xFFFF (0xFFFFFFFF), or 65535.

Char->int: Directly 0, or the original value. is still 65535.

Tip: If the initial value is signed, then the symbol extension is executed, and if it is char, the 0 extension is performed regardless of what type it will be converted to.

7. Interchange content

int x = 1984;//0x7c0

int y = 2001;//0x7d1

X^=y^=x^=y;

System.out.println ("x=" +x+ "; y=" +y);

How much does it print? x=0;y=1984

This is to tell us that you can no longer assign a value of two times to the same object in an individual expression.

8. Dos Equis

As follows:

int i = 0;

char x = ' x ';

System.out.println (true?x:0);

System.out.println (FALSE?I:X);

Print What? X88

It is important to note that I and X are different types, and that it is common to use the same type of data, so there is nothing like this.

See three rules:

(a) If the type of the two operands is the same, the type of the expression is determined,

(b) If the type of an operand is t,t can be short,char,byte, and the other operand is an int constant, then the final type is T

(c) If the type of the two operands is different, the type extension is required, and the final expression type is the extension type.

So the above two output statements, the first type is char, and the second type is int, so print the X88

Tip: It's a good idea to use two of the same operands in an expression statement, and of course we did it unintentionally.

9, half a catty

Give the declaration of the variable, make the x+=i legal, and make the x=x+i illegal;

This is the difference between a compound assignment and a simple assignment.

The Java language Specification describes compound assignment E1 op= E2 equivalent to Simple assignment e1= (t) (E1 op E2), where T is the type of E1.

Once you've seen it, you'll find that the compound assignment is not just a simple operation, but also an automatic type conversion.

Therefore the declaration is short x = 0;int i=123456; can make the x+=i legal, and x=x+i illegal

Tip: Do not effect the compound assignment operator on the Byte,short,char type variable.

10, 82

In contrast to Puzzle 9, give a statement. Makes x = x + i legal, and makes x+=i illegal

Compound assignment operator: Both operands are required to be basic data types;

Simple evaluators: Allow the left side to be an object reference, so you can use them to represent anything you want to represent, as long as the right side of the expression is assignment-compatible with the variable on the left.

Object x= "SDJHG";

String i= "Sadgag";

Such a declaration can make x=x+i legal, and make x+=i illegal.


In this chapter, we are looking at some small and magical places, in fact, we have been avoiding, almost do not use such expressions, such as conditional expressions, Dora such assignments.



The mystery of expression

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.