1. Java 的基本数据类型都有哪些各占几个字节
Java的基本数据类型_字符串
2. short s1 = 1;s1 =s1 + 1;有错吗?short s1 = 1;s1 += 1有错吗;

前者不正确,后者正确。对于shorts1=1;s1=s1 + 1;由于1是int类型,因此s1+1运算结果也是int型,
需要强制转换类型才能赋值给short 型。而short s1 = 1;s1 += 1可以正确编译,因为s1+= 1;相当于s1 =
(short)(s1 + 1):其中有隐含的强制类型转换。

3. int和和Integer有什么区别?
Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,为了能够将这些基本数据类型当成对象操作, Java为每一个基本数据类型都引入了对应的包装类型 (wrapperclass) ,int 的包装类就是Integer,从Java 5开始引入了自动装箱/拆箱机制,使得者可以相互转换。

Java为每个原始类型提供了包装类型:

  • 原始类型: boolean, char, byte, short, int, long, float, double
  • 包装类型:Boolean, Character, Byte, Short, Integer, Long, Float, Double
class AutoUnbaxingTest {
	public static void main(String[] args) {
		Integer a = new Integer(3);
		Integer b = 3;//将3自动装箱成Integer类型
		int c=3;
		System.out.println(a == b);// false 两个引用没有引用同一对象
		System.out.printn(a == c);//true a自动拆箱成int类型再和c比较
	}
}
复制代码

4.下面 Integer类型的数值比较输出的结果为?

public class Test03 {
	public static void main(String[] args) {
		Integerf1 = 100, f2= 100, f3=150, f4= 150;
		System.out. println(f1 == f2); 
		System.out. println(f3 == f4); 
	}
}
复制代码

如果不明就里很容易认为两个输出要么都是true 要么都是false。 首先需要注意的是f1、f2、 f3、f4四个变量
都是Integer对象引用,所以下面的= =运算比较的不是值而是引用。装箱的本质是什么呢?当我们给一个Integer对象赋一个int值的时候,会调用Integer 类的静态方法valueOf,如果看看valueOf 的源代码就知道发生了什么。
源码: .

public static Integer valueOf(inti) {
	if(i >= IntegerCache.low && i <= IntegerCache.high)
		return IntegerCache.cache[i + (-IntegerCache.low)];
	return new Integer(i);
}
复制代码

IntegerCache是Integer的内部类,其代码如下所示:

* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
* 
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun. misc.VM class.
*/
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("ava.lang.Integer.IntegerCache.high");
		if (integerCacheHighPropValue != null) {
			try {
				int i = parselnt(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() {}
}
复制代码

简单的说,如果整型字面量的值在-128到127之间,那么不会new新的Integer 对象,而是直接引用常量池中的Integer对象,所以上面的面试题中f1= =f2 的结果是true,而f3= =f4的结果是false.

5. String 类常用方法
Java的基本数据类型_基本数据类型_02
6. String、 StringBuffer、 StringBuilder 的区别?
(1)、可变不可变
String:字符串常量,在修改时不会改变自身;若修改,等于重新生成新的字符串对象。
StringBuffer:在修改时会改变对象自身,每次操作都是对StringBuffer对象本身进行修改,不是生成新的对象;使用场景:对字符串经常改变情况下,主要方法: append (),insert ()等。
(2)、线程是否安全
String:对象定义后不可变,线程安全。
StringBuffer:是线程安全的(对调用方法加入同步锁),执行效率较慢, 适用于多线程下操作字符串缓冲区大量数据。
StringBuilder:是线程不安全的,适用于单线程下操作字符串缓冲区大量数据。
(3)、共同点
StringBuilder与StringBuffer有公共父类AbstractStringBuilder(抽象类)。
StringBuilder、StringBuffer的方法都会调用AbstractStringBuilder中的公共方法,如super.append(…)
只是StringBuffer会在方法上加synchronized关键字,进行同步。最后,如果程序不是多线程的,那么使用StringBuilder效率高于StringBuffer.

7.数据类型之间的转换
(1)、字符串如何转基本数据类型?
调用基本数据类型对应的包装类中的方法parseXX(String)或valueOf(String)即可返回相应基本类型。
(2)、基本数据类型如何转字符串?
一种方法是将基本数据类型与空字符串("")连接(+ )即可获得其所对应的字符串;另一种方法是调用String类中的valueOf()方法返回相应字符串。