Values
After this: Tell a number from text from a yes/no, and say what + does to each.
On this page · 7 sections · 3 checks
Think about a form at a doctor's office. It asks for your age, your name, and whether you have been there before. The age is a number. The name is text. The last answer is yes or no. Each answer is a different kind of information.
A program works with the same kinds. The last lesson printed text and a number. This lesson names the kinds and shows what each one can do.
A value is one piece of information
A is one piece of information. 42 is a value. "Ana" is a value. The answer to 7 > 6 is a value. You meet four kinds in this lesson.
Whole numbers
An is a whole number: 42, 0, -3. You write it with digits and nothing else. No quotes.
Numbers with a fraction part
A is a number with a decimal point: 3.5, 0.25. The name is short for "floating point", which is how the computer stores the point. 2.0 is a float even though it has no fraction, because of the point. Java names this kind double; the idea is the same.
Text
Text is written inside quotes: "Ana", "Hi ". The quotes tell the computer where the text starts and ends. They are not part of the value. Anything inside quotes is text, including digits: "42" is text, not a number, and the computer will not do arithmetic with it. Lesson 5 shows where that leads.
Yes or no
A is a yes/no value. There are only two, written Truetrue and Falsefalse. You rarely type them. They come out of questions: 7 > 6 asks "is seven greater than six?", and the answer is Truetrue.
Watch three values printed
print(42)print("Ana")print(7 > 6)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The first line prints a number. The second prints text, without its quotes. The third asks a question and prints the answer, Truetrue. Here are all four kinds in one program.
print(42) # a whole numberprint(3.5) # a number with a fraction partprint("Ana") # textprint(7 > 6) # a yes/no: is 7 greater than 6?42 3.5 Ana True
System.out.println(42); // a whole numberSystem.out.println(3.5); // a number with a fraction partSystem.out.println("Ana"); // textSystem.out.println(7 > 6); // a yes/no: is 7 greater than 6?42 3.5 Ana true
What + does depends on the kind
The plus sign does two different jobs. On two numbers, it adds them. On two pieces of text, it joins them end to end.
print(3 + 4)print("Hi " + "Ana")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
3 + 4: two numbers, so the computer adds. It prints7."Hi " + "Ana": two pieces of text, so the computer joins them. It printsHi Ana. The space afterHiis inside the quotes, so it is part of the text.
The rule is the same in both languages: the kinds of the two values decide what + does.
Run it
Change the name inside the quotes, or the numbers, and run it again.
The four kinds in one table
| Kind | Looks like | Example |
|---|---|---|
| integer | digits, no point | 42 |
| float | digits with a decimal point | 3.5 |
| text | anything inside quotes | "Ana", "42" |
| boolean | one of two words | Truetrue |
Check yourself
Which of these values is text?
What does this print?
print(2 + 2)print("Hi " + "Bo")print(2 > 3)System.out.println(2 + 2);System.out.println("Hi " + "Bo");System.out.println(2 > 3);What kind of value is 3.5?
What comes next
A program needs to keep a value and use it again later. The next lesson gives a value a name.
- Say what a value is: one piece of information.
- Tell the four kinds apart: integer, float, text in quotes, and the yes/no boolean.
- Say that digits inside quotes are text, not a number.
- Say what + does: it adds two numbers and joins two pieces of text.