原始类型封装后的比较 Integer Short Long
2008-11-13 15:37
381 查看
Integer是对原始类型int的封装。Short是对short的封装。Long是对long的封装.public class IntegerTest {
public static void main(String[] args) {
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);
if(x == y){
System.out.println("x == y");
}
if(x.equals(y)){
System.out.println("x.equals(y)");
}
if(x.equals(z)){
System.out.println("x.equals(z)");
}
if(x.equals(x2)){
System.out.println("x.equals(x2)");
}
if(x.equals(z2)){
System.out.println("x.equals(z2)");
}
}
}
运行结果:==========================x.equals(y)
要注意的一点是Integer 不能和诸如 Short类型比较,因为他们属于不同的类型,所以它们不会相等。
public static void main(String[] args) {
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);
if(x == y){
System.out.println("x == y");
}
if(x.equals(y)){
System.out.println("x.equals(y)");
}
if(x.equals(z)){
System.out.println("x.equals(z)");
}
if(x.equals(x2)){
System.out.println("x.equals(x2)");
}
if(x.equals(z2)){
System.out.println("x.equals(z2)");
}
}
}
运行结果:==========================x.equals(y)
要注意的一点是Integer 不能和诸如 Short类型比较,因为他们属于不同的类型,所以它们不会相等。
相关文章推荐
- Double与Float,Integer,Long封装类型的变量,比较大小时,
- java Long、Integer 、Double、Boolean类型 不能直接比较
- 1.00 【基础知识之基础类型】 JAVA中Long与Integer比较容易犯的错误
- Java中的原始类型和相应的封装类,int--Integer等
- 包装类型的比较,如:Integer,Long,Double
- Java的String.valueOf 转换 与、空串+类型变量转换与封装类(Integer)的toString方式转换比较。
- Java的String.valueOf 转换 与、空串+类型变量转换与封装类(Integer)的toString方式转换比较
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- Java中long类型与Long类型的区别和大小比较详解
- 08.java语言基础-整数类型(byte-short-int-long)
- hibernate错误 this id generator generates long, integer, short or string
- java基础之Integer和Long对象相等比较
- Map key为Integer类型时用Long值取value问题
- Long的equals和相同数值的Integer比较为何为false
- java中数据类型转换 Integer String Long Float Double Date
- Long类型比较大小
- c语言基本数据类型(short、int、long、char、float、double)
- js,格式化long类型日期封装
- java基本数据类型与封装类型详解(int和Integer区别)
- Hibernate Tip: 为什么要把变量类型设置为Long, Integer,而不是long, int