//Program to convert a String to upper case letters.
class StringUpper
{
public static void main(String[] ar)
{
String str= new String("new zealand");
System.out.println("Original String is : "+ str);
System.out.println("String's upper case value : " + str.toUpperCase());
}
}
String's value : new zealand
String's upper case value : New ZEALAND
A String object was initialized to a value new zealand. When the method toUpperCase() was called on this String object, it returned the content of the invoked String in upper case letters.
//Program to convert a String to upper case letters.
class StringUpper
{
public static void main(String[] ar)
{
String str= new String("mars - the red planet");
System.out.println("Original String is : "+ str);
System.out.println("String's upper case value : " + str.toUpperCase());
System.out.println("String value : " + str);
}
}
String's value : mars - the red planet
String's upper case value : MARS - THE RED PLANET
String's value : mars - the red planet
Even after calling the method toUpperCase() on the String object, the original case of its content remains the same - mars - the red planet, because the method toUpperCase() only returns the
upper case form of the content in a String object, without changing the original case of its content.
//Program to convert a string to Uppercase letters.
class StringUpper2
{
public static void main(String[] ar)
{
String str= new String("hurray! it's sunday!");
System.out.println("Original string is : "+ str);
str=str.toUpperCase(); //result of calling toUpperCase() is stored back in String object
System.out.println("String's value after converting to UpperCase : " + str);
}
}
Original string is : hurray! it's sunday!
String's value after converting to upper case : HURRAY! IT'S SUNDAY!
The original content of String object is modified by placing the result of toUpperCase() method back in the String object.
Coming Next
-
JSP & Servlets