Java String
时间: 2024-05-09 10:19:03 浏览: 101
Java String is a sequence of characters. It is a class that belongs to the java.lang package and is used to represent and manipulate strings in Java programming language. Strings in Java are immutable, which means once a string is created, it cannot be modified.
The String class has many useful methods for manipulating strings, such as concatenation, substring, length, comparison, and more. Strings can be created using string literals or by using the new keyword to create a new instance of the String class.
Example:
```java
String str1 = "Hello World"; // Using string literal
String str2 = new String("Hello World"); // Using new keyword
System.out.println(str1); // Output: Hello World
System.out.println(str2); // Output: Hello World
System.out.println(str1.equals(str2)); // Output: true
```
In the example above, we created two strings using a string literal and the new keyword. We then printed the strings and compared them using the equals() method, which returns true because the strings have the same content.
Overall, the Java String class is an essential part of the Java programming language and is used extensively in almost every Java application.
阅读全文