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

后端 未结 3 691
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 13:56

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

相关标签:
3条回答
  • 2020-12-11 14:37

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

    long micro=24*60*60*1000*1000L
    
    0 讨论(0)
  • 2020-12-11 14:50

    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 讨论(0)
  • 2020-12-11 14:58

    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

    0 讨论(0)
提交回复
热议问题