Java - String String Case

Introduction

To convert a string to lower and upper case, use the toLowerCase() and the toUpperCase() methods, respectively.

For example, "Hello".toUpperCase() will return the string "HELLO", whereas "Hello".toLowerCase() will return the string "hello".

String objects are immutable.

toLowerCase() or toUpperCase() leave the content of the original object unchanged.

Java creates a new String object with the identical content as the original String object with the cases of the original characters changed.

The following snippet of code creates three String objects:

Demo

public class Main {
  public static void main(String[] args) {
    String str1 = new String("Hello"); // str1 contains "Hello"
    String str2 = str1.toUpperCase();  // str2 contains "HELLO"
    String str3 = str1.toLowerCase();  // str3 contains "hello"
    System.out.println(str1);/*  w ww .j ava  2  s .c  om*/
    System.out.println(str2);
    System.out.println(str3);
  }
}

Result

Related Topics

Exercise