Java Notes
A data type tells Java what kind of value a variable holds: a whole number, decimal, character, true/false answer, or text.
A data type tells Java what kind of information you are storing. This matters because Java must know whether it should add numbers, compare a yes/no answer, or join text together.
For example, 18, 82.5, true, and "Asha" look different to Java. A person's age is a whole number. A test average can have a decimal. A pass result is yes or no. A person's name is text. Each needs an appropriate type.
The five types beginners use most
Start with these five types. They are enough for most first programs.
| When you want to store... | Use this type | Example | Why this type fits |
|---|---|---|---|
| A whole number | int | int age = 18; | There is no decimal part. |
| A decimal number | double | double average = 82.5; | It can store a fraction such as .5. |
| One character | char | char grade = 'A'; | It stores one letter, digit, or symbol. |
| A yes/no answer | boolean | boolean passed = true; | It can contain only true or false. |
| Text | String | String name = "Asha"; | It stores one or more characters. |
Do not choose a type only because it “works.” Choose it by what the value means. An age should use int, not String, because you may want to calculate age + 1. A name should use String, not char, because a name has more than one character.
See all five types in one program
This program creates a small student profile. Read the variable names first, then see which data type matches each kind of information.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Here is what each line stores:
namestores text, so it usesStringand double quotes.agestores a whole number, so it usesintand no quotes.averageMarkstores a number with a decimal point, so it usesdouble.gradestores one character, so it usescharand single quotes.passedstores the answer to a question: “Is the average at least 40?” The comparison produces eithertrueorfalse, so it usesboolean.
Change averageMark to a number below 40 and run the program again. Notice that Java changes the value printed for passed automatically because the condition is checked again.
Whole numbers and decimal numbers are not the same
int stores whole numbers only. double stores decimal numbers. This difference matters when you calculate an average.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
wholeAverage loses the .5 because both values used in the division are int. In the second calculation, (double) totalMarks changes one value into a decimal before Java divides, so the decimal part is kept. You will learn this conversion more deeply in the Type Casting lesson.
One character and text are different
char is for exactly one character. String is for text with one or more characters. The quotation marks tell Java which one you mean.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Use single quotes for char: 'A'. Use double quotes for String: "Asha". char grade = "A"; is wrong because "A" is text, even though it contains only one letter.
Why String is different from int
int is a simple numeric value. String is Java's ready-made text type. It gives text useful actions, called methods, such as counting letters or changing letter case.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
name.length() asks Java to count the letters in the text. name.toUpperCase() creates uppercase text for the result. The final line still prints Asha, because toUpperCase() does not overwrite the original text automatically. Save the result in another variable when you need to keep it.
Primitive and reference types: the beginner rule
You may see Java types divided into two groups. Use this simple rule first:
- Primitive types hold simple values:
int,double,char, andboolean. - Reference types describe more complex things:
String, arrays, and classes such asStudent.
The word “reference” is about how Java keeps track of a complex value. You do not need its memory details yet. The useful difference today is that a String has helpful methods such as length(), while an int is a number you calculate with.
Common beginner mistakes
Putting text into a number variable
// int lessons = "ten";This is wrong because "ten" is text and int accepts a whole number. Write int lessons = 10; when you need to calculate with the value. Use String lessons = "ten"; only when you truly want the written word.
Using the wrong quotes
// char initial = "J";
// String initial = 'J';The first line gives text to a one-character type. The second line gives one character to a text type. Use char initial = 'J'; or String initial = "J"; depending on what your program needs.
Try it yourself
Create a program named BookDetails that stores a book title, page count, rating, first letter of the title, and whether it is available.
Hint
Choose String for the title, int for pages, double for rating, char for the first letter, and boolean for availability. Print every value with a clear label.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Change the values and run it. Check whether the quotation marks still match the data type you selected.
Continue the lesson path