搜索
您的当前位置:首页正文

Java之常用类简介及使用方法

来源:步旅网

基本数据类型的包装类

包装类是将基本类型封装到一个类中
包含属性和方法,方便对象操作
包装类位于java.lang包中

//基本数据类型转换为包装类,装箱
Integer value = new Integer(10);
Integer value = new Integer("10");
Integer value = Integer.valueOf(11);
Integer value = Integer.valueOf("11");

//包装类转换成基本数据类型,拆箱
Integer value = new Integer(12);
int num = value.intValue();

//基本类型和包装类的自动转换,自动装箱or拆箱
Integer value = 13;
int num = value;

包装类并非用于取代基本类型
在使用基本数据类型和包装类进行比较等过程中会涉及到自动装箱自动拆箱
装箱:将基本数据类型转换成包装类
** 拆箱:将包装类转换成基本数据类型**

Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i1==i2)//true
System.out.println(i3==i4)//false

无处不在的String

//字符串创建的几种方式
String s1 = "HelloWorld";
String s2 = new String();
String s3 = new String("HelloWorld");

//字符串比较
s1.equals(s3);//true
s1==s3;//false 比较是s1指向的常量池地址与s3对象地址值
s3=s3.intern();//使s3对象地址值改为s3常量池地址值
s1==s3;//true因为常量池地址相同
String a = "abc";
String b = "def";
String c = "abcdef";
String d = a+b;//使用到时再分别去a和b指向的常量池中取值后进行装箱
String e = "abc"+"def";//拼接成字符串后去常量池之中找值并使引用指向常量池的地址
System.out.println(c==d);//false
System.out.println(c==e);//true

.intern方法的用途

关于字符串String中的intern方法,是当前的字符对象(通过new出来的对象)可以使用intern方法从常量池中获取,

如果常量池中不存在该字符串,那么就新建一个这样的字符串放到常量池中。

https://www.cnblogs.com/feizhai/p/10196955.html

可变字符串

	StringBuffer stra = new StringBuffer();
	StringBuffer strb = new StringBuffer("abc");
	strb.append(1).append("def").append(true);//转换为字符串后进行拼接
StringBuffer:线程安全,效率低
StringBuilder:线程不安全,效率高

时间处理相关类

Date date = new Date();
System.out.println(date);
System.out.println(date.getTime());//打印1900年到当前时间毫秒值
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"ss");//创建日期格式化类
String str = dateFormat.format(date);//格式化日期并返回字符串
System.out.println(str);
Date d = dateFormat.parse("2011-11-11 22:22:22");//字符串转换为日期类

//日历类
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
calender.serTime(d);//传入日期类设置日历类
System.out.println(calendar.get(Calendar.YEAR));

Math类

Math.abs(-10);//取绝对值
Math.sqrt(2);//开方
Math.ceil(3.14)//向上取整
Math.floor(3.14)//向下取整
Math.pow(2,3)//次方

枚举

1.只能取特定值中的一个
2.使用enum关键字
3.所有枚举类型隐性地继承自java.lang.Enum(枚举实质上还是类,而每个被枚举的成员实质上就是一个枚举类型的实例,他们默认都是public static final的。可以直接通过枚举类型名直接使用它们)
4.使用一组常量时,建议使用枚举类型

public enum Color{//定义枚举类
	red,green,blue
}
public class Test{
	Color col = Color.red;
	COlor col2 = Color.greed;
}
public enum EventEnum{//定义枚举类
	LAUNCH("lauch"),PAGEVIEW("pageview"),EVENT("event");
	EventEnum(String name){//枚举类构造方法参数与上面一行必须相同
		this.name = name;
	}
	private String name;
	
	public void show(){
		System.out.println(this.name);
		//values获取所有枚举类型,方法由jvm提供,编译时会加上value方法的实现,同样jvm还提供valueOf(String s)
		EventEnum[] ee = values();
		for(int i = 0;i<ee.length;i++)
		{
			//遍历打印LAUNCH PAGEVIEW EVENT
			System.out.println(ee[i]);
		}
	}
}
//另外一个类
public class Test()
{
	public static void main(String [] args){
		EventEnum ee = EventEnum.LAUNCH;//等于new EventEum("lauch");
		ee.show();//打印launch

		String name = EventEnum.PAGEVIEW.name();//打印PAGEViEW,name()由jvm提供
		System.out.println(name);
	}
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top