java基础详谈

java基础
public static void main(String[] args) {
Integer integer=new Integer(123);
Integer integer2=new Integer(123);
System.out.println(integerinteger2);
Integer integer1=Integer.valueOf(123);
Integer integer3=Integer.valueOf(123);
System.out.println(integer1
integer3);
System.out.println(integer==integer1);
/**
* Integer.valueOf()方法实现比较简单,判断缓冲池中是否存在Integer对象,有就直接返回一个。
* public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
/
/

* java1.8中缓冲池的默认大小为-128~127之间。
* 测试下:
/
/
*
* 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;
		}
	 */
	Integer newIn=new Integer(555);
	System.out.println(newIn);
	/**
	 * 发现缓冲池的大小是可以改变的。
	 */
	/*
	 * 编译器在自动装箱的时候重新创建new对象 与某些大牛写的好像不一样哎
	 */
	Integer m=456;
	Integer n=456;
	System.out.println("这"+(m==n));//输出false
	
	/**
	 * 基本类型对应的缓冲池如下:
		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
		在使用这些基本类型对应的包装类型时,就可以直接使用缓冲池中的对象。
	 */
}

public static void main(String[] args) {
/**
* Java 的参数是以值传递的形式传入方法中,而不是引用传递。
*/

	/*
	 * float  与      double
	 */
	/*
	 * Java 不能隐式执行向下转型,因为这会使得精度降低。
	 */
	//float x=1.1;
	//这回报错的,1.1是double类型,不可以隐式的向下转型
	float x=1.1f;//这样就对了
	
	
	//int  与  short
	short s1=1;
	//s1=s1+1;//同上
	
	//但是使用 += 或者 ++ 运算符可以执行隐式类型转换。相当于 s1=(short) s1+1
	
	s1+=1;
	s1++;
}

public static void main(String[] args) {
/*
* String 字符串不可变 线程安全
* StringBuffer and StringBuilder 可变
* StringBuffer线程安全,内部使用synchronized进行同步
* StringBuilder不安全但是StringBuilder效率高
*/
}

public static void main(String[] args) {
//String被声明为final不可继承。内部使用char数组,数组也是final修饰,意味着初始化后不可引入其他数组,并且没有改变value
//的方法,因此String 不可变。
/*
* 源代码:
* public final class String
implements java.io.Serializable, Comparable, CharSequence {
该值用于字符存储。
private final char value[];
*/

	/*
	 * String 不可变的好处。
	 *1存储hash值:作为hashmap的键不可变。
	 *2 String pool(字符串常亮池) 的需要,如果有就直接使用,因为不可变
	 *3安全性。  网络传输时候,如果字符串可变,那么得到的就不一定是真实值。
	 *4 线程安全。
	 */
}

public static void main(String[] args) {
/*
* 字符串常量池(String Pool)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。
* 不仅这样,还可以使用String 的interm方法将字符串添加到StringPool中
* 当一个字符串调用 intern() 方法时,如果 String Pool 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),
* 那么就会返回 String Pool 中字符串的引用;否则,就会在 String Pool 中添加一个新的字符串,并返回这个新字符串的引用。
/
/

* 下面示例中,s1 和 s2 采用 new String() 的方式新建了两个不同字符串,而 s3 和 s4 是通过 s1.intern() 方法取得一个字符串引用。
* intern() 首先把 s1 引用的字符串放到 String Pool 中,然后返回这个字符串引用。因此 s3 和 s4 引用的是同一个字符串。
/
“abc”.intern();
“bcd”.intern();
String s6=new String(“abc”);
System.out.println(s6.equals(“abc”));
System.out.println(s6==“abc”);
String s1 = new String(“aaa”);
String s2 = new String(“aaa”);
System.out.println(s1 == s2); // false
String s3 = s1.intern();
String s4 = s1.intern();
System.out.println(s3 == s4); // true
/

* String aaa=“ss”;
* 这种方式直接放到常量池中
/
/
*
* 在 Java 7 之前,String Pool 被放在运行时常量池中,它属于永久代。
* 而在 Java 7,String Pool 被移到堆中。这是因为永久代的空间有限,
* 在大量使用字符串的场景下会导致 OutOfMemoryError 错误。
/
new String(“adf”);
/
*
* 使用这种方式一共会创建两个字符串对象(前提是 String Pool 中还没有 “adf” 字符串对象)。
“adf” 属于字符串字面量,因此编译时期会在 String Pool 中创建一个字符串对象,指向这个 “adf” 字符串字面量;
而使用 new 的方式会在堆中创建一个字符串对象。
*/
}

public static void main(String[] args) {
//java7 开始 switch 判断语句可以使用String
//switch 不支持 long,是因为 switch 的设计初衷是对那些只有少数的几个值进行等值判断,
//如果值过于复杂,那么还是用 if 比较合适
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值