千锋逆战班,包装类和String类

在千锋学习的第21天
不积跬步无以至千里,不积小流无以成江河!加油
今天我学习了包装类和String类

package com.qf.day1.TeatByte;

public class TestAutoUpDownConve {

	public static void main(String[] args) {
		byte b1 = 10;
		
		Byte b2 = 40;
		
		Byte b3 = new Byte("30");
		
		//JDK5之后,提供自动装箱、拆箱
		
		Byte b4 = 40;//自动装箱,将基本类型直接赋值给包装类型 会默认调用valueOf(byte b)
				
		byte b5 = b4;//自动拆箱,将包装类型的值,直接赋值给基本类型 这一个会默认调用 byteValue()方法
		
		Integer i1 = 40;
		int i2 =i1;// 默认调用intValue方法
		
		
	}

}

package com.qf.day1.TeatByte;

public class TestByte {

	public static void main(String[] args) {
		
		byte b = 10;
		
		Byte b1 = new Byte(b);//基于基本类型byte进行构建
		Byte b2 = new Byte("10");//基于字符串进行构建
		
		
		//Number父类提供的转型方法(数字型6种包装类型)
		//将构建的值,返回为特定类型
		byte b3 = b1.byteValue();
		int i1 = b1.intValue();
		short s1 = b1.shortValue();
		long l1 = b1.longValue();
		float f1 = b1.floatValue();
		double d1 = b1.doubleValue();
		
		//parseXXX静态方法(8种包装类型)
		
		byte b4 = Byte.parseByte("111");//返回类型为byte,传入只能传入字符串
		System.out.println(b4);
		
		//valueOf(基本类型)、valueOf(字符串类型) 静态方法
		
		Byte b5 = Byte.valueOf(b);
		System.out.println(b5); //他是Byte类型为什么能够输出值呢,因为他重写了toString方法
		
		Byte b6 = Byte.valueOf("123");
		System.out.println(b6);
		
		
	}

}

package com.qf.day1.TeatByte;

public class TestCache {

	public static void main(String[] args) {
		//Byte、Short、Integer、Long,四种整数包装类型都提供了对应的cache缓冲区,提前创建了256个常用对象。

		//缓冲区(大量的重复应用下,节约内存的效果明显)
				
		//赋值过程(什么叫自动装箱)Debug    
		Short s1 = 100;//0x5555  调用valueOf(100),进行区间的判断(-128~127),满足区间条件,将一个数组中的Short对象进行返回,赋值给s1
		Short s2 = 100;//0x5555 调用valueOf(100)
				
		Short s3 = 200;//0x6666 如果超出范围则返回一个新对象地址
		Short s4 = 200;//0x7777
			
		System.out.println(s1 == s2);
		System.out.println(s3 == s4);
				
		System.out.println(s1.equals(s2));//判断内容
		System.out.println(s3.equals(s4));//判断内容  覆盖了父类的Object  equals{ this == obj}
	}

}

package com.qf.day1.strings;

public class TestBasicString {

	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
	}

}



package com.qf.day1.strings;

public class TestString2 {

	public static void main(String[] args) {

//		String s1 = "abc";//内存中存储abc
//		
//		String s2 = "def";//abcdef   --->   abcdef
//		
		
		//情况1
		//"abc" + "def" //编译时,支持处理为"abcdef"
		
		//情况2
		String s1 = "abc";//单独存储"abc"  0x1111
		
		String s2 = "def";//单独存储"def" 0x2222
		
		String s3 = s1 + s2;//单独存储“abcdef”  0x3333
		
		int count =1; for(int i=1;i<=5;i++){ count+=i; } System.out.println(count);
		
	}
}

package com.qf.day1.strings;

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缓冲区,在块空间中添加
		}

	}
}

//理论的验证(真实结存结构)
class MyClass{//MyClass.class
	String s1 = "abc";
	String s2 = s1 +"def";
	String s3 = "abcdef";
}
package com.qf.day1.strings;

public class TestStrings {

	public static void main(String[] args) {
		
		
		String str1 = "abcdef";
		System.out.println(str1.charAt(1));//public char charAt(int index) :根据下标获取字符。
		
		System.out.println(str1.contains("abc"));//public boolean contains(tring str) :判断当前字符串中是否包含str。
		
		
		char[] arr = str1.toCharArray();//public charD toCharArray( :将字符串转换成数组。
		for(int i=0;i<arr.length;i++){
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		
		
		System.out.println(str1.indexOf("abc"));//public int indexOf(String str) :查找t首次出现的下标,存在,则返回该下标;不存在,则返回-1。
		
		System.out.println(str1.lastIndexOf("bc"));//public int lastIndexOf(String str) :查找字符串在当前字符串中最后一次出现的 下标索引。
		
		
		System.out.println(str1.length());//public int length0:返回字符串的长度。
		
		String str2 = " qwe ";
		System.out.println(str2);
		System.out.println(str2.trim());//public String trim0:去掉字符串前后的空格。
		
		
		
		System.out.println(str2.toUpperCase());//public String toUpperCase():将小写转成大写。
		
		
		String str3 = "ABCD";
		System.out.println(str3.toLowerCase());//将大写转换为小写
		
		System.out.println(str1.endsWith("ef"));//public boolean endWith(String str):判断字符串是否以str结尾。
		
		
		System.out.println(str1.replace("abc", "efd"));//str1里面的abc替换成了efd
		
		
		String[] str5 = str1.split("cd");//public String] split(String str):根据str做拆分。
		
		for(int i=0;i<str5.length;i++){
			System.out.println(str5[i]);
			
		}
		
		
	}

}

运行结果

b
true
a b c d e f 
0
1
6
 qwe 
qwe
 QWE 
abcd
true
efddef
ab
ef
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值