------
Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
Integer类的两个特点及问题:
A:常用包装类主要有Character和Integer
包装类的共有成员变量
public static final int MAX_VALUE 此数据类型的最大值
public static final int MIN_VALUE 此数据类型的最小值<pre name="code" class="java">基于这两个变量可以得出数据类型的取值范围
Character 类在对象中包装一个基本类型 char 的值此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然
* 构造方法:
* Character(char value)<pre name="code" class="java"> * public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
* public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
* public static boolean isDigit(char ch):判断给定的字符是否是数字字符
* public static char toUpperCase(char ch):把给定的字符转换为大写字符
* public static char toLowerCase(char ch):把给定的字符转换为小写字符
Integer类的常用方法
* Integer的构造方法:
* public Integer(int value)
* public Integer(String s)
* 注意:这个字符串必须是由数字字符组成<pre name="code" class="java"> * 常用的基本进制转换
* public static String toBinaryString(int i) 二进制
* public static String toOctalString(int i) 八进制
* public static String toHexString(int i) 十六进制
Integer类的两个特点及问题:
1)JDK5以后可以自动拆装箱,但是必须有初始化值,否则会先空指针异常。
自动拆装箱的代码解析
Integer ii = 100;
ii += 200;
System.out.println("ii:" + ii);
// 通过反编译后的代码
// Integer ii = Integer.valueOf(100); //自动装箱
// ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱
// System.out.println((new StringBuilder("ii:")).append(ii).toString());
* 注意一个小问题:
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,然后再使用。
Integer iii = null;
// NullPointerException
if (iii != null) { //因为已近知道iii=null,所以if判断后的代码就无法执行了(死代码)。
iii += 1000;
System.out.println(iii);
2)-128到127之间的数据缓冲池问题。
Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据,并不创建新的空间
// == 比较的是地址值
// equals 基本包装类都重写了该方法,比较的是内容值
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false
System.out.println(i5.equals(i6));//true
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true
System.out.println(i7.equals(i8));//true
B:StringBuffer类
StringBuffer是一个线程安全的可变字符串,使用其进行拼接字符串不会浪费太多资源。
* StringBuffer的构造方法:
* public StringBuffer():无参构造方法
* public StringBuffer(int capacity):指定容量的字符串缓冲区对象
* public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
常用方法
* public int capacity():返回当前容量。 理论值,默认值是16,当存入字符时自加
* public int length():返回长度(字符数), 实际值 * StringBuffer的添加功能:
* public StringBuffer append(String str)
* 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
*
* public StringBuffer insert(int offset,String str)
* 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
* StringBuffer的删除功能
* public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
* public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身 * StringBuffer的替换功能:
* public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 * StringBuffer的反转功能:
* public StringBuffer reverse()可以使用此方法实现字符串的快速反转 * StringBuffer的截取功能:注意返回值类型不再是StringBuffer本身了
* public String substring(int start)
* public String substring(int start,int end)
面试题:
* 面试题:
* 1:String,StringBuffer,StringBuilder的区别?
* A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。
* B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高
*
* 2:StringBuffer和数组的区别?
* 二者都可以看出是一个容器,装其他的数据。
* 但是呢,StringBuffer的数据最终是一个字符串数据。
* 而数组可以放置多种数据,但必须是同一种数据类型的。
*
* 3:形式参数问题
* String作为参数传递,与基本类型是一样的。
* StringBuffer作为参数传递,属于引用类型的传递。
StringBuffer作为形式参数的代码讲解
public class StringBufferDemo {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println(s1 + "---" + s2);// hello---world
change(s1, s2);
// 基本类型作为形式参数传递,只是把值传递了过去
// 至于方法对值进行怎样的处理,不会改变我的实际值。
System.out.println(s1 + "---" + s2);// hello---world
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
System.out.println(sb1 + "---" + sb2);// hello---world
change(sb1, sb2);
// 引用类型作为形式参数传递,是把地址值传递过去
// 我本身的地址值不会发生改变,但是如果方法对地址值中的数据进行了更改,我的实际数据内容也会发生改变
System.out.println(sb1 + "---" + sb2);// hello---worldworld
}
public static void change(StringBuffer sb1, StringBuffer sb2) {
sb1 = sb2;
sb2.append(sb1);
}
public static void change(String s1, String s2) {
s1 = s2;
s2 = s1 + s2;
}
}
由于StringBuffer是线程安全的,所以效率较低,所以我们一般使用StringBuilder,两者的方法完全能一样。
C:数组(以下方法适用于任意类型的数组)工具类Arrays,常用方法有
* 1:public static String toString(int[] a) 把数组转成字符串
* 2:public static void sort(int[] a) 对数组进行排序
* 3:public static int binarySearch(int[] a,int key) 二分查找,在数组a中查找key的索引,如果不存在返回负数-(length+1)
案例:
public class ArraysDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
// public static String toString(int[] a) 把数组转成字符串
System.out.println("排序前:" + Arrays.toString(arr));
// public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
// [13, 24, 57, 69, 80]
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
}
}
数组工具类方法的源码解析
public static String toString(int[] a)
public static void sort(int[] a) 底层是快速排序,知道就可以了
public static int binarySearch(int[] a,int key)
开发原则:
只要是对象,我们就要判断该对象是否为null。
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr));
public static String toString(int[] a) {
//a -- arr -- { 24, 69, 80, 57, 13 }
if (a == null)
return "null"; //说明数组对象不存在
int iMax = a.length - 1; //iMax=4;
if (iMax == -1)
return "[]"; //说明数组存在,但是没有元素。
StringBuilder b = new StringBuilder();
b.append('['); //"["
for (int i = 0; ; i++) {
b.append(a[i]); //"[24, 69, 80, 57, 13"
if (i == iMax)
//"[24, 69, 80, 57, 13]"
return b.append(']').toString();
b.append(", "); //"[24, 69, 80, 57, "
}
}
-----------------------------------------------------
int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
public static int binarySearch(int[] a, int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//key -- 577
return binarySearch0(a, 0, a.length, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//fromIndex -- 0
//toIndex -- 5
//key -- 577
int low = fromIndex; //low=0
int high = toIndex - 1; //high=4
while (low <= high) {
int mid = (low + high) >>> 1; //mid=2,mid=3,mid=4
int midVal = a[mid]; //midVal=57,midVal=69,midVal=80
if (midVal < key)
low = mid + 1; //low=3,low=4,low=5
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}