String


String

In Java, String is collections of character sequence and they are used as an Object. we use the Java.lang.String is the package used for string object creation. Java Strings are non - mutable in nature. Alteration is not allowed at once the object is created. Each time a string is created, an object is initialized of that type. The characters are mentioned within the double quotes which string initialization

Example:

String String_Name = "My Java Code";

Example:

String s=new String(“My Java Code”);
So what do we mean by Sequence of Character, see below

Example: Java stores each character of string as an char[ ]

Java String provide a lot of string manipulation methods such as compare(), split(), length(), equals(), compareTo(), substring etc

Ways to create string in java

Following are the two different ways of string creation By using the string literal
By using the new keyword

String literal
Using double quotes the java string literal is created
String a = “imagination”;
The object of the string are stored in the string constant pool
By using new keyword
String s = new String(“imagination”);
In this case, one reference variable and two objects are created. The string object is placed by the JVM in the normal pool memory and in the string constant pool the actual literal is placed

Example :

public class Main { public static void main(String[] args) { String web="Knowledge2life"; System.out.println("Online website "+ web); } }

OUTPUT:

Online website Knowledge2life