JAvaにおけるInteger,String判定等とintegerの比較サイズ
10685 ワード
package sfk.bbs.test.springjsbctempletTest;
import static org.junit.Assert.*;
import org.junit.Test; public class testBase { @Test public void test() { Integer tt2 = -129; Integer tt = new Integer(-129);// Integer tt2 = -129;, [-128,127] Integer tt2 = -129; new integer
System.out.println(tt == tt2);// , new integer , . .== , System.out.println(tt.equals(tt2));//equals , int , int . true System.out.println(tt > tt2);// int , System.out.println(tt < tt2); //System.out.println(tt + tt2); /** * false * true * false * false */ } }
拡張版
package sfk.bbs.test.springjsbctempletTest;
import org.junit.Test;
public class testBase
{
@Test
public void test()
{
/*
* public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}*/
//
// [-128,127] Integer , cache , [-128,127] integer ,
// . a true
Integer autoPackage0 = Integer.valueOf(1);
/**
* public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
*/
// , , Integer.valueOf(int i)
//
Integer autoPacking1 = 1;
Integer autoPacking2 = 1;
Integer newInteger = new Integer(1);
System.out.println(" integer cache ");
//equeals
System.out.println("autoPacking1.equals(autoPacking2) : "+autoPacking1.equals(autoPacking2));//true Integer
System.out.println("autoPacking1.equals(newInteger) : "+autoPacking1.equals(newInteger));//true,, Integercache
//
System.out.println("autoPacking1==autoPacking2 : "+(autoPacking1==autoPacking2));//true,
System.out.println("autoPacking1==newInteger : "+(autoPacking1==newInteger));//fasle,new ,
System.out.println(" integer cache ");
//
Integer autoPacking3 = Integer.valueOf(128);
Integer autoPacking4 = 128;
Integer autoPacking5 = 128;
Integer newIntger2 = new Integer(128);
System.out.println("autoPacking5==autoPacking4 : "+(autoPacking5==autoPacking4));//a false, cache , Integer.valueOf(int i) .
System.out.println("autoPacking3==autoPacking4 : "+(autoPacking3==autoPacking4));// false, , , false
System.out.println("autoPacking4==newIntger2 : "+(autoPacking4==newIntger2));// false
System.out.println(" Integer (>,=,<=) , value ");
System.out.println(autoPacking4.intValue()==autoPacking4.intValue());// , intValue
}
@Test
public void testString()
{
/**
* String equals , , true,
* , , , , true
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
*/
System.out.println(" , new , , ( ) ");
String s1 = "qq";
String s11 = "qq";
String s2 = new String("qq");
//
System.out.println("s1==s11 : "+(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
}
}
http://bbs.csdn.net/topics/392029500
http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html