public class TestInteger { public static void main(String[] args) { //Integer面试题 Integer n1 = 1; Integer m1 = new Integer(1); Integer c1 = Integer.valueOf(1); Integer n2 = 127; Integer m2 = new Integer(127); Integer c2 = Integer.valueOf(127); Integer n3 = 128; Integer m3 = new Integer(128); Integer c3 = Integer.valueOf(128); System.out.println(n1 == m1); //false System.out.println(n1 == c1); //true System.out.println(m1 == c1); //false System.out.println(n2 == m2); //false System.out.println(n2 == c2); //true System.out.println(m2 == c2); //false System.out.println(n3 == m3); //false System.out.println(n3 == c3); //false System.out.println(m3 == c3); //false //String面试题 final String MESSAGE="taobao"; String a ="tao"+"bao"; String b="tao"; String c="bao"; System.out.println(a==MESSAGE); //true System.out.println((b+c)==MESSAGE); //false } }
Integer num1 = new Integer(100); Integer num2 = new Integer(100); System.out.println(num1 == num2); //包装类型存放在堆中,内存地址指向不一样 System.out.println(num1.equals(num2)); //只判断值是否相同 Integer num3 = Integer.valueOf(100); System.out.println(num1 == num3); //false Integer num4 = Integer.valueOf(100); System.out.println(num4 == num3); //true Integer num5 = Integer.valueOf(200); Integer num6 = Integer.valueOf(200); System.out.println(num5 == num6); //false
Integer源码:java
//缓存基本数据类型代码 private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} } public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } valueOf(String s)和valueOf(int i)两个方法基本相同,此方法将始终缓存-128到127范围内的值,若是i的值在-128至127之间,则返回基本数据类型,不建立Integer对象,基本数据类型数据保存在JVM栈中,若是不在缓存返回则建立Integer对象.
String intern方法(返回常量池中该字符串的引用):面试
/** * Returns a canonical representation for the string object. * <p> * A pool of strings, initially empty, is maintained privately by the * class {@code String}. * <p> * When the intern method is invoked, if the pool already contains a * string equal to this {@code String} object as determined by * the {@link #equals(Object)} method, then the string from the pool is * returned. Otherwise, this {@code String} object is added to the * pool and a reference to this {@code String} object is returned. * <p> * It follows that for any two strings {@code s} and {@code t}, * {@code s.intern() == t.intern()} is {@code true} * if and only if {@code s.equals(t)} is {@code true}. * <p> * All literal strings and string-valued constant expressions are * interned. String literals are defined in section 3.10.5 of the * <cite>The Java™ Language Specification</cite>. * * @return a string that has the same contents as this string, but is * guaranteed to be from a pool of unique strings. */ public native String intern();
String mxh1 = "mxh".intern(); String mxh0 = "mxh"; System.out.println(mxh0 == mxh1); //true
当常量池中不存在"mxh"这个字符串的引用,则将这个对象的引用加入常量池,返回则个对象的引用;express
当常量池中存在"mxh"这个字符串的引用,返回这个对象的引用.缓存
"mxh".intern()在常量池中建立"mxh"的引用,mxh0引用了常量池中的"mxh",故结果为true.this
包装类型与基本数据类型的区别:code
1. 包装类型容许为null,基本数据类型不能够;orm
2. 包装类型存放在堆中,基本数据类型存放在栈中;对象
3. 从效率上将,基本数据类型效率高于包装类型;blog
4. 包装类型能够作泛型,基本数据类型不能够;内存
问题解析:
1.为何包装类型容许为null,而基本数据类型不能够?
基本数据类型存放在栈内存中,包装类型及对象存放在对内存中,栈内存中存放的是对象的引用地址,包装类型能够没有指向堆的内存地址,即对象能够为null;
2.为何效率上基本数据类型比包装类型效率高?
基本数据类型存放在栈中,而包装类型存放在堆中,在栈中找到内存地址后还须要去堆中取值,多了一步,因此基本数据类型效率高;
3.为何包装类型能够作泛型而基本数据类型不能够?
集合中存放的都是Object类型及Object的子类,基本数据类型不是Object的子类,固然基本数据类型有对应的包装类;