Java Expression Traps

Source: Internet
Author: User

here are some common questions about interviewing for Java, and I will create a direct form of additional attention.
Question 1:
int a = ten;        int B = A + (A = 5) + A + (A = ten);        System.out.println (b);//b = 30

Question 2:
   int i = 1;        i = (i=i+ (i+ (i=2) +i) +i) +3;        System.out.println (i);//i = 11

Question 3:
int a = 1;        int b = 2;        System.out.println ((A = 3) + (b = 4) + a*b);//19

Question 4:
System.out.println ( -11 -7);//-4     Note: In a modulo operation, the first operand provides the symbol for the resulting value. Absolute values of the first and second operands are modeled as the absolute value of the result
Question 5:
int i = 1;        System.out.println (i+++i+++i+++i++);//10    Note: 1+2+3+4 = 10
Question 6:
  int i = 1;        System.out.println (i++ + ++i+i++ + ++i);//12    Note: 1+3+3+5 = 12
Question 7:
int i = 1;        Int J = i+++ (I=1) +i+++ (i=1) +i++;        System.out.println (j);//j = 5        Note: j = 1+1+1+1+1
Question 8:
int a = ten;        int B = Ten;        Double c = 10.0;        System.out.println (A = = B);//true        System.out.println (A = = c);//true
Question 9:
System.out.println (3.0*10 = = 30.0);//true        System.out.println (3.141*10 = 31.41);//true        System.out.println ( 3.14*10 = = 31.4);//false        Note: 3.14*10 = 31.400000000000002        System.out.println (3.1415*10 = 31.415);//false        Note: 3.1415*10 = 31.415000000000003
question:
Java caches the number of ranges in -128~+127 (Byte range) in a static array, so that only the boxes in this range point to the objects in the array, and the outside of this range requires new        Integer a = n;        Integer b = n;        Integer c = 127;        Integer d = 127;        System.out.println (A = = B);//false        System.out.println (c = = d);//true
Questions:
  byte a = 1;//        byte a = 128//error. Note the range of byte is -128~127        byte B = 1;        A = (byte) (a+1);        b++;        System.out.println (a==b);//true
question:
System.out.println (True | false & False);//true        Note: High precedence over operation or operation
question:
int a = 10;//0000 0000 0000 0000 0000 0000 0000 1010        System.out.println (a<<1);//10*2=20        System.out.println (a<<2);//10*2^2=10*4=40        System.out.println (a<<32);//10        moves left 32 bits (size of int). Return to the original value        System.out.println (a<<32l);//10        will not be transformed. The right side operand only provides left shift number        long B = 10L;        System.out.println (b<<1);//20        System.out.println (b<<2);//40        System.out.println (b<<64 );//10         Long is 8 bytes, 64 bits. So we need to move the 64-bit circle back to the Origin point        System.out.println (b<<64l);//10        byte c = 1;        System.out.println (c<<1);//2        System.out.println (c<<2);//4        System.out.println (c<<8) ;//256        need attention here. C is a byte type, and the minimum operand of a shift operation is an int type. BYTE will be converted        to int System.out.println (C<<32);//1        can treat C as int type

question:
  int a = ten;        A = a<<10;        A = a<<22;        int B = Ten;        b = b<<32;        System.out.println (a);//0        Note: The first step to the left 10-bit, the second move of a is not the original a, but a left 10 bits after a, this time the left 22-bit shifted the non-0-bit outside the boundary, after 0 value to 0        System.out.println (b);//10        return to the original 10
question:
int a = ten;        Double b = 9.5;        System.out.println (A>b a:b);//10.0        result will be converted to double type        System.out.println (a>b 1:false);   2         Although a Boolean cannot be converted to int, it can be converted to an object after it is actively boxed
Questions:
int a = 1;        a+=a+=a++;        System.out.println (a);//3
question:
int a = 1;        A + = 3l;//not error//        a = a+3l;//error, high type low type        System.out.println (a);                int b = 1;        B + = 3.5;            Complex arithmetic expressions, self-motivated high-willed low-        System.out.println (b);//Result 4 replaces 4.5














Java Expression Traps

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.