Mixed-type compution or implicit type conversion

Source: Internet
Author: User

1 Java puzzler 5

 

The negative decimal number can be expressed by a minus sign. However, if the highest bit of the hexadecimal and octal literal constants is set to a negative number.

 

Package arkblue. lang. javapuzzler. n5; </P> <p> public class joyofhex {<br/> Public static void main (string [] ARGs) {<br/> system. out. println (Long. tohexstring (0x00000000l + 0 xcafebabe); <br/>}< br/>}

Print result:

Cafebabe

0 xcafebabe is upgraded to a long integer during addition, that is, 0 xffffffffcafebabe. It is added to 0x000000000000l, and the 32-bit height is changed to zero, leaving the positive integer cafebabe.

Public static void main (string [] ARGs) {byte B = (byte) 0xff; byte b1 = (byte)-1; byte b2 = (byte) 1; system. out. println (B1); //-1system. out. println (integer. tohexstring (B1); // ffffffffsystem. out. println (B); //-1system. out. println (B1 & 0xff); // 255system. out. println (b2 <1); // 2system. out. println (b2 <8); // 256system. out. println (Long. tohexstring (0x00000000l + 0 xcafebabe); // cafebabesystem. out. println (integer. tohexstring (0 xcafebabe); // cafebabesystem. out. println (Long. tohexstring (0 xcafebabe); // ffffffcafebabesystem. out. println (0 xcafebabe); //-889275714system. out. println (long) 0 xcafebabe); //-889275714system. out. println (Long. tohexstring (long) 0 xcafebabe); // ffffffffcafebabesystem. out. println (Long. tohexstring (0x00000000l); // uses the specified Snapshot System. out. println (byte. tostring (byte) 0xff); //-1system. out. println (byte) 0xff + (byte) 1); // 0}

 

2 Java puzzler 6

 

The conversion from byte to Char is not a conversion of extended native types, but a conversion of extended and narrowed native types: byte is converted to int, and then int is converted to Char.

Package arkblue. javapuzzler. n6; </P> <p> public class multucast {<br/> Public static void main (string [] ARGs) {<br/> system. out. println (INT) (char) (byte)-1); <br/>}< br/>

Int type constant-1 is first converted to byte, expressed as 0xff, then the process of converting to Char is to extend the width to int, that is, 0 xffffffffff, in conversion to char type, truncates 2 bytes in height and retains 2 bytes in height. The value is 0 xFFFF, that is, 65535 of the char type. Then, convert the value to the int type. The value is still 65535.

 

(1) convert Char to a wider type int I = C & 0 xFFFF; // No symbol extension occurs

Or int I = C; // No symbol extension occurs.

(2) convert Char to a wider type and expect signed extension. Add comments.

Int I = (short) C; // signed Extension

(3) convert byte to Char. If you do not want symbol extension, all bytes are converted to integers.

Char c = (char) (B & 0xff );

(4) convert byte to Char, with signed Extension

Char c = (char) B;

Package arkblue. javapuzzler. n6; </P> <p> public class multucast {<br/> Public static void main (string [] ARGs) {<br/> system. out. println (INT) (char) (byte)-1); <br/> byte B =-1; <br/> system. out. println (INT) (char) B); // signed extension <br/> system. out. println (INT) (char) (B & 0xff); // unsigned extension <br/>}< br/>

Print results

65535 <br/> 65535 <br/> 255 <br/>

 

3 Java puzzler 8

? : Rule for determining the result type of operators (conditional expressions:

(1) If the second and third operands have the same type, it is the type of the conditional expression.

(2) If the type of an operand is t, t indicates byte, short, or char, And the other operand isConstant expression of int typeThe value can be represented by type T, so the type of the conditional expression is T.

(3) Otherwise, binary numbers will be used for the operand type, and the type of the conditional expression is the type after the second and third operands are upgraded.

 

Package arkblue. javapuzzler. n8; </P> <p> public class dosequals {<br/> Public static void main (string [] ARGs) {<br/> char x = 'X '; <br/> int I = 0; // It is a variable </P> <p> // The second case <br/> system. out. println (true? X: 0); </P> <p> // The third case is not a constant. <br/> system. Out. println (false? I: X); </P> <p> // case 2 <br/> final Int J = 0; // use final to modify J, J can be converted into a constant expression <br/> system. out. println (true? X: J); // The result can be represented by T </P> <p >}< br/>

Execution result:

X <br/> 88 <br/> x <br/>

 

 

System. out. println (0x99); </P> <p> for (int B = byte. min_value; B <byte. max_value; B ++) {<br/> If (B = 0x99) {<br/> system. out. println ("OK"); <br/>}< br/>}

In the preceding operation, B is extended to integer int, [-128,127], while 0x99 is equal to 153. The iteration ends and the conditions cannot be met, so no OK is printed.

 

To (B & 0xff), the iteration range is [-128,127), the iteration comparison value (B & 0xff) ranges from [128,255] and [0,127), so how to print OK.

 

4 composite value assignment expressions automatically convert the results of the calculations they execute into their left-side variables

 

Int I = 123456; <br/> short x = 0; <br/> X + = I; // contains implicit type conversion, however, the result is incorrect. </P> <p> X = x + I; // The result cannot be compiled. "precision may be lost"

Java puzzler 10

(1) The composite value assignment operator requires that both operands belong to the original type, such as int, or the encapsulated original type, such as integer, but there is an exception: if the operand on the left side of the + = Operator is of the string type, it allows the operand on the right side to be of any type. In this case, the operator performs the string join operation.

(2) The simple value assignment operator (=) allows the object reference type on the left, which is much looser: You can use them to represent anything you want to represent, as long as the variable on the right side of the expression is compatible with the value assignment on the left.

 

Package arkblue. javapuzzler. n10; </P> <p> public class comboundassign {<br/> Public static void main (string [] ARGs) {<br/> Object x = "buy "; <br/> string I = "EJ"; </P> <p> X = x + I; <br/> X + = I; // The operator + = is undefined for the argument type (s) object, <br/> // string <br/>}< br/>

 

 

Example: Short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?
Short S1 = 1; S1 = S1 + 1; (the S1 + 1 operation results in int type, which requires forced conversion)
Short S1 = 1; S1 + = 1; (it can be compiled correctly)

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.