目标
java.lang包简介
包装类
int num = 10;
这里的num只是一个变量,而不是对象;
原始数据类型和包装类对照表
原始数据类型 | 包 装 类 |
boolean(布尔型) | Boolean |
byte(字节型) | Byte |
char(字符型) | Character |
short(短整型) | Short |
int(整型) | Integer |
long(长整型) | Long |
float(浮点型) | Float |
double(双精度浮点型) | Double |
包装类的构造方法
- public class LangDemo
- {
- public static void main(String[] args)
- {
- Boolean objBool = new Boolean(true);
- Character objChar = new Character('X');
- Integer objInt = new Integer(100);
- Long objLong = new Long(2568);
- Double objDou = new Double(3.1415);
- System.out.println(objBool);
- System.out.println(objChar);
- System.out.println(objInt);
- System.out.println(objLong);
- System.out.println(objDou);
- }
- }
包装类的valueOf方法
- public class LangDemo {
- public static void main(String[] args) {
- String str = "120";
- //如果转换失败,将会引发NumberFormatException异常
- Byte objByte = Byte.valueOf(str);
- Short objShort = Short.valueOf(str);
- Integer objInt = Integer.valueOf(str);
- Long objLong = Long.valueOf(str);
- System.out.println(objByte);
- System.out.println(objShort);
- System.out.println(objInt);
- System.out.println(objLong);
- }
- }
包装类的parseXxx方法
- public class ParseTest {
- public static void main(String[] args) {
- String str = "116";
- //分别调用各个包装类的paseXxx方法对字符串进行转换,如果转换失败,将报异常
- int i = Integer.parseInt(str);
- short s = Short.parseShort(str);
- byte b = Byte.parseByte(str);
- long l = Long.parseLong(str);
- float f = Float.parseFloat(str);
- double d = Double.parseDouble(str);
- System.out.println(i);
- System.out.println(s);
- System.out.println(b);
- System.out.println(l);
- System.out.println(f);
- System.out.println(d);
- }
- }
Character类中的常用方法
方 法 原 型 | 说 明 |
boolean isLetter(char ch) | 判断字符ch是否为英文字母 |
boolean isDigit(char ch) | 判断字符ch是否为0~9之间的数字 |
boolean isUpperCase(char ch) | 判断字符ch是否为大写形式 |
boolean isLowerCase(char ch) | 判断字符ch是否为小写形式 |
boolean isWhitespace(char ch) | 判断字符ch是否为空格或换行符 |
以上方法都是静态方法,可以直接通过类名调用,返回值均
为boolean类型,如果是返回true,否则返回false。
Character类常用方法示例
- public class CharacterDemo {
- public static void main(String[] args) {
- char[] charArray = {'*', '7', 'b', ' ', 'A'};
- for (int i = 0; i < charArray.length; i++) {
- if (Character.isDigit(charArray[i])) {
- System.out.println(charArray[i] + "是一个数字。");
- }
- if (Character.isLetter(charArray[i])) {
- System.out.println(charArray[i] + "是一个字母。");
- }
- if (Character.isWhitespace(charArray[i])) {
- System.out.println(charArray[i] + "是一个空格。");
- }
- if (Character.isLowerCase(charArray[i])) {
- System.out.println(charArray[i] + "是小写形式。");
- }
- if (Character.isUpperCase(charArray[i])) {
- System.out.println(charArray[i] + "是大写形式。");
- }
- }
- }
- }
String类
String类的构造方法
构造方法 | 说 明 |
String() | 将创建一个空字符串 |
String(String original) | 将新建一个字符串作为指定字符串的副本 |
String(char[] value) | 将根据字符数组构造一个新字符串 |
String(byte[] tytes) | 将通过转换指定的字节数组新建一个字符串 |
String类构造方法示例
- public class StringDemo
- {
- public static void main(String[] args)
- {
- char[] aryChar = {‘I', 'C', ‘S', ‘S'};
- String str1 = “ETC";
- //利用一个字符串常量值创建新的字符串
- String str2 = new String(“ICSSETC");
- //利用一个字符型数组创建新的字符串
- String str3 = new String(aryChar);
- System.out.println(str1);
- System.out.println(str2);
- System.out.println(str3);
- }
- }
字符串长度
- public class StringDemo
- {
- public static void main(String[] args)
- {
- String str1 = "John Smith";
- String str2 = new String("I Love Java");
- System.out.println(str1.length());
- System.out.println(str2.length());
- }
- }
字符串比较
boolean equals(Object anObject)
如果相等返回true,否则返回false。
字符串比较示例
- public class StringDemo {
- public static void main(String[] args) {
- String str1 = “ICSS", str2 = “ICSS";
- String str3 = new String(“ETC"), str4 = new String(“ETC");
- if (str1 == str2) {
- System.out.println("str1和str2指向同一字符串"); }
- else {
- System.out.println("str1和str2分别指向不同字符串"); }
- if (str1.equals(str2)) {
- System.out.println("str1和str2的内容完全相同"); }
- else {
- System.out.println("str1和str2的内容不相同"); }
- if (str3 == str4) {
- System.out.println("str3和str4指向同一字符串"); }
- else {
- System.out.println("str3和str4分别指向不同字符串"); }
- if (str3.equals(str4)) {
- System.out.println("str3和str4的内容完全相同");}
- else {
- System.out.println("str3和str4的内容不相同"); }
- }
- }
其它的比较方法
方 法 原 型 | 说 明 |
boolean equalsIgnoreCase(String anotherString) | 判断字符串anotherString是否与当前字符串相等,忽略大小写形式 |
int compareTo(String anotherString) | 根据ASCII码比较字符串anoterString和当前字符串的大小,比较方式类似于C语言中的strcmp函数 |
boolean startsWith(String prefix) | 判断当前字符串是否以字符串prefix为开头 |
boolean endsWith(String suffix) | 判断当前字符串是否以字符串suffix为后缀 |
字符串搜索
方 法 原 型 | 说 明 |
int indexOf(int ch) | 搜索字符ch在当前字符串中第一次出现的索引,没有出现则返回-1 |
int indexOf(String str) | 搜索字符串str在当前字符串中第一次出现的索引,没有出现则返回-1 |
int lastIndexOf(int ch) | 搜索字符ch在当前字符串中最后一次出现的索引,没有出现则返回-1 |
int lastIndexOf(String str) | 搜索字符串str在当前字符串中最后一次出现的索引,没有出现则返回-1 |
字符串搜索示例
- public class StringDemo
- {
- public static void main(String[] args)
- {
- String strEmail = "java@sun.com";
- int index;
- System.out.println("E-mail地址:" + strEmail);
- index = strEmail.indexOf('@');
- System.out.println("@字符出现的索引:" + index);
- index = strEmail.indexOf("sun");
- System.out.println("字符串\"sun\"出现的索引:" + index);
- index = strEmail.lastIndexOf('a');
- System.out.println("a字符最后一次出现的索引:" + index);
- }
- }
提取字符串
方 法 原 型 | 说 明 |
char charAt(int index) | 用于从指定位置提取单个字符,该位置由index指定,索引值必须为非负 |
String substring(int index) | 用于提取从index指定的位置开始的字符串部分 |
String substring(int begin, int end) | 用于提取 begin 和 end 位置之间的字符串部分 |
String concat(String str) | 用于连接两个字符串,并新建一个包含调用字符串的字符串对象 |
String replace(char oldChar, char newChar) | 用于将调用字符串中出现oldChar指定的字符全部都替换为newChar指定的字符 |
replaceAll(String regex, String replacement) | 用于将调用字符串中出现或者匹配regex的字符串全部都替换为replacement指定的字符 |
String trim() | 用于返回一个前后不含任何空格的调用字符串的副本 |
提取字符串示例
- public class StringDemo
- {
- public static void main(String[] args)
- {
- String str1 = " Java is OOP";
- String str2 = new String(“icss");
- System.out.println(str1.charAt(2));
- System.out.println(str1.substring(5));
- System.out.println(str1.substring(2, 9));
- System.out.println(str1.concat(str2));
- System.out.println(str1 + str2);
- System.out.println(str1.replace('a', 'e'));
- System.out.println(str1.trim());
- }
- }
更改字符串的大小写形式
方 法 原 型 | 说 明 |
String toUpperCase() | 返回当前字符串的全大写形式 |
String toLowerCase() | 返回当前字符串的全小写形式 |
更改大小写形式示例
- public class StringDemo
- {
- public static void main(String[] args)
- {
- String str1 = "Java is OOP";
- String str2;
- str2 = str1.toLowerCase();
- System.out.println(str2);
- str2 = str1.toUpperCase();
- System.out.println(str2);
- }
- }
数据格式转化
方 法 原 型 | 说 明 |
byte[] getBytes() | 返回当前字符串转化成byte型数组的形式(即字符串在内存中保存的最原始的二进制形态, 流的应用) |
char[] toCharArray() | 返回当前字符串的字符数组形式,类似于C语言中字符串的保存形式 |
StringBuffer类
构造方法 | 说 明 |
StringBuffer() | 创建一个空的StringBuffer对象,默认保留16个字符的缓冲空间 |
StringBuffer(String str) | 根据字符串str的内容创建StringBuffer对象,并默认保留 16 个字符的缓冲空间 |
StringBuffer(int capacity) | 创建一个空的StringBuffer对象,缓冲空间大小由capacity指定 |
StringBuffer类的常用方法
方 法 原 型 | 说 明 |
StringBuffer insert(int index, x x) | 将x插入到索引为index的位置,x可以为任何类型的数据 |
int length() | 获得当前StringBuffer对象的长度 |
void setCharAt(int index, char ch) | 使用 ch 指定的新值替换 index指定的位置上的字符 |
String toString() | 转换为字符串形式 |
StringBuffer reverse() | 将当前StringBuffer对象中的字符序列倒置 |
StringBuffer delete(int start, int end) | 删除当前对象中从start位置开始直到 end 指定的索引 位置的字符序列 |
StringBuffer deleteCharAt(int index) | 将删除 index 指定的索引处的字符 |
StringBuffer replace(int start, int end, String str) | 此方法使用一组字符替换另一组字符。将用替换字符串从 start指定的位置开始替换,直到 end 指定的位置结束 |
StringBuffer示例
- public class StringBufferDemo {
- public static void main(String[] args) {
- StringBuffer strBuf = new StringBuffer("Java");
- strBuf.append(“ Guide Ver1/”); //连接 Java Guide Ver1/
- System.out.println(strBuf); //Java Guide Ver1/
- strBuf.append(3);
- System.out.println(strBuf); //Java Guide Ver1/3
- strBuf.insert(5, "Student"); //插入
- System.out.println(strBuf); //Java StudentGuide Ver1/3
- strBuf.setCharAt(20, '.'); //替换字符
- System.out.println(strBuf); //Java StudentGuide Ve.1/3
- strBuf.reverse(); //倒序
- System.out.println(strBuf);
- String str = strBuf.toString();
- System.out.println(str);
- }
- }
区别(面试常规题)
String练习
•对一个字符串数组进行排序.
Math类
Math类的常用方法
方 法 原 型 | 说 明 |
static int abs(int a) | 求a的绝对值,有4种重载,还有float,double和long |
static double pow(double a, double b) | 求a的b次方幂 |
static double sqrt(double a) | 求a的平方根 |
static int round(float a) | 求a的四舍五入结果 |
static double ceil(double a) | 返回不小于a的最小整数值 |
static double floor(double a) | 返回不大于a的最大整数值 |
static double sin(double a) | 返回a的正弦值 |
static double cos(double a) | 返回a的余弦值 |
Math类中的常量
Object类
Object类的常用方法
方 法 原 型 | 说 明 |
boolean equals(Object obj) | 判断当前对象是否与参数obj(内容)相等,如果有必要,应该在自定义的类中覆盖该方法 |
String toString() | 返回当前对象的字符串表示,如果有必要,应该在自定义的类中覆盖该方法 |
Class getClass() | 返回当前对象的类描述对象,此方法被继承到所有类中 |
protected void finalize() throws Throwable | 当前对象被垃圾回收时调用此方法(类似于C++的析构函数),但无法确定具体何时调用 |
public final void wait() throws InterruptedException | 使当前线程进入等待状态 |
toString方法示例
- class Student { //定义Student类,缺省继承于Object类
- private String mName;
- private int mAge;
- public Student(String name, int age) { //构造方法
- mName = name;
- mAge = age;
- }
- public String toString() { //覆盖Object类中的toString方法
- String str = "姓名:" + mName + ", 年龄:" + mAge + "岁";
- return (str);
- }
- }
- public class ToStringDemo { //容纳main方法
- public static void main(String[] args) {
- Student std = new Student("张三", 18);
- System.out.println(std); //默认调用toString方法
- }
- }
Class类
Class类的常用方法
方 法 原 型 | 说 明 |
static Class forName(String className) throws ClassNotFoundException | 使用参数className来指定具体的类,来获得相关的类描述对象,该方法有可能抛出类加载异常(ClassNotFoundException),必须捕捉 |
Class getSuperclass() | 获得当前类描述对象的父类的描述对象 |
String getName() | 返回当前类描述对象的类名称 |
类描述对象示例
- /*该示例可以检索任意类的继承关系*/
- public class ClassDemo {
- public static void main(String[] args) {
- try {
- /*使用forName方法获得任意一个类的类描述对象
- 这里以StringBuffer类为例
- forName方法有可能抛异常,必须捕捉*/
- Class cls = Class.forName("java.lang.StringBuffer");
- //循环打印父类信息,直到没有父类
- while (cls != null) {
- System.out.println(cls);
- cls = cls.getSuperclass();
- }
- } catch (ClassNotFoundException cnfe) {
- cnfe.printStackTrace();
- }
- }
- }
总结
补充学习资料
作业:从一段话中查询出最长的单词
作业
1.
2.
提示: 重写toString()方法实现
重写equals()方法实现
3.
4.
提示: Integer类的toBinaryString(), toHexString(),toOctalString()