开发者

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 m开发者_开发技巧e 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


You need to put an L in there for long-conversion

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜