您的位置:首页 > 编程语言 > Java开发

java自动装拆箱、Random类、Arrays类常用方法

2016-07-01 22:40 323 查看

自动装箱与拆箱概念

装箱通俗易懂说就是把基础类型封装成一个类。比如把int封装成Integer,这时你就不能把他当成一个数了,而是一个类了,对他的操作就需要用它的方法了。拆箱就是把类转换成基础类型。比如你算个加法什么的是不能用类的,就得把它转换成基本类型。首先来看一个demo:

Integer data1 = new Integer(10);
Integer data2 = new Integer(10);


用类的构造函数把基本数据类型转换为对象

Integer data3 = Integer.valueOf(10);
Integer data4 = Integer.valueOf(10);


通过类的静态方法把基本类型转换成对象

Integer data5 = 20;
Integer data6= 20;


基本数据类型自动转化为对象

String str = "12306";
Integer t2 = new Integer(str);
System.out.println(t2);


输出是12306。

float e = 11.8f;
Float x = new Float(e);
System.out.println(x);


输出是11.8。

public class Main {
public static void main(String[] args) {

Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;

System.out.println(i1==i2);
System.out.println(i3==i4);
}
}


输出是true false。为什么会出现这样的结果?输出结果表明i1和i2指向的是同一个对象,而i3和i4指向的是不同的对象。此时只需一看源码便知究竟,下面这段代码是Integer的valueOf方法的具体实现:

private static class IntegerCache {
static final int high;
static final Integer cache[];

static {
final int low = -128;

c6f0
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h;

cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}

private IntegerCache() {}
}


从上面源码我们可以知道:在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。需要注意的是:Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。而Double、Float的valueOf方法的实现是类似的,与前者不一样。

在很多很是时候会遇到“==”问题,这时候也是与自动拆装箱是联系的,具体原则是:当 “==”运算符的两个操作数都是 包装器类型的引用时,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。用下面代码验证一下:

Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 364;
Integer f = 364;
Long g = 3L;
Long h = 2L;
System.out.println(c==d);//true
System.out.println(e==f);//false
System.out.println(c==(a+b));//true
System.out.println(c.equals(a+b));//true
System.out.println(g==(a+b));//true
System.out.println(g.equals(a+b));//false
System.out.println(g.equals(a+h));//true


c.equals(a+b)会先触发自动拆箱过程,再触发自动装箱过程,也就是说a+b会先各自调用intValue方法,得到了加法运算后的数值之后,便调用Integer.valueOf方法,再进行equals比较。注意一下:g.equals(a+h),先拆箱是a与h相加后装箱后的调用的是Long.valueof方法。这个过程是先拆箱后装箱的操作顺序。

Random类类的常用方法

随机对象在程序设计中使用比较广泛,需要在不同情况下产生符合条件的随机数。

Random rnd = new Random();


创建随机数对象

int a = rnd.nextInt(10);
int b = rnd.nextInt(10) + 5;
int c = 3 * rnd.nextInt(10);


用随机数对象生成3个整形变量。在许多小游戏或者概率试验中都可以使用随机数模拟实验。

Arrays类的常用方法

int[] a = new int[] { 6, 34, 21, 11, 23, 56, 65, 33, 89, 90 };
Arrays.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "  ");
}
System.out.println("\n65的位置为:" + Arrays.binarySearch(a, 65));
System.out.println("33的位置为:" + Arrays.binarySearch(a, 33));


输出如下:

6  11  21  23  33  34  56  65  89  90
65的位置为:7
33的位置为:4


array类比较常用的就是升序排序sort方法,一般来说使用Arrays.binarySearch()方法查找相应的元素是在排序好的数组上才有意义,binarySearch()方法可以找出数组元素在数组的位置。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: