Understanding Java Strings: Creation, Immutability, and Operations
This article explains Java's String class, covering its automatic import, how to create string objects without the new keyword, concatenation, immutability, common methods like replace, and provides useful API references and code examples for practical use.
In the previous Java fundamentals series we discussed core concepts, especially object‑oriented basics; this continuation supplements those basics and moves toward application‑level topics.
Most programming languages can handle strings, which are ordered collections of characters such as "Hello World!". In Java, strings are represented by the String class, and its methods enable various string operations.
The String class resides in the java.lang package, which is automatically imported when the JVM starts, so you can use String without an explicit import statement.
Creating Strings
When creating a String object you do not need the new keyword. For example:
public class Test {
public static void main(String[] args) {
String s = "Hello World!";
System.out.println(s);
}
}Writing a literal like "Hello World" already creates the object in memory; using new String("Hello World!") would create an unnecessary duplicate.
An Object
The String class is the only class that does not require the new keyword for object creation; this peculiarity should be noted when using it.
String Operations
You can concatenate strings using the + operator, e.g., "abc" + s.
Most string manipulations are performed via the class's methods; an illustration of common methods is shown below.
Immutable Objects
String objects are immutable; existing instances cannot be altered. You can create your own immutable types by omitting mutator methods.
Methods like replace() appear to modify a string, but they actually create a new String instance. For example:
s = s.replace("World", "Universe");This call creates a new string "Hello Universe!" and returns its reference; the original "Hello World!" becomes eligible for garbage collection if no other references exist.
Immutable Object
Java API
Java provides many powerful packages; understanding these packages and their APIs is essential. The String class is defined in java.lang.String . You can consult the official Oracle documentation at:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
This page contains the most comprehensive description of the String class.
For an overview of all Java APIs, visit:
http://docs.oracle.com/javase/6/docs/api/
Original source: cnblogs.com/vamei/archive/2013/04/08/3000914.html
Java团长
每日分享Java干货
微信号:javatuanzhang
长按识别二维码
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.