Lesson Tuesday

So far, we've learned about the following primitives in Java

  • int
  • boolean
  • float

In this lesson, we are going to learn about another data type, the char.

Primitives Review

As you recall, there are two types of data: Objects, and primitives. Almost everything is an Object in Java. The exception to this rule is primitives. Primitives are even more basic than objects. And they're easy to spot because they're lowercase. We can tell just by its name that charis a primitive.

If any of this sounds unfamiliar, or if you'd like a refresher on Java primitives, revisit the Primitives and Wrapper Classes lesson from the pre-work.

About char

So what is this char primitive used for anyway? char represents a single character. We can create a char variable like this:

char a = 'a';
char one = '1';
char space = ' ';

Unlike strings, we use single quotes, not double quotes, to wrap the char. chars are not the same as Strings - be aware of differences when trying to compare them.

Use Cases

Chars are used in various useful methods. Here are several examples:

.toCharArray()

.toCharArray() is called upon an instance of the String class, and returns an Array containing the individual chars that make up that String:

> "Hello world".toCharArray();

char[] res = [H, e, l, l, o,  , w, o, r, l, d]

.indexOf()

.indexOf() is called upon a String and returns an Integer representing the first index of a char's in a String. It will return -1 if the char is not present in the String:

>  "Hello world".indexOf('e');
java.lang.Integer res = 1

> "Hello world".indexOf('7');
java.lang.Integer res = -1

.replace()

.replace() is called upon a String and replaces all instances of the char in its first parameter with the char in its second parameter:

> "Hello world".replace('l', 'L');
java.lang.String res = "HeLLo worLd"

.valueOf()

.valueOf() is called on a String and returns the string representation of the provided char:

> String.valueOf('L');
java.lang.String res = "L"

Wrapper Class

Like other primitives we've seen, char also has a corresponding wrapper class: Character. Also similar to other primitives and corresponding wrapper classes we've seen, we can easily translate one into another. Consider the following example in the REPL:

> char testLetter = 'j';
char testLetter = j

> Character wrapperClassTestLetter = testLetter;
java.lang.Character wrapperClassTestLetter = j

> wrapperClassTestLetter;
java.lang.Character res2 = j

Here, we declare and define a char named testLetter. Then, we easily translate it into an instance of Character by simply declaring a Character variable named wrapperClassTestLetter and setting it equal to our char variable, testLetter. Then, when we call wrapperClassTestLetter again, we can see it's an instance of the Character wrapper class.

Choosing Between char and Character

Remember, primitives cannot have method called upon them because they are not objects. Instances of wrapper classes can have methods called upon them, because they are objects. So, if you need to call a method on an instance of a single letter, use the Character class. It's methods are detailed in the Java Documentation Entry for Character. Otherwise, use the char primitive.

Overview


  • So far, we've learned about the following primitives in Java: int, boolean, float.

  • char is another type of primitive. Like all primitives, it is not an object and may not have methods called upon it.

  • Character is the corresponding wrapper class for char. We can easily translate a char into a Character object like this:

> char testLetter = 'j';
char testLetter = j

> Character wrapperClassTestLetter = testLetter;
java.lang.Character wrapperClassTestLetter = j

> wrapperClassTestLetter;
java.lang.Character res2 = j

Additional Resources