What is Character in Java?

We always interact with each other through words and sentences. So, there is some way by which Java should also play with these words or sentences.

Characters

You all must be knowing about characters. In Java data type which is used to store characters is char means characters that are represented using Unicode which is an international character set representing all the characters.

Basically in Java, char ranges from 0 to 65,535.

Characters are always written within ‘ ‘.

Strings

We can think of a string which is a collection of characters. In other words, it is an array of characters, like “Babu” is a string. In Java programming, strings are objects. Before we understanding it, let’s first see how to declare a string.

String b = “Jamu”;

We can also declare it as

String b = {‘J’,’a’,’m’,’u’};

Here, b is a string having the value Jamu. b is an object of the class String.

Thus ‘b’ is a String object having its value “Jamu”.

As stated that string is an array of characters, but in the second example, we are declaring string as an array of characters.

We can also create some String objects using the new keyword. Here, the new keyword is used to create an object ‘j’ of the class String. The following codes are used to create a String object ‘s’ having its value “Jamu”.

String b = new String("Jamu");
char ch = {'J','a','m','u'};
String j = new String(ch);

Methods of string classes in Java

There are some methods of a String object in Java that perform many important operations. For that, we need to first include java.lang.String class in our program. By doing this, we need to write the following code in the very beginning of our program.

List of Some important methods from the String class.

1) char charAt(int index)

It mainly returns the character value for the specified index in a string.

public class D3{

public static void main(String[] args){

String d1 = "Jamu";

char ch = d1.charAt(2);

System.out.println(ch);

}

}

Output: b

The character at index 2 in d1 is b. So, charAt(2) returned b.

2) int length()

It mainly returns the length of a string including white spaces.

public class D4{

public static void main(String[] args){

String d1 = "Codedec World";

int len = d1.length();

System.out.println("String length is " + len);

}

}

Output:13

3). String substring(int startIndex) or String substring(int startIndex, int endIndex)

It mainly returns a substring depending on the start index and the end index.

public class D5{

public static void main(String[] args){

String d1 = "Code Umbrella";

String d2 = d1.substring(2);

String d3 = d1.substring(2,6);

System.out.println(d2);

System.out.println(d3);

}

}

Output: de Umbrella

de U

4). boolean isEmpty()

It checks whether a string has a NULL value or not.

public class C6{

public static void main(String[] args){

String d1 = "Code Umbrella";

String d2 = "";

System.out.println(d1.isEmpty());

System.out.println(d2.isEmpty());

}

}

Output: false

true

If you have any confusion about data types please read our previous article Data Type in Java for more clarity !!