Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
In Java, we come across situations where we need to use objects instead of primitive data types. To accomplish this, Java provides wrapper class Character for primitive data type char. In this article on Char in Java, let us understand the same in detail.
The following topics will be covered in this article:
Let’s begin!
The Character class generally wraps the value of all the primitive type char into an object. Any object of the type character may contain a single field whose type is char. The Character class offers a number of useful classes (i.e., static) methods for working with characters.
To create a character object with the character constructor −
Character ch = new Character('a');
The above statement creates a character object which contains ‘a’ of type char. There is only one constructor in character class which expects an argument of the char data type.
Next in this article on Char in Java, let us see few escape sequences used with the characters in Java.
A character preceded by a backslash () is generally called an escape sequence. There is a table mentioned below that will help you in understanding this concept.
Escape Sequence | Description |
t | Inserts a tab in the text at this point. |
n | It inserts a new line in the text. |
b | Inserts a backspace in the text at this point. |
r | It Inserts a carriage return in the text at this point. |
f | It inserts a form feed in the text at this point. |
‘ | It iserts a single quote character in the text at this point. |
\” | It inserts a double quote character in the text at this point. |
\ | Inserts a backslash character in the text at this point. |
Since you have understood the escape sequences, let us move ahead and understand the methods that character class offers in Java.
The following table discusses a few important methods of the character class.
Methods | Description |
isWhitespace() | It helps in determining whether the specified char value is whitespace. |
isDigit() | It helps in determining whether the specified char value is a digit. |
isLetter() | It helps in determining whether the char value is a letter. |
isUpperCase() | It helps in determining whether the specified char value is uppercase. |
isLowerCase() | It helps in determining whether the specified char value is lowercase. |
toUpperCase() | It returns the uppercase form of the specified char value. |
toLowerCase() | It returns the lowercase form of the specified char value. |
toString() | It returns a String object representing the specified character value |
Next, in this article on Char in Java, let us see the practical implementation of the above-discussed methods.
import java.util.Scanner; public class JavaCharacterExample1 { public static void main(String[] args) { // Ask the user for the first input. System.out.print("First input:"); // Use the Scanner class to get the user input. Scanner scanner = new Scanner(System.in); // Gets the user input. char[] value1 = scanner.nextLine().toCharArray(); int result1 = 0; // Count the characters for a specific character. for (char ch1 : value1) { result1 = Character.charCount(ch1); } // Print the result. System.out.print("Value: "+result1+"n"); System.out.print("Second input:"); char[] value2 = scanner.nextLine().toCharArray(); for (char ch2 : value2) { int result2 = Character.hashCode(ch2); System.out.print("The hash code for the character '"+ch2+"' is given as:"+result2+"n"); } System.out.print("Third input:"); char[] value3 = scanner.nextLine().toCharArray(); for (char ch3 : value3) { boolean result3 = Character.isDigit(ch3); if(result3){ System.out.println("The character '" + ch3 + "' is a digit. "); } else{ System.out.println("The character '" + ch3 + "' is not a digit."); } System.out.print("Fourth input:"); char[] value4 = scanner.nextLine().toCharArray(); for (char ch4 : value4) { boolean result4 = Character.isISOControl(ch4); System.out.println("The fourth character '"+ch4+"' is an ISO Control:"+result4); } } } }
First input:89 Value: 1 Second input:J The hash code for the character 'J' is given as:74 Third input:5 The character '5' is a digit. Fourth input:h The fourth character 'h' is an ISO Control:false
With this, we come to an end to this article on Char in Java. I hope you understood the fundamentals of Java. If you found this article on “Char in Java”, check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “Char in Java” and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
edureka.co