首页 > 编程知识 正文

string类型和integer,integer int 比较

时间:2023-05-06 04:10:20 阅读:129203 作者:4958

packages fk.BBS.test.springjsbctemplettest; importorg.junit.Test; 公共类测试库

{

@Testpublic voidtest () ) )。

{/*源代码

*publicstaticintegervalueof{

if (I=integer cache.lowi=integer cache.high ) )。

returnintegercache.cache [ I (-integer cache.low ) ];

返回新集成器(I;

() /

//自动装箱//系统将介于[-128,127 ]之间的整数自动装箱到Integer实例中,并缓存在名为cache的数组中。 如果稍后将介于[-128,127 ]之间的整数自动填充到Integer实例中,则//实际上是指向直接对应的数组元素

integer autopackage0=integer.value of (1;/* * *公共统计信息转换器(inti ) {

if (I=integer cache.lowi=integer cache.high ) )。

returnintegercache.cache [ I (-integer cache.low ) ];

返回新集成器(I;

() /

//自动装箱,直接给一个对象赋值时,调用用integer.valueof(inti )这个方法//双等进行比较的是对象所在的地址

integer自动打包1=1;

integer自动打包2=1;

integernewinteger=newinteger(1;

system.out.println (在integer的cache时); //equeals比较

system.out.println (自动打包1.equals )自动打包2 ) : (自动打包1.equals )自动打包2 ); //true如果两个对象都是Integer,则表示正在比较自动开箱的值

system.out.println (自动打包1.equals (new integer ) : ) auto打包1.equals (new integer ) ); //true,此时不管两者的值是否在Integercache中,都用//双等进行比较

system.out.println (自动打包1==自动打包2: ) (自动打包1==自动打包2 ) ); //true,考虑自动开箱

system.out.println (autopacking1==newinteger : ) ) auto packing1==new integer ); //fasle,new关键字的意思是必须新建,而不是现在已经有了

system.out.println ('如果不在integer的cache中'); //integerautopacking3=integer.value of (128;

integer自动打包4=128;

integer自动打包5=128;

Integernewintger2=新Integer (128;

system.out.println (自动打包5==自动打包4: ) (自动打包5==自动打包4 ) ); //a返回false,自动装箱,因为它已经不在cache中,然后调用integer.valueof(intI )方法。

system.out.println (自动打包3==自动打包4: ) (自动打包3==自动打包4 ) ); 返回//false。 因为都是新对象,所以地址不同。 全部返回false

system.out.println (auto packing4==new intger 23360 ) ) autopacking4==newintger2); 返回false

System.out.println;

system.out.println (auto packing4. intvalue (==auto packing4. intvalue ) );//要双等判断两个对象的值是否相等时,调用intValue方法

}

@Testpublic voidtestString () )

{/*** String的equals方法首先比较两个对象的地址是否相等,如果地址相等,则返回true。

*如果地址不相等,则遍历字符路径以确定两个对象的字符串值是否相等,如果相等,则返回true

publicbooleanequals (对象对象) {

if(this==anobject ) {

返回真;

}

if(anobjectinstanceofstring ) {

stringanotherstring=(string ) anObject;

int n=value.length;

if (n==anotherstring.value.length ) {

char v1[]=value;

char v2[]=anotherString.value;

int i=0;

wile(n----!=0}{

if(V1[I]!=v2[i] )

返回假;

I;

}

返回真;

}

}

返回假;

() }*/System.out.println ) )事先声明,new的一个对象什么时候要表示的是新对象,而不是我已经拥有的对象

String s1='qq ';

String s11='qq ';

stringS2=newstring('QQ ); //双等

system.out.println (S1==s 11: ) ) S1==S11 ); //相等的原因是,java缓存机制//系统会创建一个名为qq的String实例,并缓存该实例。 第二次//String s11='qq '; 对于代码,系统首先检查缓存中是否有此String实例的内容。 与此' qq'//直接量的字符串相同。 如果在缓存中知道这样的String实例,则系统会直接使此String实例//或对两者的引用地址相等

system.out.println(S1==S2: ) ) S1==S2 ); //equals

system.out.println (S1.equals (S11 ) : ) S1.equals (S11 ); 可见//true在equals方法的定义中返回true

system.out.println (S1.equals (S11 ) : ) S1.equals (S11 ); 可见//true在equals方法的定义中返回true

system.out.println (S1.equals (S11 ) : ) S1.equals (S11 ); //true从equals方法的定义中可以看出,返回true//String的缓存机制与Integer的常量池不同

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。