Java String Initialization - Java 147

Java String Initialization – Java 147

Java String Initialization

字串初始化的方式,在 Java 中,字串是不可變的物件,一旦建立後就不能改變,使用 new 關鍵字建立字串,在這種情況下,JVM 將在普通堆記憶體中建立一個新的字串物件,並且文字將被放置在字串池中,變數將引用堆中的物件, String Java Initialization 初始化字串,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- string
       |                   +- StringInitializationTest.java   

單元測試

Initialization of String 提供初始化建立字串。

withLiteral

使用文字,建立一個 String 。

	@Test
	public void withLiteral() {
		String value = "Assertj";
		System.out.println(value);
		assertEquals("Assertj", value);

		String value2 = "155";
		System.out.println(value2);
		assertEquals("155", value2);
	}
Assertj
155

withNew

Initialization of String 使用 new 關鍵字,建立一個 String 。

	@Test
	public void withNew() {
		String value = new String("Assertj");
		System.out.println(value);
		assertEquals("Assertj", value);

		String value2 = new String("155");
		System.out.println(value2);
		assertEquals("155", value2);
	}
Assertj
155

withByteArray

Initialization of String 使用 byte 陣列,建立一個 String 。

	@Test
	public void withByteArray() {
		byte[] value = new byte[] { 65, 115, 115, 101, 114, 116, 106 };
		String result = new String(value);
		System.out.println(result);
		assertEquals("Assertj", result);

		byte[] value2 = new byte[] { 65, 115, 115, 101, 114, 116, 106, 32, 49, 53, 53 };
		String result2 = new String(value2, 8, 3);
		System.out.println(result2);
		assertEquals("155", result2);
	}
Assertj
155

withCharArray

Initialization of String 使用 char 陣列,建立一個 String 。

	@Test
	public void withCharArray() {
		char[] value = new char[] { 'A', 's', 's', 'e', 'r', 't', 'j' };
		String result = new String(value);
		System.out.println(result);
		assertEquals("Assertj", result);

		char[] value2 = new char[] { 'A', 's', 's', 'e', 'r', 't', 'j', ' ', '1', '5', '5' };
		String result2 = new String(value2, 8, 3);
		System.out.println(result2);
		assertEquals("155", result2);
	}
Assertj
155

intStream

Initializing Stream in Java 建立一個 IntStream ,初始化三個元素。

	@Test
	public void intStream() {
		int expectedCount = 3;
		IntStream intStream = IntStream.of(10, 20, 30);
		long count = intStream.peek(System.out::println).count();
		assertEquals(expectedCount, count);
	}
Assertj
155

StringInitializationTest.java

Initializing String in Java 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.string;

import static org.junit.Assert.*;

import org.junit.Test;

public class StringInitializationTest {

	@Test
	public void withLiteral() {
		String value = "Assertj";
		System.out.println(value);
		assertEquals("Assertj", value);

		String value2 = "155";
		System.out.println(value2);
		assertEquals("155", value2);
	}

	@Test
	public void withNew() {
		String value = new String("Assertj");
		System.out.println(value);
		assertEquals("Assertj", value);

		String value2 = new String("155");
		System.out.println(value2);
		assertEquals("155", value2);
	}

	@Test
	public void withByteArray() {
		byte[] value = new byte[] { 65, 115, 115, 101, 114, 116, 106 };
		String result = new String(value);
		System.out.println(result);
		assertEquals("Assertj", result);

		byte[] value2 = new byte[] { 65, 115, 115, 101, 114, 116, 106, 32, 49, 53, 53 };
		String result2 = new String(value2, 8, 3);
		System.out.println(result2);
		assertEquals("155", result2);
	}

	@Test
	public void withCharArray() {
		char[] value = new char[] { 'A', 's', 's', 'e', 'r', 't', 'j' };
		String result = new String(value);
		System.out.println(result);
		assertEquals("Assertj", result);

		char[] value2 = new char[] { 'A', 's', 's', 'e', 'r', 't', 'j', ' ', '1', '5', '5' };
		String result2 = new String(value2, 8, 3);
		System.out.println(result2);
		assertEquals("155", result2);
	}
}

心得分享

Java Initializing String 一般而言有幾種 String 初始化的操作, Initializing String in Java 提供了例如: new 、 byte[] 、 char[] 初始化方式,在應用上相當廣泛。

發佈留言