String class available in java.lang package .
java.lang.String
String in java is defined as , an object that represents sequence of characters.In java , objects of String are immutable which means a constant , and can not be changed once created.
String class provide various important method like :toUpperCase() - convert String to upper case.
toLowerCase() - convert String to lower case.
length() - to check length of String.
concat() - to concatenate two String .
split() - to split String.
and many other methods ,each method is used to perform some operation on String value.
Ways To Create String in Java :
check below program where we are creating String type variable using different ways :
public class StringDemo {
public static void main(String[] args) {
String websiteUrl="https://www.crtr4u.com";
String subject=new String("java programming");
char charArray[] = {'P','Q','R','S','T'};
String strFromCharArray= new String(charArray);
byte[] byteArray= {70,71,72};
String stringFromByteArray=new String(byteArray);
System.out.println(stringFromByteArray);
System.out.println(strFromCharArray);
System.out.println(subject);
System.out.println(websiteUrl);
}
}
Output : FGH PQRST java programming https://www.crtr4u.comPerforming different operations on String using inbuilt methods :
package com.suv.string;
public class StringDemo2 {
public static void main(String[] args) {
String websiteUrl="https://www.crtr4u.com/2023/01/string-in-java.html";
String name="Swapnil V";
String []splittedArray=websiteUrl.split("\\.");
for(String str: splittedArray) {
System.out.println(str);
}
System.out.println(name.toLowerCase());
System.out.println(name.toUpperCase());
System.out.println(name.length());
}
}
Output : https://www crtr4u com/2023/01/string-in-java html swapnil v SWAPNIL V 9The java.lang.String class implements Serializable, Comparable and CharSequence interfaces. Immutable String in Java : immutable means unmodifiable or unchangeable , String object in java is immutable.
check link String vs StringBuffer vs StringBuilder