In this lesson, you will learn
In Java, toUpperCase() and toLowerCase() are methods of the String class used to convert all the characters in a given string to uppercase or lowercase, respectively.
public String toUpperCase()
String str = "Hello World";
String upperStr = str.toUpperCase();
System.out.println(upperStr); // Outputs: HELLO WORLD
public String toLowerCase()
String str = "Hello World";
String lowerStr = str.toLowerCase();
System.out.println(lowerStr); // Outputs: hello world
In Java, data conversion methods related to strings are crucial for transforming data between different types, such as converting strings to numbers (integers, floating-point numbers) and vice versa.
Java provides several wrapper classes like Integer, Double, Float, etc., which offer methods to convert strings into respective numeric types.
parseInt: Converts the string argument to an integer (int).
public static int parseInt(String s) throws NumberFormatException
String str = "100";
int num = Integer.parseInt(str);
System.out.println(num); // Outputs: 100
valueOf(): Returns an Integer object holding the value extracted from the specified string.
public static Integer valueOf(String s) throws NumberFormatException
String str = "200";
Integer num = Integer.valueOf(str);
System.out.println(num); // Outputs: 200
Similar to the Integer class, for converting strings into doubles.
parseDouble()
public static double parseDouble(String s) throws NumberFormatException
String str = "99.99";
double num = Double.parseDouble(str);
System.out.println(num); // Outputs: 99.99
valueOf()
public static Double valueOf(String s) throws NumberFormatException
String str = "10.01";
Double num = Double.valueOf(str);
System.out.println(num); // Outputs: 10.01
Java provides a String.valueOf() method that can convert various data types, including numeric types to String.
public static String valueOf(dataType data)
int num1 = 100;
String str1 = String.valueOf(num1);
System.out.println(str1); // Outputs: "100"
double num2 = 99.99;
String str2 = String.valueOf(num2);
System.out.println(str2); // Outputs: "99.99"
Besides numbers, String.valueOf() can also handle other data types, including characters, boolean values, and objects, converting them to their string representation.
boolean flag = true;
String boolString = String.valueOf(flag);
System.out.println(boolString); // Outputs: "true"
Output
Output
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.