Math类
Java的Math类包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。Math类的方法都被定义为静态形式,通过Math类可以在主函数中直接调用。
Random类
Random():用于创建一个伪随机数产生器。
Random random=new Random();
System.out.printf(random.nextInt(100)+"");//随机产生0-100
方法 | 说明 |
---|---|
nextboolen() | 随机生成true或false |
nextInt() | 随机生成一个int类型的值 |
nextInt(int n) | 随机生成一个[0,n)的值 |
Data类
Date date=new Date();//当前时间
System.out.printf(date+"");
Calendar类
Calendar calendar=Calendar.getInstance();
System.out.printf(calendar+"");//直接打印出全部信息
常量 | 说明 |
---|---|
Calender.YEAR/MONTH/DATA | 年/月/日 |
Calendar.HOUR/HOUR_OF_MONTH | 时(12小时制)/(24) |
Calendar.MINUTE/SECOND | 分/秒 |
Sanner类
通过Scanner类获取用户的输入
Scanner scanner=new Scanner(System.in);
String s=scan.nextLine();
System.out.println(s);
next()
- 一定要读取到有效字符后才可以结束输入。
- 对有效字符之前输入的空白,next()方法会自动将其去掉。
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next()不能得到带有空格的字符串。
nextLine()
- 以Enter键为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
- 可以获得空白。
DecimalFormat类
DecimalFormat类,可以快速将数字格式化。
double pi=3.1415926;
System.out.printf(new DecimalFormat("0").format(pi));//3
System.out.printf(new DecimalFormat("0.00").format(pi));//3.14
System.out.printf(new DecimalFormat("#.##").format(pi));//3.14
Enum类
public enum EnumText{
a,b,c,d,e,f,g;
}
public static void main(String[] args) {
for (EnumText e :
EnumText.values()) {
System.out.printf(e.toString());
}
}
包装类
Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多不便。为了解决这个问题,在设计类时为每个基本数据类型设计了一个对应的类,8个和基本数据类型对应的类统称为包装类(wrapper class),有时也翻译为外覆类或数据类型类。
int是Java的原始数据类型,Integer是Java为int类型提供的封装类,Java为每个原始类型提供了封装类。