实用类介绍

一、枚举

枚举指由一组固定的常量组成的类型

特点: 类型安全,易于输入,代码清晰
枚举的常见写法
a)直接定义枚举值

public enum Week{
	//枚举值全大写,逗号分隔,类名前加enum关键词区分枚举类
	SUN,MON,TUE,WED
}

b)在枚举中添加方法

public enum Week{
	SUN("星期日"),
	MON("星期一"),
	TUE("星期二"),
	WED("星期三");
	
	private String name;
	//注意get/set方法只写get方法,枚举本质是个静态常量,如果有set方法,
	//就能随意修改静态常量的值,在实际使用时便会发生错误
	public String getName() {  
        return name;  
    }  
    //构造方法为私有的,无法被实例化
	private Week(String name){
	this.name = name;
	}
}

c)switch选择结构中,case值可为枚举值

二、包装类

a)每个基本类型在java.lang包中都有一个相应的包装类
int——Integer
byte——Byte
short——Short
long——Long
float——Float
double——Double
char——Character
boolean——Boolean

b)作用:
提供了一系列使用的方法,
集合不允许存放基本数据类型的数据,存放数字时,要用包装类

c)注意:
(1)Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false

(2)当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常

(3)包装类的默认值为null,基本类型的默认值为一个数值,包装类可区分出未赋值和赋值为0的情况,可用包装类来区分比如学生缺考和学生成绩为0分的情况,像在jsp的表单数据应用中就有实际用途

(4)Integer对象封装的数据在1byte的取值范围内,即-128~127,在这个范围内比较的是数据的值是否相等,若超出则比较的是地址是否相同

	Integer tg1 = 500;
	Integer tg2 = 500;
	tg1 == tg2;//false

	Integer tg1 = 127;
	Integer tg2 = 127;
	tg1 == tg2;//true
//若括号内为非数字字符串代码编译则会报异常
	Integer i=new Integer(1);
	Integer i=new Integer("150");
	Integer i=new Integer("hi");//编译会有NumberFormatException异常

d)包装类的常用方法(都为静态方法,直接调用)
将包装类数据转为相应的基本类型的数据:
byteValue()、intValue()、longValue()、shortValue()
doubleValue()、floatValue()、charValue()、booleanValue()

toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)

valueOf:返回一个指定类型的Integer实例对象

parselnt(String s):数值型字符串转基本类型数据

	Integer integerId=new Integer(25);
	int intId=integerId.intValue();
	int byteId=integerId.byteValue();
	int doubleId=integerId.doubleValue();

e)包装类的特点:

JDK1.5后,允许基本数据类型和包装类型进行混合数学运算
包装类并不是用来取代基本数据类型的,在基本数据类型需要用对象表示时使用

f)自动拆箱和自动装箱的概念
自动装箱 ----- 基本类型的值 → 包装类的实例,数值自动转为对象

自动拆箱 ----- 基本类型的值 ← 包装类的实例,对象自动转换为数值

三、String对象

a)使用String对象存储字符串

	String s = "Hello World";
	String s = new String();
	String s = new String("Hello World");

b)字符串比较的方法
"=="和equals()有什么区别:
equals比较的是两个字符串的值是否相同
==比较的字符串的值和地址是否同时相同

字符串比较的其他方法
使用equalsIgnoreCase()方法 --不区分大小写
//比如登录时不考虑用户名的大小写问题,实现登录

使用toLowerCase()方法,字符串全转为小写
使用toUpperCase()方法,字符串全转为大写

c)字符串的连接

方法1:使用“+”

方法2:使用String类的concat()方法

d)字符串常用提取方法

(1)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int indexOf(int ch)
public int indexOf(String value)

(2)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int lastIndexOf(int ch)
public int lastIndexOf(String value)

(3)提取从位置索引开始的字符串部分
public String substring(int index)

(4)提取beginindex和endindex之间的字符串部分
public String substring(int beginindex, int endindex)

(6)返回一个前后不含任何空格的调用字符串的副本
public String trim()

(7)字符串拆分: 字符串 调用split(""); 方法 进行拆分

四、StringBuffer类

a)StringBuffer声明:

	StringBuffer strb = new StringBuffer();
	StringBuffer strb = new StringBuffer("aaa");

b)StringBuffer的使用:

sb.toString(); //转化为String类型
sb.append(“xx”); //追加字符串
sb.insert (1, “xx”); //插入字符串

c)String类&StringBuffer类

特性:String是不可变对象,经常改变内容的字符串最好不要使用String

StringBuffer是可变的字符串,字符串经常改变的情况可使用StringBuffer,更高效

JDK5.0后提供了StringBuilder,等价StringBuffer,不同点是StringBuffer线程安全,效率低;StringBuilder线程不安全,效率高,内部加不加锁的原因

五、Random随机数类

next(int bits)返回相应数据类型的随机值

	Random random = new Random;
	int ranInt = random.nextInt(10);//产生[0,10)间的一个整数类型随机数
	//注意next方法中boolean、long、double、float无法传参
	int ranLong = random.nextLong();
	int ranDouble = random.nextDouble();

注意:
Random类所产生的随机数是一个伪随机数,其产生的随机数是根据种子和顺序决定的,详情可自行了解

六、Math类

Math 的方法都被定义为静态形式,通过 Math 类可以在主函数中直接调用
Math类的常用方法
static double abs(); //返回对应数据类型的绝对值

static double ceil(double a); //向上取整
static double floor(double a); //向下取整
static double round(double a)//四舍五入取整——》floor(a+0.5);

	Math.ceil(10.3);——》11
	Math.floor(10.9);——》10
	Math.round(10.4);——》10
	Math.round(-10.6);——》-11

static long max(long a, long b); //比较两数中的最大值
static long min(long a, long b); //比较两数中的最小值

七、Date类

java.util.Date类:表示日期和时间
java.text.SimpleDateFormat类 指定日期格式

		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String form = format.format(date);
		System.out.println(form);

八、Calendar类

		Calendar calendar = Calendar.getInstance();
		Date time = calendar.getTime();
		System.out.println(time);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int week = calendar.get(Calendar.DAY_OF_WEEK)-1;//国外星期从星期天开始
		System.out.println(year + "年" + month + "月" + day + "日" + "星期" + week);
		Calendar calendar = Calendar.getInstance();
		//Date date = new Date();
		//calendar.setTime(date);//设置当前时间
		calendar.set(2015, 4, 6);//设置自定义事件时间
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		System.out.println(year + "年" + month + "月" + day + 
		"日是今年的第" + week + "个星期");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值