Characters
Java Characters
A single character can be stored using the char data type. The character needs to have single quotes around it, such as “A” or “c”:
Example
char myGrade = 'B';
System.out.println(myGrade);
As an alternative, you can utilize ASCII values to display specific characters if you are familiar with them:
Example
char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1);
System.out.println(myVar2);
System.out.println(myVar3);
Tip: A helpful resource is our ASCII Table Reference, which has a list of all ASCII values.
Strings
A string of characters (text) is stored in the String data type. Double quotations must enclose string values:
Example
String greeting = "Hello World";
System.out.println(greeting);
Some refer to the String type as “the special ninth type” because of how frequently it is utilized and integrated in Java.
Because a String in Java refers to an object, it is actually a non-primitive data type. There are methods on the String object that can be used to manipulate strings in different ways. If you’re still confused about what a “object” is, don’t worry. In a later chapter, we will cover strings and objects in greater detail.