I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

This article is part of a series:
• Java String.String()
• Java String.String()
• Java String.codePointCount()
• Java String.codePointAt()
• Java String.concat()
• Java String.contains()
• Java String.copyValueOf()
• Java String.endsWith()
• Java String.format()
• Java String.getBytes()
• Java String.indexOf()
• Java String.intern()
• Java String.isEmpty()
• Java String.lastIndexOf()
• Java String.regionMatches()
• Java String.replace()
• Java String.replaceAll()
• Java String.split()
• Java String.startsWith()
• Java String.subSequence()
• Java String.substring()
• Java String.toLowerCase()
• Java String.toUpperCase()
• Java String.trim()
• Java String.valueOf()

String objects can be created by either using literals:

String s = "a string";

or by calling one of the constructors:

String s = new String("a string");

If we use the String literal, it’ll try to reuse already existing object from the String constant pool.

On the other hand, when instantiating a String using the constructor, a new object will be created

This constructor accepts many types of arguments and uses them to create a new String object.

Available Signatures

public String()
public String(byte[] bytes)
public String(byte[] bytes, Charset charset)
public String(byte[] bytes, int offset, int length)
public String(byte[] bytes, int offset, int length, Charset charset)
public String(byte[] bytes, int offset, int length, String charsetName)
public String(byte[] bytes, String charsetName)
public String(char[] value)
public String(char[] value, int offset, int count)
public String(int[] codePoints, int offset, int count)
public String(String original)
public String(StringBuffer buffer)
public String(StringBuilder builder)

Example

@Test
public void whenCreateStringUsingByteArray_thenCorrect() {
    byte[] array = new byte[] { 97, 98, 99, 100 };
    String s = new String(array);
    
    assertEquals("abcd", s);
}

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS