JavaSE第三十一讲:Java数组剖析

复习前面所讲内容:

1):String Pool(字符串池)Java中需要维护字符串池的是因为在实际开发中经常会遇到String,所以不用每次都在heap中去生成一个对象,而且这个对象用完一般都是丢掉的,所以需要维护字符串池,而且一般字符串写法用这种形式:String str = "aaa"。

2):StringBuffer类,String不同的是,这个可以加字符串的内容,而String一旦生成则不会被改变。

public final class StringBuffer
extends Object
implements Serializable, CharSequence
A thread-safe, mutable sequence of characters. A string buffer is like aString, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

查看StringBuffer类同时以下例子。
查看StringBuffer类构造函数:选择不带参数的构造函数。
Constructor and Description
StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer(CharSequence seq)
Constructs a string buffer that contains the same characters as the specified CharSequence.
StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.
StringBuffer(String str)
Constructs a string buffer initialized to the contents of the specified string.

查看String Buffer类中的append(): 选择以下方法,它返回的也是一个StringBuffer类型。
StringBufferappend(String str)
Appends the specified string to this character sequence.

选择StringBuffer类中的toString(),将序列中的数据 [append()方法调用的追求内容的数据] 返回成一个字符串。
StringtoString()
Returns a string representing the data in this sequence.

public class StringBufferTest{
	public static void main(String[] args){
		StringBuffer buffer = new StringBuffer();
		buffer.append("hello").append(" world").append(" welcome");

		//以下这种写法append()方法追加内容的写法也是一样的,注意它返回的也是StringBuffer类型。通常用上面这种写法。
		//buffer = buffer.append("hello");
		//buffer.append(" world");
		//buffer.append(" welcome"); 

		String result = buffer.toString();
		System.out.println(result);
	}
}
编译执行结果:
D:\src>java StringBufferTest
hello world welcome

String类:
The String class represents character strings. All string literals in Java programs, such as"abc", are implemented as instances of this class.
StringBuffer类:
A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String类不可被修改,StringBuffer类可以被修改


查看JDK文档,StringBuffer类被重写的方法append()还可以接受int,boolean. String类的字符串拼接操作如下例子所示。
public class StringBufferTest{
	public static void main(String[] args){
		StringBuffer buffer = new StringBuffer();
		buffer.append("hello").append(" world").append(" welcome").append(100).append(false);

		//以下这种写法append()方法追加内容的写法也是一样的,注意它返回的也是StringBuffer类型。通常用上面这种写法。
		//buffer = buffer.append("hello");
		//buffer.append(" world");
		//buffer.append(" welcome"); 

		String result = buffer.toString();
		System.out.println(result);

		String s = "abc";
		int a = 100;
		boolean b = true;

		String str = a + s + b;
		System.out.println(str);
	}
}
编译执行结果:
D:\src>java StringBufferTest
hello world welcome100false
100abctrue

		int m = 100;
		int n = 200;
		System.out.println(m + n);//输出300
		System.out.println(100 + 200);//输出300
		System.out.println("100" + 200);//输出100200
注意第三个输出,因为100加上引号则不是整了,是一个String了,凡是和字符串进行拼接操作的,都会将非String转化为String,然后与字符串进行拼接。

		System.out.println(false + true);//变态程序,出错
		System.out.println("false" + true); //输出为falsetrue

查看 TheJava™ Language Specification 3.10.5 String Literals 字符串字面值的拼接
package testPackage;
class Test {
    public static void main(String[] args) {
        String hello = "Hello", lo = "lo"; //hello为池中生成的对象
        System.out.print((hello == "Hello") + " ");  //String Pool池中的对象与字符串匹配相等
        System.out.print((Other.hello == hello) + " ");
        System.out.print((other.Other.hello == hello) + " ");
        System.out.print((hello == ("Hel"+"lo")) + " "); //如果是两个常量进行拼接,则为真
        System.out.print((hello == ("Hel"+lo)) + " "); //如果常量和字符串变量凭借会生成一个新的字符串对象,比较为假
        System.out.println(hello == ("Hel"+lo).intern()); //如果调用intern()方法不管是否哪里的对象都会返回池中的那个对象。
    }
}
class Other { static String hello = "Hello"; }

输出结果:
true true true true false true

1. 包装类(Wrapper Class)。针对于原生数据类型的包装。所有的包装类(8个)都位于java.lang包下。Java中的8个包装类分别是:Byte, Short, Integer, Long, Float, Double, Character, Boolean。他们的使用方式都是一样的,可以实现原生数据类型与包装类型的双向转换。

包装类的作用:Java中就属于这八个原生数据类型不属于对象,或者说是引用类型,剩下的都是对象,或者引用类型,此时会产生一些问题,比如说我想接受一个引用类型的,但是这个时候正好又只有整[传递一个整数],此时就没法传递过去,为了更加的使程序更加面向对象,Java就提供了包装类,意思就是将八个整形包装起来放在包装类中,就可以按照通常对对象的那种方式来操作包装类了。


掌握其中一个包装类,其他的就很好解决了,查看JDK文档java.lang.Integer。

public final class Integer
extends Number
implements Comparable<Integer>
The Integer class wraps a value of the primitive typeint in an object. An object of type Integer contains a single field whose type isint.

查看其构造方法,可以将其int类型转化对于的包装类。

Integer(int value)

Constructs a newly allocated Integer object that represents the specified int value.
然后就可以调用包装类Integer的方法了,可以调用intValue()方法,
将包装类中的int值取出。
intintValue()
Returns the value of this Integer as an int.

//Integer在lang包下,所以不需要导入了
public class IntegerTest{
	public static void main(String[] args){
		int a = 10;
		Integer integer = new Integer(a);

		int b = integer.intValue();

		System.out.println(a == b);

	}
}
编译执行结果:
D:\src>java IntegerTest
true

2. 数组(Array):相同类型数据的集合就叫做数组。 

3. 如何定义数组。

type[] 变量名 = new type[数组中元素的个数];

可以按照下列方式定义长 度为 10 的数组: 

    int[] a = new int[10];

或者 int a[] = new int[10]; 

4. 数组中的元素索引是从0开始的。对于数组来说,最大的索引==数组的长度  1。 

5. 定义数组的第3种方式:

type[] 变量名 = {new type[]}{逗号分隔的初始化值列表}; 

6. Java 中的每个数组都有一个名为length的属性,表示数组的长度。length属性是public, final,int的。数组长度一旦确定,就不能改变大小。 

7. int[] a = new int[10],其中a是一个引用,它指向了生成的数组对象的首地址,数组中每个元素都是int类型,其中仅存放数据值本身。 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值