Java Interview basic problem---encounter pit

Source: Internet
Author: User

As these questions feel that their Java foundation is fake, too many pits

Pit one, about the packing class
Integer num1=127;integet num2=127;System.out.println(num1==num2);

The answer to this question is

But when we put the range to less than 128 and greater than 127, then the answer is different.

Pit two, about Java class Property loading and memory model
package test;public class Demo1 {    int i=getJ();    int j=0;    public int getJ() {        j=10;        System.out.println(i);        return j;    }    public static void main(String[] args) {        Demo1 demo=new Demo1();        System.out.println(demo.j);                 }}

Option A, can run correctly B, an output result C, 0 and d,0 and 0
Look at the answer.

Look at another situation, J, when defined earlier,

Hang Three,
package test;public class Demo1 {    public static void main(String[] args) {        int num=0;        for(int i=0;i<100;i++) {            num=num++;        }        System.out.println("num="+num);    }}

I do not know the first time you have been this problem, many people's answer may be 100 anyway, I am very tangled, see the answer

But let's change the parameters to pick it up and look at the results.

Pit four, about with or not
public static void main(String[] args) {        int n=100;        int m=20;                if(m<=20 && n==n++)        System.out.println(n);        System.out.println(m);        }

Please say the result, this relatively simple, should not have too many people to be pits

When we remove the equals sign

The results are different.

public static void main(String[] args) {        int n=100;        int m=20;                if(m<=20 || n==n++)        System.out.println(n);        System.out.println(m);}

And look at the results.

Change again and look at the results.

There is a pit, is the examination eyes and careful, not pay attention to see will say m=20

Pit Five, about the conversion of the basic type
public static void main(String[] args) {                short s=10;        s=s+2;        System.out.println(s);}

This is a little closer look, the basic type of understanding of the people are easy to see, this is the compiler will error, the output is not the result, here the 2 default is the int type, Short+int automatically converted to int type

Let's take a closer look at this detail, because these variable types we still rarely use, may not be very careful, so write no error.

And one is about byte, look at the picture.

Here is a loop ( -128~~127), which seems to be about a conversion of the complement, the computer composition principle seems to be able to explain, I forgot almost

Java Interview basic problem---encounter pit

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.