Java基础突击第二天0005

类属于引用数据类型,一段堆空间可以同时被多个栈内存指向。

class Person{

    String name;   int age;

}

Person per1 = new Person();

Person per2 = per1;

per1.name = "FangXy";   per1.age = 11;

此时per2.name == "FangXy";  per2.age ==11;

per1 = null;  per2 = null;

此时堆内存储的name和age变成了垃圾。等待垃圾收集机制来释放空间。

Java本身提供垃圾收集机制,会不定期的释放不用的内存空间,只要对象不再使用,就会等待GC释放空间。


String可以通过直接赋值和实例化赋值。

String str1 = "Hello World!";

String str2 = new String("Hello World!");

str1 == str2 -> false,==指代的是字符串地址。只有在两个变量指向同一个字符串的时候==才为true;

str1.equals(str2) -> true;

一个字符串就是一个String类的匿名对象(已经开辟了堆内存空间并可以使用的对象)。

直接赋值法:若有一个字符串已经被一个字符串对象引用,以后再有相同的字符串声明时,就不会重新开辟空间。

String str1="Hello",str2 ="Hello",str3 = "Hello";

-> str1 == str2 ==str3

用new String,每一个new都会重新分配一个新的空间。

String str4 =new String("Hello");

因为每个字符串就是一个String类的匿名对象,“Hello”占用了一段内存空间;

且new又开辟了一个“Hello”内存空间。所以上述声明语句相当于开辟了两段内存空间。


字符串的内容一旦声明,不可改变。

String str = "hello"; //在堆内存开辟空间分给hello,将内存地址赋值给str

str = str + "world";  //在堆内存中开辟空间分给world,在堆内存中开辟空间分给“hello world”

//将“hello world”的堆内存地址赋值给str,此时堆内存“hello”“world”变成了垃圾。等待回收。

所以如下代码的性能时很低的。

String str1 = "FangXy";

for(int i=0;i<100;i++){

    str1 +=1;  //每次结合造成两次垃圾,循环100次性能极其低下。需要此种情况时,可考虑StringBuffer类。

}

String常用方法:

构造方法: public String(char[] value)

                  public String(char[] value,int offset,int count) //将指定范围的字符数组变为字符串。

                  public String(byte[] value)

                  public String(byte[] value,int offset,int length) //将指定范围的byte数组变为字符串。

普通方法: public char[]  toCharArray()      //String转换字符数组

                  public char charAt(int index)    //从特定位置取出字符

                  public byte[] getBytes()             //String转换为byte数组

                  public int length()

                  public int indexOf(String str)

                  public int indexOf(String str , int fromIndex)  //从指定位置开始查找指定字符串位置。

                  public String trim()                                          //清除左右两端的空格

                  public String substring(int beginIndex)    //从指定位置开始截取字符串到末尾

                  public String substring(int begin, int end)//指定的首尾位置截取字符串

                  public String[] split(String regex)               //按照指定字符串对字符串进行拆分,regex是正则表达式的意思

                  public String toUpperCase()

                  public String toLowerCase()

                  public boolean startsWith(String prefix)      //判断是否以指定字符串开头

                  public boolean endsWith(String suffix)        //判断是否以指定字符串结尾

                  public boolean equals(String str)

                  public boolean equalsIgnoreCase(String str)  //不区分大小写比较两个字符串是否相等。

                  public String replaceAll(String regex, String replacement)

具体应用如下代码

	        public static void printChar(char array[]){
			for(int i=0;i<array.length;i++){
				System.out.print(array[i]+" ");
			}
			System.out.println();
		}//printChar
		public static void printByte(byte array[]){
			for(int i=0;i<array.length;i++){
				System.out.print(array[i]+" ");
			}
			System.out.println();
		}//printByte
		public static void main(String[] args){
			String str1="Hello",str2 ="Hello";
			String str3 = new String("Hello");
			System.out.println(str1 == str2);
			System.out.println(str1 == str3);

			char c[] = str1.toCharArray();
			printChar(c);
			char cUpper[] = str1.toUpperCase().toCharArray();
			char cLower[] = str1.toLowerCase().toCharArray();
			printChar(cUpper);
			printChar(cLower);

			byte b[] = str1.getBytes();
			printByte(b);

			System.out.println(new String(b));
			System.out.println(new String(b,0,2));
			System.out.println(new String(c));
			System.out.println(new String(c,1,2));

			System.out.println("The length of str1 is "+str1.length());

			int indexL1 = str1.indexOf('l');
			System.out.println("The position of l in hello is "+(indexL1+1));
			int indexL2 = str1.indexOf("l",indexL1+1);
			System.out.println("The position of l in hello is "+(indexL2+1));
			System.out.println("The position of x in hello is "+(str1.indexOf("x")+1));

			String strTrim = "  Hello!  ";
			System.out.println("Original:  Hello!  After trimming:"+strTrim.trim());

			String subToTear = str1.substring(2);
			System.out.println(subToTear);
			String subBeginEnd = str1.substring(2,4); //the end character is not included
			System.out.println(subBeginEnd);

			String strSplit = "Hello WorLd";   //the last L is capital
			String strArray[] = strSplit.split("l");  // "l" is correct,not 'l'
			for(int i = 0;i<strArray.length;i++){
				System.out.print(strArray[i]+"/");
			}
			System.out.println();
			System.out.println("The length of String Array is "+strArray.length);
			//The splited array is "He"  ""  "o WorLd"

			System.out.println(str1.equals("HELLO"));
			System.out.println(str1.equalsIgnoreCase("HELLO"));

			System.out.println(str1.startsWith("h"));  // " " is correct,not ''
			System.out.println(str1.startsWith("H"));
			System.out.println(str1.endsWith("o"));
			System.out.println(str1.endsWith("O"));

		  System.out.println("After replace:"+str1.replaceAll("l","x"));
		}//main

输出:

true
false
H e l l o
H E L L O
h e l l o
72 101 108 108 111
Hello
He
Hello
el
The length of str1 is 5
The position of l in hello is 3
The position of l in hello is 4
The position of x in hello is 0
Original:  Hello!  After trimming:Hello!
llo
ll
He//o WorLd/
The length of String Array is 3
false
true
false
true
true
false
After replace:Hexxo




猜你喜欢

转载自blog.csdn.net/u012144068/article/details/80888888