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 类型无法比较吗 ?
long l = 1000*1000*1000*20;
int j = 109613828;
if (l > j)
System.out.println("hello"
);
else
System.out.println("nonooo");
}
} 无显示结果,因为long 和int 类型无法比较吗 ?
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
运行程序,执行的是else语句。调试发现果然是l=-1474836480。
long的范围不是–9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807吗?
这是怎么回事
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
#
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");
}
}这样是可以的,你为什吗要用强制转化啊,这个会自动转换
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");
}
}按你的方法测试也没有问题,
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");
}
}
}
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");
}
}这个测试也没有问题
为什么不行?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
不用自己强制转换。不知道你们是怎么测试的
long l = 1000 * 1000 * 1000 * 20;这句代码右边没有写L的话 是int类型的 这个时候超出的范围了
所以就截断了 成负数了
这样写就打印hello了
long l = 1000 * 1000 * 1000 * 20L;
在比较l>j时,java会自动进行类型提升,不用强制转换。
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溢出!
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");
}
}
1000 * 1000 * 1000 * 20 结果先存在int ,然后再转的long
所以你的值会变成负的
{
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的数值的确是超了,但是按照我的这个还是可以打印出来的,不过呢,你还是注意一下吧,你这个数值绝对溢出了。。
右边先计算,得到int值,此时越界溢出,进行截取后再转型为long,此时l为负数了。