String in Java Use of String datatype in Java with Example

By | December 7, 2010
String is a system defined class in Java.Declaring “Hello World” in code will create an object of type string with data “Hello World” and returns a reference to it.String in Java Use of String datatype in Java with Example.
String is a system defined class in Java.Declaring “Hello World” in code will create an object of type string with data “Hello World” and returns a reference to it.
System.out.println(“Hello World”);
Unlike C, the string is of fixed length and memory for the string is managed totally by the String class.
i)Prevents buffer overruns
ii)NULL terminator not used in strings
iii)String is not a simple array of characters
iv)String is immutable
Example:
class CheckString{
public static void main(String args[]){
String str=”HELLO guys”;
System.out.println(“The String is:” + str);
System.out.println(“Length of the String is:” +
str.length());
System.out.println(“Character at specified position:”
+ str.charAt(4));
System.out.println(“substring of the String is:” +
str.substring(6,10));
System.out.println(“Index of the specified character:”
+ str.indexOf(“g”));
System.out.println(“conversion to uppercase:” +
str.toUpperCase());
System.out.println(“conversion to uppercase:” +
str.toLowerCase());
}
}
Result:
The String is:HELLO guys
Length of the String is:10
Character at specified position:O
substring of the String is:guys
Index of the specified character:6
conversion to uppercase:HELLO GUYS
conversion to uppercase:hello guys

Leave a Reply

Your email address will not be published. Required fields are marked *