Learn Pain Less

HomeOur TeamContact
Java
Working with Character value in java
Pawneshwer Gupta
Pawneshwer Gupta
February 10, 2015
1 min
Working with Character value in java

Character is single valued object. A character is anything that you can type such as letters, digits, punctuation and spaces. Character is similar to String; the difference is that String contain 0 or more values but character contains only 1 value. Character value is written inside single quotes. We can also have thought that String is array of Characters. Because String contains multiple characters. Character contains only 1 alphabet, number or symbol.

Character is primitive data type. Every primitive type in Java have advantages in term of speed and memory footprint. But it is not a real object, so there is some possibility you lose using them. They cannot be used as Generic Type Parameters, the could not have methods or fields and so on.

For example:

char a = 'P';
char b = '1';
char c = '@';

These are example of how to create character in java.

Well, char (or its wrapper class Character) means a single character, i.e. you can’t write ‘ab’ whereas String is a text consisting of a number of characters and you can think of a string an array of characters (in fact the String class has a member char[] value).

You could work with plain char arrays but that’s quite tedious and thus the String class is there to provide a convenient way for working with texts.

To convert a string to a char array you can use

theNameOfTheString.toCharArray();

String holds many char values. This is best expressed through an example.

Examples:

char someChar ='A';
String someString ="I like to eat food!";

In terms of ASCII values, you can say char is a single ASCII value ranging from 0-255. Whereas String is a collection of ASCII values. Try this code to learn better.

char c='a';
String s="a b c d e f g hijkl";
int i=c;
System.out.println(i);
for(int count=0;count<s.length();count++)
{
int temp=s.charAt(count);
System.out.print(temp+" ");
}

The output will be:

97
97 32 98 32 99 32 100 32 101 32 102 32 103 32 104 105 106 107 108

Since 97 is the ASCII value for small ‘a’. 32 is the ASCII value for space. Hope this helps in-depth understanding of the concept.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

startchar

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Working with Files, Folders java
Working with Files, Folders java
March 25, 2015
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media