千锋20200302

千锋“逆战”学习第21天

      每日一句:如果调试一个程序让你很苦恼,千万不要放弃,成功永远在拐角之后,除非你走到拐角,否则你永远不知道你离他多远,所以,请记住,坚持不懈,直到成功。
      今天学了包装类和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

--------------------------------------------------------------------------
String_1

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

--------------------------------------------------------------------------
String_2

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

--------------------------------------------------------------------------
String_3

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

--------------------------------------------------------------------------

预习

String类常用方法:

public char charAt(int index)	//根据下标获取字符
public boolean contains(String str)	//判断当前字符中是否包含str
public char[] toCharArray()	//将字符串转化为数组
public int indexOf(string str)	//查找str首次出现的下标,存在则返回该下标,不存在返回-1
public int lastIndexOf(String str)	//查找字符串中最后一次出现的下标索引
public int length() 	//返回字符串长度
public String trim()	//去掉字符串前后的空格
public String toUpperCase()	//小写转大写
public boolean endWith(String str)	//判断字符串是否以str结尾
public String replace(char oldChar,char newChar)	//将旧字符替换成新字符
public String[] split(String str)	//根据str做拆分

--------------------------------------------------------------------------
可变字符串

StringBuffer	//可变长字符串,JDK1.0提供,运行效率慢,线程安全
StringBuilder	//可变长字符串,JDK5.0提供,运行效率快,线程不安全
发布了40 篇原创文章 · 获赞 0 · 访问量 1149

猜你喜欢

转载自blog.csdn.net/qq_41841482/article/details/104618762