Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 19
Lesson 19: String methods in Java – toUpperCase() and toLowerCase()
Welcome to lesson 19 of this tutorial on Programming in Java. Having looked into arrays in Java in our previous lessons and how we can manipulate arrays in Java, in this particular lesson we shall look at how we can take advantage of the many methods that ship in with Java to manipulate data of string type. In particular, we shall look at methods to convert strings into uppercase and lowercase.
Unlike int variables, or double variables, strings variables are objects, that means you can manipulate data of string type in more different ways than you can do with other data types such as int, double, boolean, byte, single, char or float variables.
Strings in Java are represented by series of Unicode characters under a variable name and Unicode values are stored as hexadecimals numbers.
Capital letters (A to Z) are stored using hexadecimals values \u0041 to \u005a, while lowercase letters (a to z) are stored using hexadecimals values \u0061 to \u007a.
How to convert strings into uppercase in Java
To convert strings into uppercase in Java, we use the inbuilt toUpperCase() method in Java. Let us write a simple program in Java that stores string characters in lowercase and then use the method to convert the strings into uppercase. Create a new Java class called ChangeToUpper and write the following code.
Program to convert strings into uppercase in Java
In the above program, we have defined a variable of string type called mystring using the following statement.
String mystring = "How are you";
In the first output using JoptionPane, we output the string as it is i.e "How are you". In the second output we first convert the string into uppercase and then output the same string but in uppercase.
mystring = mystring.toUpperCase();
How to convert strings into lowercase in Java
Now, modify the program or create another Java class called ChangeToLower as shown above. Change the string variable to be in uppercase and then use the method toLowerCase() to convert the string into lowercase and output. In the next lesson, we shall learn how we can compare strings in Java.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 4
In Lesson 3, we learnt how to write a Java program in the Java code window and how to Run a Java program. In this lesson, we shall learn how to work with variables in Java and how to manipulate computer memory using variables.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 6
In this lesson we’ll learn about another useful class for accepting user inputs and displaying results. This class is called JOptionPane class and is located in the javax.swing library.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 18
Array lists in Java are dynamic structures for store of values, that means values can be added or be removed from the list. We do not even need to declare the size of the list but values can be accessed by their index positions.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 3
In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program.