Sub topic 3
Converting Between Strings and Values
Converting values
Use Integer.parseInt("42") to turn text into an integer, a whole number. Use String.valueOf(number) to turn the number back into text. If the text is not a valid integer, parseInt throws an error called NumberFormatException.
Formatting text and values
Formatting controls how values appear as text. For example, String.format(Locale.ROOT, "%.2f", 12.5) produces "12.50". The %.2f part requests two digits after the decimal point. Locale.ROOT makes the format independent of local language settings.
Unicode code points and text boundaries
Unicode is a standard for representing text. A code point is a number assigned to a character. Java stores strings using UTF-16, which uses units of text storage. Some code points need two units.
For "A\uD83D\uDE00", length() returns 3, but codePointCount() returns 2. A visible character can contain more than one code point, so even a code point count may not match the number of visible characters.
Encoding and decoding bytes
Encoding changes text into bytes, which are units used to store or send data. Decoding changes bytes back into text. Use the same character encoding, such as UTF-8, for both steps.
Splitting and replacing with regular expressions
A regular expression is a pattern used to find text. The split method uses a pattern to divide text into pieces. A limit of -1 keeps empty pieces at the end. In replaceAll("\\s+", " "), the pattern matches one or more spaces or other whitespace characters.
String pool, interning, and identity
Java’s string pool is a store of shared strings. Identity means whether two references point to the same object. The intern() method returns the pooled string with the same contents. Use equals to compare contents, not ==.
Try the examples
Save this program as ValuesDemo.java. Compile it with javac -encoding UTF-8 ValuesDemo.java, then run it with java ValuesDemo.
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
class ValuesDemo {
public static void main(String[] args) {
int number = Integer.parseInt("42");
String value = String.valueOf(number);
System.out.println(value + "!");
System.out.println(String.format(Locale.ROOT, "%.2f", 12.5));
String text = "A\uD83D\uDE00";
System.out.println(text.length());
System.out.println(text.codePointCount(0, text.length()));
String original = "café";
byte[] bytes = original.getBytes(StandardCharsets.UTF_8);
String decoded = new String(bytes, StandardCharsets.UTF_8);
System.out.println(original.equals(decoded));
String[] fields = "red,blue,".split(",", -1);
System.out.println(Arrays.toString(fields));
System.out.println("a b".replaceAll("\\s+", " "));
String pooled = "cat";
String separate = new String(pooled);
System.out.println(pooled.equals(separate));
System.out.println(pooled == separate);
System.out.println(pooled == separate.intern());
}
}Exercise
Why does "red,blue,".split(",", -1) produce three pieces?
Show answer
The -1 limit keeps the empty piece after the final comma.