Variables and data types
On this page 8 topics
Overview
Topics
- Primitives
- reference types
- variables
- literals
- constants
- naming conventions
- type conversion
Learn first
83 min of reading · 4 h of practice (estimate)
Learning outcomes
- Declare and initialize variables of each primitive type and of
String, choosing an appropriate type for given data. - Explain the difference between primitive and reference types at a conceptual level.
- Apply Java naming conventions and use
finalfor constants. - Perform widening and narrowing conversions and predict data loss, including integer overflow.
- Use
varfor local variables where the type is obvious.
Lessons
- 01Variables as labeled boxes12 min read
- 02The eight primitive types12 min read
- 03Strings and reference types12 min read
- 04Literals, constants, and naming11 min read
- 05Type conversion and casting12 min read
- 06Local type inference with var12 min read
- 07Demo: unit converter and an integer overflow12 min read
Lab and exercises
Lab
BEG-009-LAB1 · Unit converter (fixed values)
Outcomes: 1–5 · Time: ~2 h Convert hard-coded temperatures, distances, and currencies and print the results with the correct types. Include one deliberate overflow and explain it in a comment. Acceptance: correct output; the overflow is explained.
Exercises (assessment: six exercises plus the type-selection quiz)
| ID | Outcome | Task |
|---|---|---|
| BEG-009-EX01 | 1 | Declare one variable of each type for: age, price in cents, temperature, first initial, is-student, full name. |
| BEG-009-EX02 | 2 | Explain in two sentences the difference between int and String in how they hold data. |
| BEG-009-EX03 | 3 | Rename these to Java style: Total_Price, maxspeed, a constant for 3.14159. |
| BEG-009-EX04 | 4 | Predict, then run: 7 / 2, 7 / 2.0, (int) 9.99, (double) 7 / 2. |
| BEG-009-EX05 | 4 | Predict the result of Integer.MAX_VALUE + 1, then run it and explain. |
| BEG-009-EX06 | 5 | Rewrite three declarations with var where the type is obvious; leave one that should not use it. |
Show solutions
- EX01
int age;int priceCents;double temperature;char initial;boolean isStudent;String fullName. - EX02
intholds the value directly;Stringholds a reference to a text object elsewhere. - EX03
totalPrice,maxSpeed,final double PI = 3.14159;. - EX04
3,3.5,9,3.5. - EX05
-2147483648: the value wraps past the maximum ofint. - EX06 For example
var count = 10;andvar name = "Ana";; avoidvar x = compute();.
Quiz
Outcomes: O1 declare · O2 primitive vs reference · O3 naming and final · O4 conversion · O5 var.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises BEG-009-EX01 to EX06 plus the lab. Pass: at least five exercises correct and the overflow explained.
Common mistakes
int division where decimals were expected; using double for money; == on strings (flagged here, explained in BEG-026).
Assessment
6 exercises plus a quiz on type selection.
Career relevance
Type choice for money returns in JNR-028 (BigDecimal/numeric) and data modeling.