实用类

实用类

枚举

使用枚举的好处:

  1. 类型安全
  2. 易于输入
  3. 代码清晰

示例:
枚举类:

 public enum Genders{
	Male,Female
}

学生类:

public class Student{
   public  Genders  sex;
}

包装类

所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
在这里插入图片描述
包装类的常用方法:
在这里插入图片描述

Math类

常用方法:

Math.abs(-3.5); //返回3.5  绝对值
Math.max(2.5, 90.5);//返回90.5 最大值
int random = (int) (Math.random() * 10); //生成一个0-9之间的随机数

Random类

Random rand=new Random(); //创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示
    int num=rand.nextInt(10);//返回下一个伪随机数,(随机出0-9的整数)
  	System.out.println("第"+(i+1)+"个随机数是:"+num);
} 

String类

  1. 字符串长度:length();
  2. 比较存储在两个字符串对象的内容是否一致:equals();
  3. 不区分大小写比较:equalsIgnoreCase();
  4. 转换成小写:toLowerCase();
  5. 转换成大写:toUpperCase();
  6. 字符串连接:“+” 或者 concat()
  7. 字符串常用提取方法:
    在这里插入图片描述

"=="和equals()的区别:

  1. ==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象
  2. equals():检查组成字符串内容的字符是否完全一致

StringBuffer类

对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率
声明和使用:

StringBuffer strb = new StringBuffer();
strb.toString();           	//转化为String类型
strb.append("abc");  	    //追加字符串
strb.insert (1, "abc");     //插入字符串

Date类和SimpleDateFormat类

//创建日期对象
Date date = new Date(); 
//定制日期格式
SimpleDateFormat formater = new SimpleDateFormat("yyyy- MM-dd 	HH:mm:ss");
String now = formater.format(date);
System.out.println(now);

Calendar类

在这里插入图片描述
示例:

Calendar c= Calendar.getInstance();
c.set(Calendar.YEAR,2015);
c.set(Calendar.MONTH,3);
c.set(Calendar.DAY_OF_MONTH,5);

int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
int year_week = c.get(Calendar.WEEK_OF_YEAR);
System.out.println(year_week);
System.out.println(year+"-"+month+"-"+day);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值