3.4 特殊类
本节中将介绍几个常用类和他们的主要方法。
3.4.1 String类
直接赋值 |
String str = "Hello World" ;//一旦定义不可改变,否则会产生垃圾内存 |
构造赋值 |
String str = new String("Hello World") ;//不推荐,会产生垃圾内存 |
字符串比较 |
str1.equals(str2); |
常用方法
分类 |
方法名 |
效果 |
字符(数组)与字符串 |
public String(char value[]) |
构造方法,将字符数组转换为字符串 |
public String (char value[],int offset,int count) |
构造方法,将value[offset]到value[offset+count]之间的所有字符转换为字符串 | |
public char charAt(int index) |
将字符串中指定位置的字符去除,index从0开始计算 | |
public char[] toCharArray() |
字符串转换为数组 | |
字符串比较 |
public boolean equals(Object anObject) |
区分大小写的字符串比较 |
public boolean equalsIgnoreCase(String anotherString) |
不区分大小写的字符串比较 | |
public int compareTo(String anotherString) |
比较两个字符串的大小关系 | |
字符串查找 |
public Boolean contains(CharSequence s) |
判断子串是否存在 |
public int indexOf(String str[, int fromindex]) |
从头[从指定位置]开始查找指定字符串的位置,找到返回索引,反之返回-1 | |
public int lastIndexOf(String str[, int fromindex]) |
从结尾[从指定位置]反向查找 | |
public boolean stratsWith(String str[, int toffset]) |
判断[从指定位置算起]是否以指定的字符串开头 | |
public boolean endsWith(String str) |
判断是否以指定的字符串结尾 | |
其他 |
public int length() |
获取字符串长度 |
public Boolean isEmpty() |
判断是否为空字符串 (长度为0的字符串) |
public class Example{ String str=new String("tarena"); char[]ch={'a','b','c'}; public static void main(String args[]){ Example ex=new Example(); ex.change(ex.str,ex.ch); System.out.print(ex.str+" and "); System.out.print(ex.ch); } public void change(String str,char ch[]){ //引用类型变量,传递的是地址,属于引用传递。 str="test ok"; ch[0]='g'; } } |
正确答案: B tarena and abc tarena and gbc test ok and abc test ok and gbc |
3.4.2 StringBuffer/StringBuilder类
String与StringBuffer/StringBuilder类在操作上几乎完全一致,甚至方法都是公用的,但是String类的内容一旦修改,则修改的是栈内存的指向而已,并不会直接修改堆内存的内容,而StringBuffer/StringBuilder类的内容可以修改,在频繁需要修改字符串的情况下考虑使用StringBuffer,而StringBuilder存在线程安全问题不推荐使用。
StringBuffer中新增加的方法
public synchronized StringBuffer reverse() |
字符串反转 |
public synchronized StringBuffer delete(int start, int end) |
删除索引从start到end之间的内容 |
public synchronized StringBuffer insert(int offset, 各种数据类型 b) |
字符串插入 |
3.4.3 Object类
Object是Java默认提供的一个类。Java里面除了Object类,所有的类都是存在继承关系的。默认会继承Object父类。即,所有类的对象都可以使用Object进行接收。
所以在开发之中,Object类是参数的最高同一类型。但是Object类也存在有定义好的一些方法。
在使用对象直接输出的时候,默认输出的是一个地址编码。如果现在使用的是String类,该类对象直接输出的是内容,主要原因就是toString()方法的问题。
默认Object类提供的toString()方法只能够得到一个对象的地址(而这是所有对象都共同具备的特征)。如若觉得默认给出的toString()方法功能不足,就在需要的子类上覆写toString()方法。
toString()的核心目的在于取得对象信息。
String作为信息输出的重要数据类型,在Java中所有的数据类型只要遇见了String并且执行了 + ,那么都要求将其变为字符串后连接,而所有对象要想变为字符串就默认使用 toString() 方法。
String类对象的比较使用的是equals()方法,实际上String类的equals()方法就是覆写的Object类中的equals()方法。
Object是所有类的父类,但是Obejct并不局限于此,它可以接收所有数据类型,包括:类、数组、接口。而Object可以接收接口是Java中的强制要求,因为接口本身不能继承任何类。
下面不属于Object类中方法的是: |
正确答案: B hashCode() finally() wait() toString() |
3.4.4 包装类
Object类可以接受所有的引用数据类型,而对于基本数据类型的处理则需要利用到包装类。
所谓的包装类就是将基本数据类型封装到类中。Java为了方便开发,专门提供了包装类的使用,而对于包装类的使用,提供了两种类型。
对象型(Object的直接子类):Boolean、Character(char);
数值型(Number的直接子类): Byte、Double、Short、Long、Integer(int)、Float;
在包装类的使用上有两个概念:
装箱:将基本数据类型变为包装类对象,利用每一个包装类提供的构造方法实现装箱处理。
拆箱:将包装类中包装的基本数据类型取出。利用Number类中提供的六种方法。
Integer num = new Integer(55) ; // 装箱 int data = num.intValue() ; // 拆箱 System.out.println(data); |
那么包装类之间的比较是如何实现的?
Integer num1 = new Integer(10) ; Integer num2 = new Integer(10) ; System.out.println(num1 == num2); System.out.println(num1 == new Integer(10)); System.out.println(num1.equals(new Integer(10))); |
public class HelloWorld{ public static void main(String []args){ Integer i1=127, i2=127, i3=128, i4=128; System.out.println(i1==i2); System.out.println(i1.equals(i2)); System.out.println(i3==i4); System.out.println(i3.equals(i4)); } }
|
false true true true true false true true true true false true true true true false |
对于 Integer var = ? 在-128 至 127 范围内的赋值,Integer 对象是在IntegerCache.cache 产生,会复用已有对象,这个区间内的 Integer 值可以直接使用==进行判断,但是这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象,这是一个大坑,推荐使用 equals 方法进行判断。
字符串与基本数据类型转换
以后要进行各种数据的输入,一定都是字符串类型的接收。所以在开发之中,如何将字符串变为各个数据类型,这个时候就需要包装类支持。
1. String变为int 类型(Integer类):public static int parseInt(String s) throws
NumberFormatException
2. String变为double类型(Double类):public static double parseDouble(String s) throws
NumberFormatException
3. String变为Boolean类型(Boolean类):public static boolean parseBoolean(String s)