简体   繁体   中英

Java problem-Whats the reason behind and what will be probable output

1.)

long milli=24*60*60*1000;
long micro=24*60*60*1000*1000;
long result=micro/milli;

The result should be 1000 but it's not.

Please can you tell me the output and explain it?

2)

int i=0;
for(a=0;a<=integer.MAX_VAL;a++)
{
    i++;
}

S.O.P(i);

Normally it went to infine loop why because there is max value it should come out of loop. At what conditions it will executed sucessfully and what will be excepted value. .....Anyone can tell me about VM... handing of nummbers in JAVA

您需要在其中放置一个L以进行长时间转换

long micro=24*60*60*1000*1000L

2)

public class test {
    public static void main(String[] ar){
        int i=0;
        for(int a=0; a< Integer.MAX_VALUE;a++) {
            i++;
        }
        System.out.println(i);
    }
}

output:

2147483647

This feels jeopardy, having to guess the question as well as the answer. ;)

I think the second question should read

int i=0;
for(a=0;a<=Integer.MAX_VALUE;a++)
    i++

This will go into an infinite loop because all possible values of a are <= MAX_VALUE.

You can re-write this loop as

int a=0;
do {
    i++
} while (a++ != Integer.MAX_VALUE);

i will be Integer.MIN_VALUE as it overflows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM