A string is a final class in Java that extends java.lang. An object which is represented as character strings. Serializable, Comparable and CharSequence interfaces are implemented by string class. All the string literals such as “string example” are treated as instances of the String class.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsCode:
public class test { public static void main(String[] args) { String s1 = "I am at a \"Palace \" of Mumbai."; System.out.println(s1); } }
Output:
If you add two strings, the result will be string concatenation.
Code:
public class test { public static void main(String[] args) { int a = 10; int b = 20; int c = a+b; System.out.println(c); String s = "10"; int d = 10; String s1 = s+d; System.out.println(s1); } }
Output:
A string, once created, its value cannot be changed. Strings are constant. Such a property is known as immutable.
Code:
class Test { public static void main(String args[]) { String str="I am string"; str.concat(" I am instance of String class."); System.out.println(str); } }
Output:
Explaination:
Code:
class Test { public static void main(String args[]) { String str="I am string"; str = str.concat(" I am instance of String class."); System.out.println(str); } }
Output:
To create a mutable string, you can use StringBuffer or StringBuilder Class.
There are two types of character sequence classes in Java. The immutable class which is the String class, and the mutable class which is StringBuilder and StringBuffer.
StringBuilder and StringBuffer have some differences.
Given below are the example of StringBuffer and StringBuilder
Code:
class StringBufferExample { public static void main(String args[]) { StringBuffer bufferstring=new StringBuffer("I am stringbuffer."); bufferstring.append("I am thread safe."); System.out.println(bufferstring); } }
Output:
Code:
class StringBuilderExample { public static void main(String args[]) { StringBuilder builderstring=new StringBuilder("I am stringbuilder."); builderstring.append("I am more efficient than string buffer."); System.out.println(builderstring); }
Output:
In Java, strings are the sequence of characters, and unlike c,c++, they are the objects of class String.
There are some constructors supported by this class which is as follows:
Important string constructors in Java
Given below are some important string constructors in java:
Important string methods in Java
Given below are some important string methods in java:
Given below are the examples of String Class in Java:
Let us check whether the string contains only whitespaces.
Code:
class StringWhitespaceChecker { public static boolean isStringWithWhitespace(String s) { if(s.trim().isEmpty) { return true; } else { return false; } } public static void main(String args[]) { StringWhitespaceChecker s1 = new StringWhitespaceChecker(); String s =new String(" "); String string = new String(" I am string. "); boolean condition1 = s1.isStringWithWhitespace(s); boolean condition2 = s1.isStringWithWhitespace(string); System.out.println(condition1); System.out.println(condition2); } }
Output:
Explaination:
String function.
Code:
class Test { public static void main(String args[]) { String s = new String("hello"); s.toUpperCase(); System.out.println(s); s = s.concat("world"); System.out.println(s); System.out.println(s.charAt(1)); String s1 = new String("helllo"); boolean b = s.equals(s1); System.out.println(b); } }
Output:
Java String to toCharArray()
Code:
class Test { public static void main(String args[]) { String s = new String("hello"); Char[] c1 = s.toCharArray() for(int i=0;i<c1.length;i++) { System.out.println(c1[i]); } } }
Output:
This article is focused upon the criteria such as introduction to the string, String constructors, String Methods, Types of String classes, String immutable, StringBuffer and StringBuilder. Examples of each property. A string is a very important class in Java. You will work with string manipulation in multiple ways in real-world projects.
The above is the detailed content of String Class in Java. For more information, please follow other related articles on the PHP Chinese website!