Java + Spring Boot track

Lesson 02 · Part 1: Basics

Variables and Types

A variable is a “labeled box” that holds data. Java is a static type language, so you must decide in advance what type of value each variable will contain at the time…

Part 1: Basics4 min readFree lesson

A variable is a “labeled box” that holds data. Java is a static type language, so you must decide in advance what type of value each variable will contain at the time of declaration. In this section, we look at eight Java primitives, type conversion, and the var keyword of JDK 10+.

Learning Objectives

  • Know the sizes and default values of eight primitive types
  • Understand when type conversion (implicit and explicit) occurs
  • Distinguishes between locations where var can be used and locations where it cannot be used.
  • Know the difference between String literal and new String()

Key concepts

1) 8 types of Primitive

Type size default range
byte 1 byte 0 -128 ~ 127
short 2 byte 0 -32,768 ~ 32,767
int 4 bytes 0 Approximately ±2.1 billion
long 8 bytes 0L Approximately ±9 × 10^18
float 4 bytes 0.0f IEEE 754 single precision
double 8 bytes 0.0d IEEE 754 double precision
boolean JVM dependency false true / false
char 2 bytes ' ' UTF-16 code units
int age = 21;
long population = 50_000_000L;   //long literals have an L at the end
double pi = 3.14159;
boolean done = false;
char grade = 'A';

2) Type conversion

int i = 100;
double d = i;          //int → double : implicit (value preservation)
int back = (int) d;    //double → int : explicit (truncate decimal point)

Small type → large type is automatically converted, but the reverse requires explicit casting due to the possibility of data loss.

3) var keyword (JDK 10+)

var name = "exponent";    //Inference with String
var nums = new int[]{1, 2, 3};
  • Can only be used in local variables (field/method signatures are not allowed)
  • Since the type is inferred from the initial value, value must be present at the same time as declaration.
  • Recommended to use only when it helps readability

4) String literal vs new String()

String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b);          //true (string pool shared)
System.out.println(a == c);          //false (another object)
System.out.println(a.equals(c));     //true (content comparison)

See as example

Example 1 — Primitives.java: 8 primitive outputs

public class Primitives {
    public static void main(String[] args) {
        byte b = 10;
        short s = 200;
        int i = 100_000;
        long l = 9_999_999_999L;
        float f = 3.14f;
        double d = 3.141592653589793;
        boolean t = true;
        char c = 'A';
        System.out.printf("byte=%d, short=%d, int=%d, long=%d%n", b, s, i, l);
        System.out.printf("float=%.2f, double=%.6f%n", f, d);
        System.out.printf("boolean=%b, char=%c(%d)%n", t, c, (int) c);
    }
}

Execution result

byte=10, short=200, int=100000, long=9999999999
float=3.14, double=3.141593
boolean=true, char=A(65)

Note: Large numbers can be placed as _ (100_000).

Example 2 — Conversion.java: Casting trap

public class Conversion {
    public static void main(String[] args) {
        int i = 100;
        double d = i;
        System.out.println("int -> double: " + d);

        double pi = 3.99;
        int truncated = (int) pi;
        System.out.println("double -> int (cut):" + truncated);

        int big = 1_000;
        byte overflow = (byte) big;
        System.out.println("byte overflow:" + overflow);
    }
}

Execution result

int -> double: 100.0
double -> int (cut): 3
byte overflow: -24

Memo: If it is outside the byte range (-128 to 127), bits are truncated and unpredictable values are produced.

Example 3 — VarKeyword.java : possible/impossible case using var

import java.util.ArrayList;
import java.util.List;

public class VarKeyword {
    public static void main(String[] args) {
        var name = "exponent";
        var list = new ArrayList<String>();
        list.add("Java");
        list.add("Spring");

        for (var s : list) {
            System.out.println(s);
        }

        //var x;          // Compilation error: initial value required
        //var n = null;   // Compilation error: type inference not possible
        List<Integer> nums = List.of(1, 2, 3);
        System.out.println("Name:" + name);
        System.out.println("The types are:" + nums.getClass().getSimpleName());
    }
}

Execution result

Java
Spring
Name: Jisoo
The type is: ListN

Memo: Field/parameter/return-type var cannot be used. This is limited to local variables.

Example 4 — StringBasics.java: String literal vs new String()

public class StringBasics {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = new String("hello");
        System.out.println("a == b : " + (a == b));
        System.out.println("a == c : " + (a == c));
        System.out.println("a.equals(c) : " + a.equals(c));
        System.out.println("length : " + a.length());
        System.out.println("upper  : " + a.toUpperCase());
    }
}

Execution result

a == b : true
a == c : false
a.equals(c) : true
length : 5
upper  : HELLO

Note: Always use .equals()** to compare string **contents in Java. == is a reference (address) comparison.

Common Mistakes

  1. Omitting L in the long literal → interpreted as int and overflow
  2. Omit f in float literal → Default is double, casting required
  3. I forgot that int / int is integer division and got confused by 1 / 2 == 0.
  4. Not knowing that char is compatible with small integers (0~65535)
  5. Use == for string comparison

Cleanup

  • Debugging is easy if you memorize Java's 8 primitives and their default values.
  • The principle of type conversion is small → large automatically, large → small explicit casting.
  • var is only a readability tool, not an all-purpose keyword.
  • The string content is .equals(), the reference is ==

Try it yourself

cd 01_Basics/02_Variables-and_Types/src
javac Primitives.java
java Primitives

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

src/Conversion.javaJava
public class Conversion {
    public static void main(String[] args) {
        int i = 100;
        double d = i;
        System.out.println("int -> double: " + d);

        double pi = 3.99;
        int truncated = (int) pi;
        System.out.println("double -> int (cut):" + truncated);

        int big = 1_000;
        byte overflow = (byte) big;
        System.out.println("byte overflow:" + overflow);
    }
}
src/Primitives.javaJava
public class Primitives {
    public static void main(String[] args) {
        byte b = 10;
        short s = 200;
        int i = 100_000;
        long l = 9_999_999_999L;
        float f = 3.14f;
        double d = 3.141592653589793;
        boolean t = true;
        char c = 'A';
        System.out.printf("byte=%d, short=%d, int=%d, long=%d%n", b, s, i, l);
        System.out.printf("float=%.2f, double=%.6f%n", f, d);
        System.out.printf("boolean=%b, char=%c(%d)%n", t, c, (int) c);
    }
}
src/StringBasics.javaJava
public class StringBasics {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = new String("hello");
        System.out.println("a == b : " + (a == b));
        System.out.println("a == c : " + (a == c));
        System.out.println("a.equals(c) : " + a.equals(c));
        System.out.println("length : " + a.length());
        System.out.println("upper  : " + a.toUpperCase());
    }
}
src/VarKeyword.javaJava
import java.util.ArrayList;
import java.util.List;

public class VarKeyword {
    public static void main(String[] args) {
        var name = "exponent";
        var list = new ArrayList<String>();
        list.add("Java");
        list.add("Spring");

        for (var s : list) {
            System.out.println(s);
        }

        List<Integer> nums = List.of(1, 2, 3);
        System.out.println("Name:" + name);
        System.out.println("The types are:" + nums.getClass().getSimpleName());
    }
}

Assignment

Problem 1 — My Profile Variables

  • File name: Homework01.java
  • Core concept: primitive type declaration, printf

Requirements

  • Declare the variables name (String), age (int), height (double), student status (boolean), and grade (char).
  • Print everything on one line with printf.

Expected output

Name=Hong Gil-dong, age=21, height=175.30, student=true, grade=A

Hint

  • printf Format: %s %d %.2f %b %c

Problem 2 — Four basic operations

  • File name: Homework02.java
  • Core concepts: variables, integer/real operation, explicit type conversion

Requirements

  • Declare two integers a = 17 and b = 5.
  • Outputs sum, difference, product, quotient (integer division), remainder, and real number division.

Expected output

17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
17 / 5 (real number) = 3.40

Hint

  • For the last line, you only need to cast one side like (double) a / b.

Check the correct answer

After solving it yourself, compare it with the reference solution below.

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

Homework01.javaJava
/** Output profile variables.*/
public class Homework01 {
    public static void main(String[] args) {
        String name = "Hong Gil-dong";
        int age = 21;
        double height = 175.30;
        boolean student = true;
        char grade = 'A';
        System.out.printf("Name=%s, Age=%d, Height=%.2f, Student=%b, Grade=%c%n",
                name, age, height, student, grade);
    }
}
Homework02.javaJava
/** Four arithmetic operations of two integers.*/
public class Homework02 {
    public static void main(String[] args) {
        int a = 17;
        int b = 5;
        System.out.printf("%d + %d = %d%n", a, b, a + b);
        System.out.printf("%d - %d = %d%n", a, b, a - b);
        System.out.printf("%d * %d = %d%n", a, b, a * b);
        System.out.printf("%d / %d = %d%n", a, b, a / b);
        System.out.printf("%d %% %d = %d%n", a, b, a % b);
        System.out.printf("%d / %d (real number) = %.2f%n", a, b, (double) a / b);
    }
}

Learn this lesson with a mentor

Work through the Java + Spring Boot track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now