Overview
Represents a combination of characters that make up a string
- String is a class so when we make a new string we’re actually creating an object
- You can create String objects using string literals or the
newkeyword
- String is immutable! → Use
StringBuffer(or the more modernStringBuilder) when you need a mutable string - You MUST use
"and not', since'is reserved for chars
Basics
// same thing
String myString = new String("aBc"); // new keyword
String myString = "aBc"; // String literal
// returns new string
System.out.println(myString.toUpperCase()) // ABC
System.out.println(myString.toLowerCase()) // abc
System.out.println(myString) // aBc
// string concatenation
System.out.println(a + "+" + b " = " + (a + b));
System.out.println("데카르트는 \"나는 생각한다. 고로 존재한다.\"라고 말했다.");String myString = new String("aBc");myStringreference is stored in the stack, and the String object itself is stored in the heap- Java’s Memory Model
- string concatenation
- Combining several strings into a single string, or combining a string with other data into a new, longer string.
- often used to report the value of a variable
printVSprintlnprintln: sends output to current line then moves to the beginning of a new lineprint: only sends output to current line- so it doesn’t go to the beginning of a new line
String Literal
String s1 = "hello";
String s2 = "hello";s1ands2are references pointing toStringobjects.- All string literals in a special area of memory called the String Constant Pool.
- How it works: When you create
s1, Java puts the object"hello"into the pool. When you creates2, Java checks the pool, sees that"hello"already exists, and simply makess2point to that exact same object.- They point to the same place in memory!
String class: new keyword
String s3 = new String("hello");
String s4 = new String("hello");- Explicitly creates a new object every time!
- The
newkeyword forces Java to create a brand newStringobject in the main memory area (the heap), completely separate from the String Constant Pool. - How it works: Even though
s3ands4contain the exact same characters, they are two distinct objects in different memory locations.
Escape sequences
- two-character sequences used to represent special characters, all beginning with
\
System.out.println("What \"characters\" does this \\ print?");
// Output: What "characters" does this \ print?- Use
\nfor line break
StringBuffer
StringBuffer s = new StringBuffer("hello");
s.append("!"); //s = "hello!"Comparison
// comparing
String myString = "aBc";
System.out.println(myString.toLowerCase() == "abc"); // comparing instances (addresses)
System.out.println(myString.toLowerCase().equals("abc")); // comparing values- If you compare 2 primitives types, you compare the value
- If you compare 2 reference types, you compare if they point to the same instances