包装类
原始数据类型 | 包装类 |
byte(字节) | Byte |
char(字符) | Character |
int (整型) | Integer |
long(长整型) | Long |
float(浮点型) | Float |
double(双精度) | Double |
boolean (布尔) | Boolean |
short(短整型) | Short |
包装类的用法
包装类的方法:ceil()、floor()和round()
代码示例如下:
public class NumberWrap { /** 构造方法 */ protected NumberWrap() { } /** 这是 main 方法 * 它将原始值转换为其相应的包装类型 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { String number = args[0]; Byte byNum = Byte.valueOf(number); Short shNum = Short.valueOf(number); Integer num = Integer.valueOf(number); Long lgNum = Long.valueOf(number); System.out.println("Output"); System.out.println(byNum); System.out.println(shNum); System.out.println(num); System.out.println(lgNum); } }
Character包装类的方法
方法 | 说明 |
isDigit() | 确定字符是否为0至9之间的数字 |
isLetter() | 确定字符是否为字母 |
isLowerCase() | 确定字符是否为小写形式 |
isUpperCase() | 确定字符是否为大写形式 |
isSpace() | 确定字符是否为空格或换行符 |
使用包装类Character的方法
代码示例如下:
public class TestCharacter { public static void main(String[] args) { int count; char[] values = {'*', '7', 'p', ' ', 'P'}; for (count = 0; count < values.length; count++) { if (Character.isDigit(values[count])) { System.out.println(values[count] + “是一个数字"); } if (Character.isLetter(values[count])) { System.out.println(values[count] + “是一个字母"); } if (Character.isUpperCase(values[count])) { System.out.println(values[count] + “是大写形式"); } if(Character.isUnicodeIdentifierStart(values[count])) { System.out.println(values[count] + “是 Unicode " + “标识符的第一个有效字符"); } } } }
String类
String类的构造方法
构造方法 | 说明 |
String() | 它将创建一个空字符串 |
String(String value) | 它将创建一个字符串作为制定字符串的副本 |
String(char [ ] value) | 它将根据字符数组构造一个新字符串 |
String(byte [ ] value) | 它将通过转换指定的字节数组新建一个字符串 |
字符串长度
字符串比较
字符串比较运算符的用法
使用String类的方法,如equals()和==运算符
代码实现如下:
public class Equality { /** 构造方法 */ protected Equality() { } /**它演示两个字符串的比较 * @param args 传递至 main 方法的参数 */ public static void main(String [] args) { String string1 = new String(“苹果是一种水果"); String string2 = new String(“玫瑰花是一种花"); String string3 = new String(“苹果是一种水果"); System.out.println(“字符串 1: " + string1); System.out.println(“字符串 2: " + string2); System.out.println(“字符串 3: " + string3); }
if (string1 == string2) { System.out.println(“字符串 1 和字符串 2 相等"); } else { System.out.println(“字符串 1 和字符串 2 不等"); } if (string1.equals(string3)) { System.out.println(“字符串 1 和字符串 3 相等"); } else { System.out.println("字符串 1 和字符串 2 不等"); } System.out.println(“设置字符串 1 等于字符串 2"); string2 = string1; if (string1.equals(string2)) { System.out.println(“两个字符串相等"); } else { System.out.println(“两个字符串不等"); } } }
字符串比较
方法 | 说明 |
boolean equalslgnoreCase(String value) | 此方法比较两个字符串,忽略大小写形式 |
int compare To(String value) | 按字母顺序比较两个字符串; 如果两个字符串相等,则返回0; 如果字符串在该值之前,则返回值小于0; 如果字符串在该值之后,则返回大于0。 |
boolean startsWith(String value) | 检查一个字符串是否以另一个字符串开始 |
boolean endsWith(String value) | 检查一个字符串是否以另一个字符串结束 |
比较不同的字符串
使用String类的方法:equalsgnoreCase()、compareTo()、startsWith()、endsWith()
代码实现如下:
public class Stringdemo { /** 构造方法 */ protected Stringdemo() { } /** 这是 main 方法 * 它演示 String 类的比较方法 * @param args 传递至 main 方法的参数 */ public static void main(String [] args) { String string1, string2, string3; string1 = new String("Answer"); string2 = new String("ANSWER"); string3 = new String("Question"); System.out.println(“字符串 A 是 " + string1); System.out.println(“字符串 B 是 " + string2); System.out.println(“字符串 C 是 " + string3); } }
if (string1 == string2) { System.out.println(“字符串 A 和字符串 B 指同一个对象"); } else { System.out.println(“字符串 A 和字符串 B 指不同的对象"); } if (string1.equals(string2)) { System.out.println(“字符串 A 和字符串 B 的内容相同"); } else { System.out.println(“字符串 A 和字符串 B 的内容不同"); } if (string1.equalsIgnoreCase(string2)) { System.out.println(“忽略大小写,字符串 A 和 B 的内容相同"); } else if (string1.equalsIgnoreCase(string3)) { System.out.println(“字符串 A 和 B 的内容相同"); } if (string1.compareTo("Answer") == 0) { System.out.println(“按字母,字符串 A 与 Answer 的内容相同"); } if (string1.startsWith("A")) { System.out.println(“以 A 开始"); } } }
搜索字符串
搜索字符串内有无指定的字符或字符串
使用String类的方法:indexOf()
代码实现如下:
public class SearchString { /** 构造方法 */ protected SearchString() { } /** 这是 main 方法 * 它演示在字符串内搜索 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { String name = "[email protected]"; System.out.println(“Email ID 是: " + name); System.out.println(“@ 的索引是:" + name.indexOf('@')); System.out.println(“. 的索引是:" + name.indexOf('.')); if (name.indexOf('.') > name.indexOf('@')) { System.out.println(“该电子邮件地址有效"); } else { System.out.println(“该电子邮件地址无效"); } } }
提取字符串
方法 | 说明 |
public char charAt(int index) | 此方法用于从指定位置提取单个字符,该位置与索引指定,索引中的值必须为非负 |
public String substring(int index) | 此方法用于提取从位置索引开始的字符串部分 |
public String substring(int beginindex,int endindex) | 此方法用于提取 beginindex 和 endindex 位置之间的字符串部分 |
public String concat(String str) | 此方法用于连接两个字符串,并新建一个包含调用字符串的字符串对象 |
public String replace(inchar old,char new) | 此方法用于将调用字符串中出现某个字符的所有位置都替换为另一个字符 |
public String trim() | 此方法用于返回一个前后不含任何空格的调用字符串的副本 |
如何使用字符串提取或字符提取
使用String类的方法:substring()、concat()、replace()和trim()
代码实现如下:
public class StringMethods { /** 构造方法 */ protected StringMethods() { } /** 这是 main 方法 * @param args 传递至 main 方法的参数 */ public static void main(String [] args) { String s = "Java is a " + "platform independent language"; String s1 = "Hello world"; String s2 = "Hello"; String s3 = "HELLO"; System.out.println(s); System.out.println("index of t = " + s.indexOf('t')); System.out.println("last index of t = " +s.lastIndexOf('t')); System.out.println("index of(t, 10) = " +s.indexOf('t‘, 10)); System.out.println(s1.substring(3, 8)); System.out.println(s2.concat("World")); System.out.println(s2.replace('l', 'w')); System.out.println(s1.trim()); } }
更改字符串中字符的大小写
更改字符串中字符的大小写形式
使用String类的方法:toUpperCase()和toLowerCase()
代码实现如下:
public class StringTest { /** 构造方法 */ protected StringTest() { } /** 这是 main 方法 * 它演示字符串的 length() 和 UpperCase() 方法 * @param args 传递至 main 方法 */ public static void main(String [] args) { String name = new String("George"); System.out.println(“姓名是" + name); int length = name.length(); System.out.println(“姓名的长度为 ” + length + “ 个字符"); System.out.println(“姓名用大写形式表示为: "); String nameUppercase = name.toUpperCase(); System.out.println(nameUppercase); } }
StringBuffer类
StringBuffer用于表示可以修改的字符串
使用连接运算符(+)的字符串会自动创建字符串缓冲对象
构造方法 | 说明 |
public StringBuffer() | 保留16个字符的空间 |
public StringBuffer(int length) | 设置缓存器大小 |
public StringBuffer(String value) | 接收字符串参数,用来设置初始内容,并在不重新分配的情况下保留16个字符的空间 |
StringBuffer insert(String s) | 在指定位置插入布尔值的字符串表示 |
int length() | 确定StringBuffe对象的长度 |
void setCharAt(int pos,char ch) | 使用ch指定的新值设置pos指定的位置上的字符 |
String to String () | 转换为字符串形式 |
StringBuffer reverse() | 保留 StringBuffer 对象中的字符 |
StringBuffer delete(int start,int end) | 此方法将删除调用对象中从 start 位置开始直到 end 指定的索引 -1 位置的字符序列 |
StringBuffer deleteCharAt(int pos) | 此方法将删除 pos 指定的索引处的字符 |
StringBuffer replace(int start,int end, String s) | 此方法使用一组字符替换另一组字符。将用替换字符串从 start 指定的位置开始替换,直到 end 指定的位置结束 |
不变性
使用StringBuffer类的方法:append()、insert()、replace()、setCharAt()和toString()
代码实现如下:
public class StringBuf { /** 构造方法 */ protected StringBuf() { } public static void main(String []args) { StringBuffer buf = new StringBuffer("Java"); buf.append(“ Guide Ver1/”); buf.append(3); int index = 5; buf.insert(index, "Student "); index = 23; buf.setCharAt(index, '.'); int start = 24; int end = 25; buf.replace(start, end, "4"); String s = buf.toString(); //转换为字符串 System.out.println(s); } }
Math类
方法 | 说明 |
double sin(double numvalue) | 计算角 numvalue 的正弦值 |
double cos(double numvalue) | 计算角 numvalue 的余弦值 |
double pow(double a, double b) | 计算 a 的 b 次方 |
int abs(int numvalue) | 计算 int 类型值 numvalue 的绝对值,也接收 long、float 和 double 类型的参数 |
double ceil(double numvalue) | numvalue |
double ceil(double numvalue) | 返回大于等于 numvalue 的最大整数值 |
double sqrt(double numvalue) | 计算给定值得平方根 |
int max(int a,int b) | 返回 int 型值 a 和 b 中的较大值,也接收long、 float 和 double 类型的参数 |
int min(int a,int b) | 返回 a 和 b 中的较小值,也接收long、 float 和 double 类型的参数 |
Math类的用法
使用Math类的方法:ceil()、floor()和round()
代码示例如下:
public class MathDemo { /** 构造方法 */ protected MathDemo() { } /** main 方法演示 Math 类的不同方法 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { /** 此变量存储 num 的值*/ int num = 38; /** 该变量存储 num1 的值*/ float num1 = 65.7f; System.out.println(Math.ceil(num)); System.out.println(Math.ceil(num1)); System.out.println(Math.floor(num)); System.out.println(Math.floor(num1)); System.out.println(Math.round(num)); System.out.println(Math.round(num1)); } }
Class类
Class类的用法
使用Class类的方法:getClass()和getSuperClass()
代码实现如下:
public class ClassDemo { /**构造方法 */ protected ClassDemo() { } /** 这个类演示 Class 类的访问方法 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { StoreString objString = new StoreString(); StoreInteger objInteger = new StoreInteger(); Class objClass; objClass = objString.getClass(); System.out.println(“objString 对象的类型是: “ + objClass.getName()); objClass = objInteger.getClass(); System.out.println(“objInteger 对象的类型是: " + objClass.getName()); objClass = objClass.getSuperclass(); System.out.println(“objInteger的父类是" + objClass.getName()); } }
class StoreString { /**构造方法. */ protected StoreString() { } private String name = "diana"; } /** 这个类扩展 StoreString 类.*/ class StoreInteger extends StoreString { /** 构造方法.*/ protected StoreInteger() { } /** 该变量存储整数值. */ private int deptno; }
Object类
所有类的父类
默认情况下,用户定义的类扩展自Object类
方法 | 说明 |
boolean equals(Object obj) | 将当前对象实例与给定的对象进行比较,检查它们是否相等 |
void finalize()throws Throwable | 当垃圾回收器确定不存在该对象的更多引用时,由对象的垃圾回收器调用此方法。通常被子类重写 |
String toString() | 返回此对象的字符串表示 |
void wait ()throws InterruptedException | 使当前线程进入等待状态 |
Object类的用法
使用Object类的方法:equals()
代码实现如下:
/** 这个类演示 Object 类. * @version 1.0, 2005 年 6 月 13 日 * @author Ben */ public class ObjectDemo { /** 构造方法 */ protected ObjectDemo() { } /** 这是 main 方法 * 它演示 Object 类 * @param args 传递至 main 方法的参数 */ public static void main(String[] args) { if (args[0].equals(“Java”)) { System.out.println(“是, Java 是一项非常好的技术!"); } } }
总结
包中的类必须保存在与包同名的文件夹下
默认情况下,会将java.lang包导入到每个Java程序中
包装类可以以类的形式封装简单的原始类型
StringBuffer 类用作构建字符串的构建块
字符串是不可变的,也就是说字符串是常量并且不能改变它们的值
Math 是一个final类,用于定义基本数字运算和三角函数的方法