天天看点

java常用类(一)

  • 包装类

    一、为什么需要 包装类?

    JAVA并不是纯面向对象的语言。Java语言是一个面向对象的语言,但是Java中的基本数据类型却

    是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。比如:

    集合的操作中。 这时,我们就需要将基本类型数据转化成对象!

    二、包装类均位于java.lang包,包装类和基本数据类型的对应关系:

基本数据类型 包装类
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double

三、包装类的作用:

①提供:字符串、基本类型数据、对象之间互相转化的方式!

②包含每种基本数据类型的相关属性如最大值、最小值等

③所有的包装类(Wrapper Class)都有类似的方法,掌握一个其他都类似!

package com.qhit.test;

/**
 * 测试包装类
 * Integer类的使用。其他包装类用法类似。
 * @author 梁雪
 *
 */
public class TestWrappedClass {
	public static void main(String[] args) {
		//基本数据类型转成包装类对象
		Integer   a  = new  Integer(3);
		Integer   b  = Integer.valueOf(30);   

		//把包装类对象转成基本数据类型
		int   c  =  b.intValue();
		double d = b.doubleValue();
		
		//把字符串转成包装类对象
		Integer  e = new  Integer("9999");
		Integer  f = Integer.parseInt("999888");
		
		//把包装类对象转成字符串
		String   str = f.toString();    //""+f
		
		//常见的常量
		System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
		
		
		
		
	}
}

           
int类型最大的整数:2147483647
  • 自动装箱和自动拆箱

    一、 自动装箱-boxing就是自动将基本数据类型转换为包装器类型;

基本类型就自动地封装到与它相同类型的包装中

Integer i = 100;

本质上是,编译器编译时为我们添加了: Integer i = Integer.valueOf(100);

二、自动拆箱autounboxing就是自动将包装器类型转换为基本数据类型。

包装类对象自动转换成基本类型数据。如:

int a = new Integer(100);

本质上,编译器编译时为我们添加了:

int a = new Integer(100).intValue();

看下面代码 测试自动装箱、自动拆箱

/**
 * 测试自动装箱、自动拆箱
 * @author 梁雪
 *
 */
public class TestAutoBox {
	public static void main(String[] args) {
		Integer   a  = 234;   //自动装箱。Integer  a = Integer.valueOf(234);
		int   b =  a;				//自动拆箱。编译器会修改成:int  b = a.intValue();
		
		Integer  c = null; 
//		if(c!=null){
//			int  d = c;			//自动拆箱:调用了:c.intValue()
//		}
		

		//缓存[-128,127]之间的数字。实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组。
		//当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经建好的对象
		//如果不在这个范围,则创建新的Integer对象。
		Integer in1 = Integer.valueOf(-128);  
        Integer in2 = -128;
        System.out.println(in1 == in2);//true 因为123在缓存范围内
        System.out.println(in1.equals(in2));//true
        System.out.println("################");
        Integer in3 = 1234;
        Integer in4 = 1234;
        System.out.println(in3 == in4);//false 因为1234不在缓存范围内
        System.out.println(in3.equals(in4));//true
 		
	}
}

           
  • 字符串相关类String

    一、String(不可变字符序列)

    ①Java字符串就是Unicode字符序列,例如串“Java”就是4个Unicode字符J,a,v,a组成的。

    ②Java字符串就是Unicode字符序列,例如串“Java”就是4个Unicode字符J,a,v,a组成的。

    ③Java允许使用符号"+"把两个字符串连接起来

String s1 = “Hello”;String s2 = “World!”; 
String s = s1 + s2; //HelloWorld!
           

二、String类的常用方法

• char charAt(int index)

返回字符串中第index个字符。

• boolean equals(String other)

如果字符串与other相等,返回true

• boolean equalsIgnoreCase(String other)

如果字符串与other相等(忽略大小写),则返回true

• int indexOf(String str) lastIndexOf() • int length()

返回字符串的长度。

• String replace(char oldChar,char newChar)

返回一个新串它是通过用 newChar 替换此字符串中出现的所有oldChar而生

• boolean startsWith(String prefix)

如果字符串以prefix开始,则返回true

• boolean endsWith(String prefix)

如果字符串以prefix结尾,则返回true

• String substring(int beginIndex) • String substring(int beginIndex,int endIndex)

返回一个新字符串,该串包含从原始字符串beginIndex到串尾或endIndex-1的所有字符

• String toLowerCase()

返回一个新字符串,该串将原始字符串中的所有大写字母改成小写字母

• String toUpperCase()

返回一个新字符串,该串将原始字符串中的所有小写字母改成大写字母

• String trim()

返回一个新字符串,该串删除了原始字符串头部和尾部的空格

三、字符串相等的判断(一般使用equals方法)

equals判断字符串值相等,==判断字符串对象引用相等!

public class StringTest3 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s1==s2); //true
System.out.println(s1==s3); //false
System.out.println(s3==s4); //false
}
}
           
  • StringBuffe和StringBuilder

    StringBuffer和StringBuilder非常类似,均代表可变的字符序列,而且方法也一样

/**
 * 测试StringBuilder、StringBuffer可变字符序列
 * @author 梁雪
 *
 */
public class TestStringBuilder {
	public static void main(String[] args) {
		String  str;
		
		//StringBuilder线程不安全,效率高(一般使用它);StringBuffer线程安全,效率低。
		StringBuilder  sb = new StringBuilder("abcdefg");
		
		System.out.println(Integer.toHexString(sb.hashCode()));  
		System.out.println(sb);
		
		sb.setCharAt(2, 'M');
		System.out.println(Integer.toHexString(sb.hashCode()));  
		System.out.println(sb);
		
	}
}

           
/**
 * 测试StringBuilder、StringBuffer可变字符序列的常用方法.
 * @author 梁雪
 *
 */
public class TestStringBuilder2 {
	public static void main(String[] args) {
		StringBuilder   sb =  new StringBuilder();
		
		for(int i=0;i<26;i++){
			char temp = (char)('a'+i);
			sb.append(temp);
		}
		
		System.out.println(sb);
		sb.reverse();		//倒序
		System.out.println(sb);
		sb.setCharAt(3, '高');
		System.out.println(sb);
		sb.insert(0, '我').insert(6, '爱').insert(10, '你');  		//链式调用。核心就是:该方法调用了return this,把自己返回了。
		System.out.println(sb);
		
		sb.delete(20, 23);
		System.out.println(sb);
		
		
	}
}

           

字符串选用

String:不可变字符序列

StringBuilder:可变字符序列、效率高、线程不安全

StringBuilder:可变字符序列、效率低、线程安全

String使用陷阱:

string s="a"; //创建了一个字符串
s=s+"b";

/*实际上原来的"a"字符串对象已经丢弃了,现在又产生了一个字符串s+"b"。如果多次执
行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的
操作放到循环中,会极大影响程序的性能。*/
           
package cn.sxt.test;

/**
 * 测试可变字符序列和不可变字符序列使用的陷阱
 * 
 * @author 梁雪
 *
 */
public class TestStringBuilder3 {
	public static void main(String[] args) {

		/** 使用String进行字符串的拼接 */
		String str8 = "";
		// 本质上使用StringBuilder拼接, 但是每次循环都会生成一个StringBuilder对象
		long num1 = Runtime.getRuntime().freeMemory();// 获取系统剩余内存空间
		long time1 = System.currentTimeMillis();// 获取系统的当前时间
		for (int i = 0; i < 5000; i++) {
			str8 = str8 + i;// 相当于产生了10000个对象
		}
		long num2 = Runtime.getRuntime().freeMemory();
		long time2 = System.currentTimeMillis();
		System.out.println("String占用内存 : " + (num1 - num2));
		System.out.println("String占用时间 : " + (time2 - time1));
		
		
		/** 使用StringBuilder进行字符串的拼接 */
		StringBuilder sb1 = new StringBuilder("");
		long num3 = Runtime.getRuntime().freeMemory();
		long time3 = System.currentTimeMillis();
		for (int i = 0; i < 5000; i++) {
			sb1.append(i);
		}
		long num4 = Runtime.getRuntime().freeMemory();
		long time4 = System.currentTimeMillis();
		System.out.println("StringBuilder占用内存 : " + (num3 - num4));
		System.out.println("StringBuilder占用时间 : " + (time4 - time3));
	}
}

           
上一篇: 上机课作业
下一篇: 6.12上机作业