Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Char in Java: What is Character class in Java?

Published on Oct 07,2019 53 Views
10 / 11 Blog from Objects and Classes

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

Java Logo - Char in Java - EdurekaThe following topics will be covered in this article:

Let’s begin!

Character class in Java

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.

Escape Sequence

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 SequenceDescription
tInserts a tab in the text at this point.
nIt inserts a new line in the text.
bInserts a backspace in the text at this point.
rIt Inserts a carriage return in the text at this point.
fIt 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.

Methods of character class

The following table discusses a few important methods of the character class.

MethodsDescription
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.

Code:


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);
}
}
}
}

Output:

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.