public static void main(String[] args){
long l = 1000*1000*1000*20;
int j = 109613828;
if (l > j) 
System.out.println("hello"
);
else
System.out.println("nonooo");
}
}   无显示结果,因为long  和int 类型无法比较吗  ?

解决方案 »

  1.   

    把他全部放在一个类里试一下:
    class Test {
    public static void main(String[] args){
    long l = 1000*1000*1000*20;
    int j = 109613828;
    if (l > j)  
       System.out.println("hello");
    else
       System.out.println("nonooo");
    }
    }
    将上述代码保存为Test.java
      

  2.   


    运行程序,执行的是else语句。调试发现果然是l=-1474836480。
    long的范围不是–9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807吗?
    这是怎么回事
      

  3.   

    class Test{
    final int i = 9;
    static int j ;
    public static void main(String[] args){
    long l = 1000000L;
    int j = 1096;
    if (l > (long)j) 
    System.out.println("hello"
    );
    else
    System.out.println("nonooo");
    }
    }
     改成这样还不行,显示如下错误
          An unexpected error has been detected by Java Runtime Environment:
    #
    #  Internal Error (classFileParser.cpp:2924), pid=2828, tid=3120
    #  Error: ShouldNotReachHere()
    #
    # Java VM: Java HotSpot(TM) Client VM (10.0-b22 mixed mode windows-x86)
    # An error report file with more information is saved as:
    # F:\Documents and Settings\Administrator\workspace\mobtorrent\hs_err_pid2828.log
    #
    # If you would like to submit a bug report, please visit:
    #   http://java.sun.com/webapps/bugreport/crash.jsp
    #
      

  4.   

    public class Test {
    final int i = 9;
    static int j ;
    public static void main(String[] args){
    long l = 1000000L;
    int j = 1096;
    if (l > j) 
    System.out.println("hello");
    else
    System.out.println("nonooo");
    }
    }这样是可以的,你为什吗要用强制转化啊,这个会自动转换
      

  5.   


    public class Test {
    final int i = 9;
    static int j ;
    public static void main(String[] args){
    long l = 1000000L;
    int j = 1096;
    if (l > (long)j) 
    System.out.println("hello");
    else
    System.out.println("nonooo");
    }
    }按你的方法测试也没有问题,
      

  6.   

    //我测试可以运行
    class Test{
    final int i = 9;
    static int j ;
    public static void main(String[] args){
    long l = 1000*1000*1000*20L;
    int j = 1096;
    if (l > (long)j) {
    System.out.println("hello");
    }
    else{
    System.out.println("nonooo");
    }

    }
    }
      

  7.   


    public class Test {
    final int i = 9;
    static int j ;
    public static void main(String[] args){
    long l = 1000*1000*1000*20L;
    int j = 109613828;
    if (l > (long)j) 
    System.out.println("hello");
    else
    System.out.println("nonooo");
    }
    }这个测试也没有问题
      

  8.   

    可以运行啊。。
    为什么不行?public class TestComparableNum {
    public static void main(String[] args) {
    long l = 1000 * 1000 * 1000 * 20;
    int j = 109613828;
    if (l > j) 
    System.out.println("hello");
    else
    System.out.println("nonooo");
    }
    }这是楼主的代码 
    可以运行的。。
    i和j比较的时候为什么要用强制转换啊?
    是把int转化为long
    不用自己强制转换。不知道你们是怎么测试的
      

  9.   


    long l = 1000 * 1000 * 1000 * 20;这句代码右边没有写L的话 是int类型的 这个时候超出的范围了
    所以就截断了 成负数了
    这样写就打印hello了
    long l = 1000 * 1000 * 1000 * 20L;
      

  10.   

    楼上几位说的对,LZ只要在声明为long型的值后面加上L,就行了。
    在比较l>j时,java会自动进行类型提升,不用强制转换。
      

  11.   

    public class T{
    public static void main(String[] args){
    long l = 1000*1000*1000*20;
    int j = 109613828;
    if (l > j)  
    System.out.println("hello");
    else
    System.out.println("nonooo");
           }
    }
    执行结果是输出了nonooo,可能是因为变量l溢出!
      

  12.   

    i的值太大,溢出。int:有符号 32 位整数
      

  13.   

    public class Test {
    public static void main(String[] args){
    long l = 1000L*1000*1000*20;
    int j = 109613828;
    if (l > j)
    System.out.println("hello");
    else
    System.out.println("nonooo");
    }
    }
      

  14.   

    long l = 1000 * 1000 * 1000 * 20;
    1000 * 1000 * 1000 * 20  结果先存在int ,然后再转的long
    所以你的值会变成负的
      

  15.   

    数值的默认是int类型,所以1000 * 1000 * 1000 * 20的结果也是int类型。结果溢出了,所以高位被去掉了把,使得最高位是1把,就成为了负数。
      

  16.   

    public class Java04(这个你自己改成自己java文件的名字)
    {
    public static void main(String[] args)
    {
    long l = 1000*1000*1000*20;
    int j = 109613828;
                    System.out.println(l);
                    System.out.println(Long.MAX_VALUE);
    System.out.println(Long.MAX_VALUE-l);
    if (l > j)
    {
    System.out.println("hello");
    }
    else 
    System.out.println("nonooo");
    }
    };
    我说明一下,你这个long的数值的确是超了,但是按照我的这个还是可以打印出来的,不过呢,你还是注意一下吧,你这个数值绝对溢出了。。
      

  17.   

    long l = 1000*1000*1000*20;
    右边先计算,得到int值,此时越界溢出,进行截取后再转型为long,此时l为负数了。
      

  18.   

    能执行,没问题啊,结果是nooooo
      

  19.   

    long l = 1000*1000*1000*20后没加L这样默认int行 溢出 导致l<j输出nonooo 加上L 后 l>j 输出hello了