常用基础类以及类的常用方法
1. 包装类
包装类把基本数据类型的数据封装为引用类型的对象,而且提供了很多有用的方法。
对于Java的基本数据类型,Sun公司均提供了对应的包装类,如下表所示:
2. Integer整数类
Integer是int基本类型的包装类。
2.1 属性
2.2 构造方法
2.3 方法介绍
例子:
//integer compare To方法对两个Integer对象进行大小比较
Integer i = new Integer(1234);
System.out.println("i.compareTo: " + i.compareTo(new Integer(123)));
结果是:
3. Character字符类
Character 类在对象中包装一个基本类型 char 的值;Character 类型的对象包含类型为 char 的单个字段。
3.1 属性
3.2 构造方法
Character(char value):以char参数构造一个Character对象
3.3 方法
所有方法均为public;
书写格式:[修饰符]<返回类型> <方法名([参数列表])
public class CharacterDemo {
public static void main(String[] args) {
Character ch1 = new Character('a');
Character ch2 = new Character('A'); // ch1的char值
System.out.println("char value of ch1 : " + ch1.charValue());
System.out.println("ch1 compare to ch2 : " + ch1.compareTo(ch2)); // 对比ch1和ch2的大小
System.out.println("min value(int) : " + (int) Character.MIN_VALUE); // 表示范围的最小值
System.out.println("max value(int) : " + (int) Character.MAX_VALUE); // 表示范围的最大值
System.out.println("is digit '1' : " + Character.isDigit('1')); // 判断一个字符是否是数字形式
System.out.println("is upper case 'a' : " + Character.isUpperCase('a')); // 判断一个字符是否是大写形式
System.out.println("is space char ' ' : " + Character.isSpaceChar(' ')); // 判断一个字符是否是空格
System.out.println("is letter '1' :" + Character.isLetter('1')); // 判断一个字符是否是字母
System.out.println("is letter or digit '好' :" + Character.isLetterOrDigit('好')); // 判断一个字符是否是字母或数字
System.out.println("to upper case 'a' :" + Character.toUpperCase('a')); // 把字母转化为大写形式
}
}
结果是:
4. String字符串类
字符串大量用于我们的程序中,Java提供了String类
专门用于表示字符串,内有大量实用的方法。
4.1 构造方法
4.2 方法
实例:
String result = "ILoveYou";
System.out.println(result.charAt(1));
System.out.println(result.charAt(2));
System.out.println(result.charAt(3));
System.out.println(result.charAt(4)); //通过字符的下标,返回字符
System.out.println(result.concat("Baby")); //拼接字符
System.out.println(result.indexOf("L")); //找字符的下标
System.out.println(result.length()); //字符的长度
System.out.println(result.replace("I","Y")); //以旧换新,字符串替换
System.out.println(result.substring(0,8)); //字符串的截取
System.out.println(result.toLowerCase()); //字符串小写的转换
System.out.println(result.toUpperCase()); //字符串大写的转换
// 字符不变性
String W = new String("Hello");
W = W.replace('H','o'); //赋值之后能改变字符串的内容
System.out.println("W = "+W);
结果是:
5. StringBuffer字符串缓冲类
在实际应用中,经常会遇到对字符串进行动态修改。这时候,String类的功能受到限制,而StringBuffer类可以完成字符串的动态添加、插入和替换等操作,对StringBuffer的修改,是直接的,不会创建多余的对象。
5.1 构造方法
5.2 方法
String question = new String("1+1=");
int answer = 2;
boolean result = (1 + 1 == 2);
StringBuffer sb = new StringBuffer();
sb.append(question);
sb.append(answer);
sb.append(result);
sb.insert(5, ',');
System.out.println(sb);
}
结果是:
6. Random随机类
此类用来产生随机数,位于java.util包中,所以需要手工导包。
6.1 构造方法
6.2 方法
手动导包
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random r1 = new Random(50);
System.out.println("第一个种子为50的Random对象");
System.out.println("r1.nextBoolean():\t" + r1.nextBoolean());
System.out.println("r1.nextInt():\t\t" + r1.nextInt());
System.out.println("r1.nextDouble():\t" + r1.nextDouble());
System.out.println("r1.nextGaussian():\t" + r1.nextGaussian());
}
}
结果是:
7. Date时间类
Java在日期类中封装了有关日期和时间的信息,类 Date 表示特定的瞬间,精确到毫秒。用户可以通过调用相应的方法来获取系统时间或设置日期和时间。
7.1 构造方法
7.2 方法
System.out.println(new Date()); //当前外国的时间
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
System.out.println(sdf.format(new Date())); //格式化为当前我们看习惯的时间
结果是:
8. Math算术运算类
Math类中的静态方法帮助我们完成基本的数学运算。
它的定义形式为:
public final class Math extends Object
可以看出,Math类是一个最终类,也就是说,它不能被继承,更不能被重写,Sun公司的牛人们不乐意看到它被别人改动,Math类中的方法全部为静态方法,直接使用就行。
实例:
public static void main(String[] args) {
System.out.println("abs of -1 : " + Math.abs(-1)); // 绝对值
System.out.println("ceil of 9.01 : " + Math.ceil(9.01)); // 比这个数大的最小整数
System.out.println("floor of 9.99 :" + Math.floor(9.99)); // 比这个数小的最大整数
System.out.println("the max is : " + Math.max(101, 276.001)); // 取较大者
System.out.println("random number : " + Math.random()); // 随机数,区间为[0.0,1.0)
System.out.println("round value of 9.49 :" + Math.round(9.49)); // 四舍五入
System.out.println("square root of 225 : " + Math.sqrt(225)); // 返回正确舍入的 double 值的正平方根
}
结果是: