Java String Type

Java String Type


String Type In Java
As we have already studied string is not a data type in Java instead String is a class that is found inside java.lang package which is automatically imported means we do not need to import the package explicitly.
String is a collection, or sequence of characters like “Ram”, “10/A”, etc.
String must be enclosed in double quotes.
In Java, String is a class, hence the first letter will be in upper case.
As String is a class the first latter of String literal will always be written in Upper case and the rest of it in a  small case.
Now we will see Examples - 
Example  -
// Demonstrating Strings.

class StringDemo 
{

public static void main(String args[]) 
 
{
 
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);

}
 
}
Output  - 
First String

Second String

First String and Second String
Example -
 
// Demonstrating some String methods.

class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " +
strOb1.length());
System.out.println("Char at index 3 in strOb1: " +
strOb1.charAt(3));
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
}
Output - 
 
Length of strOb1: 12
Char at index 3 in strOb1: s
strOb1 != strOb2
strOb1 == strOb3