×

【Java中Java.lang.Integer类详解】

前端技术网 前端技术网 发表于2024-01-27 17:31:46 浏览1102 评论0

抢沙发发表评论

一、Java中Integer这个类的详细描述,甚至原代码

public class Test{

public static void main(String[] args){

【Java中Java.lang.Integer类详解】

Integer i1= 127;

Integer i2= 127;

Integer i3= Integer.valueOf(127);

if(i1== i2)

System.out.println("i1== i2 is true!");

【Java中Java.lang.Integer类详解】

else

System.out.println("i1== i2 is false!");

if(i1>= i2)

System.out.println("i1>= i2 is true!");

else

System.out.println("i1>= i2 is false!");

if(i1== i3)

System.out.println("i1== i3 is true!");

else

System.out.println("i1== i3 is false!");

}

}

当值是127时,输出是:

i1== i2 is true!

i1>= i2 is true!

i1== i3 is true!

当值是128时,输出是:

i1== i2 is false!

i1>= i2 is true!

i1== i3 is false!

说明:

我使用的是Sun JDK 1.5.0_03-b07和 Eclipse 3.2M4。

“Integer i1= 127;”在JDK1.4下不能编译通过的,会提示:“ Type mi**atch: cannot convert from int to Integer”的错误,一般改写为:“Integer i1= new Integer(127);”。

“Integer i1= 127;”在JDK1.5下可以编译通过的,这就是自动装箱(Autoboxing)和自动拆箱(Auto-Unboxing)。自动装箱(Autoboxing)特性让Java自动包装一个简单数据类型(例如int)到对应的包装类型中(例如Integer)中。

在《JSR 201: Extending the Java Programming Language with Enumerations, Autoboxing, Enhanced for loops and Static Import》中,对这个问题,是作了这样的规定:

If the value p being boxed is true, false, a byte, an ASCII character, or an integer or short number between-127 and 128, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1== r2.

在Java中,The following is the list of primitives stored as immutable objects(不可变对象):

* boolean values true and false

* All byte values

* short values between-128 and 127

* int values between-128 and 127

* char in the range\u0000 to\u007F

为了更容易理解问题,用Jad将上面代码反编译,如下:

import java.io.PrintStream;

public class Test

{

public Test()

{

}

public static void main(String args[])

{

Integer i1= Integer.valueOf(128);

Integer i2= Integer.valueOf(128);

Integer i3= Integer.valueOf(128);

if(i1== i2)

System.out.println("i1== i2 is true!");

else

System.out.println("i1== i2 is false!");

if(i1.intValue()>= i2.intValue())

System.out.println("i1>= i2 is true!");

else

System.out.println("i1>= i2 is false!");

if(i1== i3)

System.out.println("i1== i3 is true!");

else

System.out.println("i1== i3 is false!");

}

}

从这可以看出,“Integer i1= 128;”在JDK1.5下应该编译成了“Integer i1= Integer.valueOf(128);”。

再看看java.lang.Integer中关于valueOf的源代码是怎样的:

public static Integer valueOf(int i){

final int offset= 128;

if(i>=-128&& i<= 127){// must cache

return IntegerCache.cache[i+ offset];

}

return new Integer(i);

}

可以看出,这个值在-128到127之间,会将其cached(缓存)起来,如果多次使用的话,会节省内存和改善性能;如果不在这个范围之内,则生成一个新的Integer Object instance,这样如果进行“==”时,由于是比较两个不同的Object references,故结果是false。事实上,这个特性从JDK 1.3就存在了(以前的我不清楚)。

二、Java中List<Integer>中的Integer是什么意思

java5.0里的新特性泛型,List<Integer>

list

的意思就是这个listjf里面装的数据类型必须是Integer的,如果装别的开发工具里会出红线提示的类型错误.你直接编译的话会编译失败.

Integer本身是int类型的封装类.意思就是把一个int

类型的数据封装成对象.

int

i=3;

Integer

it=new

Integer(3);

it就是一个对象,可以使用对象能使用的方法.

三、java中 Integer是什么意思

int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别:

int是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象

1.Java中的数据类型分为基本数据类型和复杂数据类型

int是前者而integer是后者(也就是一个类);因此在类进行初始化时int类的变量初始为0.而Integer的变量则初始化为null.

2.初始化时:

inti=1;Integeri=newInteger(1);(要把integer当做一个类看);但由于有了自动装箱和拆箱

使得对Integer类也可使用:Integer i= 1;

int是基本数据类型(面向过程留下的痕迹,不过是对java的有益补充),Integer是一个类,是int的扩展,定义了很多的转换方法

类似的还有:float Float;double Double;boolean Boolean等,而且还提供了处理 int类型时非常有用的其他一些常量和方法

举个例子:当需要往ArrayList,HashMap中放东西时,像int,double这种内建类型是放不进去的,因为容器都是装object的,这是就需要这些内建类型的外覆类了。

Java中每种内建类型都有相应的外覆类。

Java中int和Integer关系是比较微妙的。关系如下:

1.int是基本的数据类型;

2.Integer是int的封装类;

3.int和Integer都可以表示某一个数值;

4.int和Integer不能够互用,因为他们两种不同的数据类型;

举例说明

private void test(Integer iAge){

int age=iAge;

}

test(null);//将会导致空指针异常

并且泛型定义时也不支持int:如:List<Integer> list= new ArrayList<Integer>();可以而List<int> list= new ArrayList<int>();则不行

总而言之:如果我们定义一个int类型的数,只是用来进行一些加减乘除的运算or作为参数进行传递,那么就可以直接声明为int基本数据类型,但如果要像

对象一样来进行处理,那么就要用Integer来声明一个对象,因为java是面向对象的语言,因此当声明为对象时能够提供很多对象间转换的方式,与一些常用

的方法。自认为java作为一们面向对象的语言,我们在声明一个变量时最好声明为对象格式,这样更有利于你对面向对象的理解。

四、Java中怎么将Long类型转换成Integer或int类型

可以使用强制转换来语句来实现数据类型从Long类型转换成Integer或int类型。以下举例说明具体步骤:1.声明一个数据类型为long的变量并初始化:long i= 10;

2.声明一个数据类型为整型(integer)变量:int j;

3.使用强制转换符“()”转换,j=(int)(i)即可实现转换。

代码如下图:

拓展资料

Java中有六种基本数据类型:byte(字节)型、short(短整)型、int(integer)整型、long(长整)型、float(单精度)浮点型、double(双精度)浮点型。数据精度从字节型、短整型、整型、长整型、单精度浮点型、双精度浮点型依次升高。数据类型从低精度到高精度系统会自动转换(即隐式转换),从高精度到低精度需要强制转换(即显示转换)。

好了,文章到此结束,希望可以帮助到大家。