千锋Java&Day21课后作业

今天学习了包装类和String的部分。

努力到无能为力,拼搏到感动自己
加油!

课堂案例:
Finalize

public class TestFinalize {
	public static void main(String[] args) {	
		System.out.println("程序开始");	
		Student s1 = new Student();	
		s1 = null;		
		System.gc();
		// 手工触发垃圾收集的工作		
		System.out.println("程序结束");		
		// final finalize finally
		}
}
class Student {	
           @Override	
           protected void finalize() throws Throwable { 
           	super.finalize();// 千万不要改		
           	// r = this;//很大概率造成内存泄漏	
           	System.out.println(Student finalize() Executed");	
           	}
    }
运行结果:
程序开始
程序结束
Student finalize() Executed

Encapsulation

public class TestEncapsulation {
	public static void main(String[] args) {		
	       byte baseByte = 10;				
	       Byte b1 = new Byte( baseByte );
	       //基于基本类型byte进行构建		
	       Byte b2 = new Byte("20");
	       //基于字符串进行构建
	       System.out.println(Byte.MIN_VALUE);	
	       System.out.println(Byte.MAX_VALUE);	
	      //Number父类提供的转型方法(数字型6种包装类型)		
	      byte b3 = b1.byteValue();		
	      short s1 = b1.shortValue();		
	      int i1 = b1.intValue();		
	      long l1 = b1.longValue();		
	      float f1 = b1.floatValue();		
	      double d1 = b1.doubleValue();				
	      //parseXXX静态方法(8种包装类型)		
	      byte b4 = Byte.parseByte("123");//兼容的类型		
	      System.out.println(b4);						
	      //valueOf(基本类型)、valueOf(字符串类型)		
	      Byte b6 = Byte.valueOf(baseByte);		
	      System.out.println(b6);				
	      Byte b7 = Byte.valueOf("30");		
	      System.out.println(b7);	
	      }
}
运行结果:
-128
127
123
10
30

String1

public class TestString {
	public static void main(String[] args) {		
	// System.out.println( "abc" );//常量		
	String s1 = "abc";// 池中一个对象 变量s1 = 常量"abc"		
	String s2 = "abc";// 不创建对象 以上两行代码创建了一个“abc”对象,保存在常量池中		
	String s3 = new String("abc");// 堆中一个对象 此行代码创了一个“abc”对象,保存在堆中		
	/*		 * String s3 = new String( "abc" ); 
	//池中一个对象、堆中一个对象 String s1 = "abc";		
	 * //不创建对象,引用池中对象 String s2 = "abc"; 
	 * //不创建对象,引用池中对象		 * 		 */		
	 Integer i = new Integer(100);		
	 System.out.println(s1 == s2);// true		
	 System.out.println(s1 == s3);// false
	 	}
}
运行结果:
true
false

String2

public class TestString2 {
	  public static void main(String[] args) {		
	  // 情况1		
	  // "abc" + "def" //编译时,支持处理为"abcdef"		  
	   // 情况2		
	   String s1 = "abc";// 单独存储"abc" 0x1111		
	   String s2 = "def";// 单独存储"def" 0x2222		
	   String s3 = s1 + s2;// 单独存储“abcdef” 0x3333	
	   System.out.println(s1);		
	   System.out.println(s2);		
	   System.out.println(s3);	
	   }
}
运行结果:
abc
def
abcdef

String3

public class TestCreate {
	public static void main(String[] args) {		
	     String s1 = "abc";		
	     String s2 = s1 +"def";//StringBuilfer.append();	
	     StringBuilder sBuilder = new StringBuilder(s1);//可变字符串(缓冲区)		
	     sBuilder.append("def");//StringBuilder类型的对象"abcdef"		
	     String s4 = sBuilder.toString();//转换回对象	
	     String s3 = "abcdef";		
	     System.out.println(s3 == s4);						
	     for(int i = 0 ; i < 100 ; i++) {			
	     s2 += i;//产生100个中间变量(被Java优化),自动穿件StringBuilder缓冲区,在块空间中添加		}	
	    	//abc0123456789101112131415161718..........................99100
	    }
}
    //理论的验证(真实结存结构)
     class MyClass{
     //MyClass.class	
         String s1 = "abc";	
         String s2 = s1 +"def";	
         String s3 = "abcdef";
 }
运行结果:
false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值