Tutorial

Java String to Uppercase

Published on August 3, 2022
Default avatar

By Pankaj

Java String to Uppercase

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Java String to uppercase conversion can be done using toUpperCase() method.

Java String to UpperCase

java string to uppercase

  • Java String toUpperCase() method has two variants - toUpperCase() and toUpperCase(Locale locale).
  • Conversion of the characters to upper case is done by using the rules of default locale. Calling toUpperCase() function is same as calling toUpperCase(Locale.getDefault()).
  • Java String to upper case method is locale sensitive, so use it carefully with strings that are intended to be used locale independent. For example programming language identifiers, HTML tags, protocol keys etc. Otherwise you might get unwanted results.
  • To get correct upper case results for locale insensitive strings, use toUpperCase(Locale.ROOT) method.
  • String toUpperCase() returns a new string, so you will have to assign that to another string. The original string remains unchanged because String is immutable.
  • If locale is passed as null to toUpperCase(Locale locale) method, then it will throw NullPointerException.

Java String to Uppercase Example

Let’s see a simple example to convert a string to upper case and print it.

String str = "Hello World!";
System.out.println(str.toUpperCase()); //prints "HELLO WORLD!"

We can also use Scanner class to get user input and then convert it to uppercase and print it. Here is a complete example program to convert java string to uppercase and print it.

package com.journaldev.string;

import java.util.Scanner;

public class JavaStringToUpperCase {

	public static void main(String[] args) {
		String str = "hello World!";

		String strUpperCase = str.toUpperCase();

		System.out.println("Java String to Upper Case: " + strUpperCase);

		readUserInputAndPrintInUpperCase();
	}

	private static void readUserInputAndPrintInUpperCase() {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please write input String and press Enter:");
		String str = sc.nextLine();
		System.out.println("Input String in Upper Case = " + str.toUpperCase());
		sc.close();
	}

}

Below console output shows the sample execution of above program.

Java String to Upper Case: HELLO WORLD!
Please write input String and press Enter:
Welcome to JournalDev Tutorials
Input String in Upper Case = WELCOME TO JOURNALDEV TUTORIALS

That’s all for java string to uppercase conversion example. Reference: API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 11, 2021

May I know if the code below meets the following question : Write a Java method, smallestIndex, that takes as parameters an int array and its size,and returns the index, of the first occurrence, of the smallest element in the array. Also, write a program to test the method. package Assignment.Q068; import java.util.Scanner; public class trial068 { static Scanner input =new Scanner(System.in); /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); int [] numbers = new int[10]; // Create an array of length ten // Prompt the user to enter ten numbers System.out.print("Enter ten numbers: "); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); // Display the smallest value System.out.println("The minimum number is " + numbers[indexOfSmallestElement(numbers)] + " index " + indexOfSmallestElement(numbers)); } /** Method indexOfSmallestElement returns the index of * the smallest element in an array of integers */ public static int indexOfSmallestElement(int[] array) { if (array.length <= 1) return 0; int min = array[0]; int minimumIndex = 0; for (int i = 1; i < array.length; i++) { if (array[i] < min) { min = array[i]; minimumIndex = i; } } return minimumIndex; } } package Assignment.Q068; import java.util.Scanner; public class trial068 { static Scanner input =new Scanner(System.in); /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); int [] numbers = new int[10]; // Create an array of length ten // Prompt the user to enter ten numbers System.out.print("Enter ten numbers: "); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); // Display the smallest value System.out.println("The minimum number is " + numbers[indexOfSmallestElement(numbers)] + " index " + indexOfSmallestElement(numbers)); } /** Method indexOfSmallestElement returns the index of * the smallest element in an array of integers */ public static int indexOfSmallestElement(int[] array) { if (array.length <= 1) return 0; int min = array[0]; int minimumIndex = 0; for (int i = 1; i < array.length; i++) { if (array[i] < min) { min = array[i]; minimumIndex = i; } } return minimumIndex; } }

- shangkari

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 24, 2021

    Write a program that ask the user to input two string. The first string convert it to Uppercase and lowercase. Then the second string use length() string method

    - salandaye

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel