From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Working with character values

Working with character values - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Working with character values

- Java distinguishes between characters and strings. A character is a primitive data type, and it consists of just a single character. A string is a complex object, and can consist of many characters. I'll show you how to to declare and initialize characters here, and I'll add code to the main method to declare a single variable with the data type of char, C-H-A-R, a name of c1, and a value of '1', wrapped in single quotes. Literals for characters are always wrapped in single quotes, while literals for strings are wrapped in double quotes. I'll duplicate that line of code a couple of times. And then I'll change the names of the variables, to c2 and c3. And I'll change their equivalent values. Then, I'll output these values. I'll use System.out, and I'll start with the label of Char 1, and I'll append c1. I'll duplicate that a couple of times, and change the labels and values. And I get the values as expected. You can also declare a character using a literal that's a Unicode escape…

Contents