Changing case:

In Java, strings provide methods to convert the case of characters within a string. 

This can be useful for various string manipulation tasks, such as normalizing data, performing case-insensitive comparisons, or ensuring consistent formatting. 

Here are some important details to consider:


Converting to Uppercase:

The toUpperCase() method converts all characters in a string to uppercase.

It returns a new string with the uppercase version of the original string while leaving the original string unchanged.

Example:


String message = "Hello, World!"
String uppercaseMessage = message.toUpperCase();
System.out.println(uppercaseMessage); // Output: HELLO, WORLD!
System.out.println(message); // Output: Hello, World!

In this example, the toUpperCase() method is called on the message string, which converts all characters to uppercase. 

The method returns a new string, uppercaseMessage, with the uppercase version of the original string. However, the original message string remains unchanged.


Converting to Lowercase:

The toLowerCase() method converts all characters in a string to lowercase.

Similar to toUpperCase(), this method returns a new string with the lowercase version of the original string while leaving the original string unchanged.

Example:


String message = "Hello, World!"
String lowercaseMessage = message.toLowerCase();
 System.out.println(lowercaseMessage); // Output: hello, world!
System.out.println(message); // Output: Hello, World!

In this example, the toLowerCase() method is called on the message string, converting all characters to lowercase. 

The method returns a new string, lowercaseMessage, with the lowercase version of the original string. The original message string remains unaltered.


Changing Case with Locale:

Both toUpperCase() and toLowerCase() methods have overloaded versions that accept a Locale parameter.

Specifying a Locale helps in handling special characters and language-specific case conversions.

Example:


String message = "GöD MORGEN"
String uppercaseMessage = message.toUpperCase(Locale.GERMAN);
 System.out.println(uppercaseMessage); // Output: GÖD MORG

In this example, the toUpperCase() method is called on the message string with the Locale.GERMAN parameter. 

This ensures that special characters in the string are properly converted to uppercase according to the German locale rules.


Changing the case of strings is useful for various string manipulation tasks. 

It allows you to normalize data, perform case-insensitive comparisons, or ensure consistent formatting. 

By utilizing the toUpperCase() and toLowerCase() methods in Java, you can easily convert the case of characters within strings while keeping the original string intact.