Skip to content
Ameen Abozaid
BEG-009

Variables and data types

  • 7 Lessons
  • 83 min of reading
  • 4 h practice (estimate)
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

  1. Declare and initialize variables of each primitive type and of String, choosing an appropriate type for given data.
  2. Explain the difference between primitive and reference types at a conceptual level.
  3. Apply Java naming conventions and use final for constants.
  4. Perform widening and narrowing conversions and predict data loss, including integer overflow.
  5. Use var for local variables where the type is obvious.

Lessons

  1. 01Variables as labeled boxes12 min read
  2. 02The eight primitive types12 min read
  3. 03Strings and reference types12 min read
  4. 04Literals, constants, and naming11 min read
  5. 05Type conversion and casting12 min read
  6. 06Local type inference with var12 min read
  7. 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)

IDOutcomeTask
BEG-009-EX011Declare one variable of each type for: age, price in cents, temperature, first initial, is-student, full name.
BEG-009-EX022Explain in two sentences the difference between int and String in how they hold data.
BEG-009-EX033Rename these to Java style: Total_Price, maxspeed, a constant for 3.14159.
BEG-009-EX044Predict, then run: 7 / 2, 7 / 2.0, (int) 9.99, (double) 7 / 2.
BEG-009-EX054Predict the result of Integer.MAX_VALUE + 1, then run it and explain.
BEG-009-EX065Rewrite 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 int holds the value directly; String holds 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 of int.
  • EX06 For example var count = 10; and var name = "Ana";; avoid var 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.

  1. 1Best type for a person's age:O1
  2. 2Best type for true/false:O1
  3. 3Best type for a full name:O1
  4. 4Which type should you NOT use for money in real programs?O1
  5. 5String is a:O2
  6. 6The Java style for a variable name is:O3
  7. 7final on a variable means it:O3
  8. 87 / 2 in Java gives:O4
  9. 9(int) 3.9 gives:O4
  10. 10var x = 10; makes x a(n):O5

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.

Type to search levels, phases, modules and lessons.