Skip to main content

Java Basics

Data Types & I/O

Complete lesson for Data Types & I/O using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Data Types & I/O

Section: Java Basics. Primitive types, size and range, type conversion, and console I/O with System.out and Scanner.

Primitive types

  • Eight primitives: byte, short, int, long, float, double, char, boolean. They are not objects.
  • Integer literals are int by default; use L for long. Floating literals are double by default; use f for float.
  • char is an unsigned 16-bit UTF-16 code unit, not a full Unicode code point (supplementary characters need two chars).
TypeSizeRange / notesDefault (fields)
byte8-bit-128 to 1270
short16-bit-32,768 to 32,7670
int32-bit≈ ±2.1 billion0
long64-bithuge integers; suffix L0L
float32-bit IEEEapprox. 6–7 digits; suffix f0.0f
double64-bit IEEEapprox. 15 digits0.0
char16-bit0 to 65535 (UTF-16 code unit)'\u0000'
booleanJVM-definedtrue / falsefalse

Reference types and local vs field defaults

  • Classes, arrays, interfaces, and enums are references. Uninitialized *fields* are null (or zero/false for primitives).
  • Local variables have **no** default — you must assign before use or javac fails.
  • var (Java 10+) infers a local type from the initializer: var name = "Ada"; is String. You cannot write var x; with no initializer.

Console output and Scanner input

  • System.out.print / println / printf write to standard output.
  • Scanner in java.util reads tokens from System.in. Close it only if you own the stream; closing System.in is often a mistake in small demos.
import java.util.Scanner;

public class IoDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Age: ");
        int age = sc.nextInt();
        sc.nextLine(); // consume leftover newline
        System.out.print("Name: ");
        String name = sc.nextLine();
        System.out.printf("%s is %d years old%n", name, age);
        sc.close();
    }
}

Widening vs narrowing conversion

  • Widening (int → long → float → double) is implicit. Narrowing (double → int) needs a cast and truncates toward zero.
  • Integer overflow wraps silently (Integer.MAX_VALUE + 1 is Integer.MIN_VALUE).
L2

Advanced — Data Types & I/O

Scanner pitfalls and numeric precision

  • nextInt() does not consume the line ending. Mixing nextInt() and nextLine() is a classic bug — call nextLine() to clear the rest of the line.
  • next() reads one token (whitespace-delimited); nextLine() reads the rest of the line.
  • float/double are binary floating point — 0.1 + 0.2 is not exactly 0.3. Use BigDecimal for money.
  • Never use == to compare doubles for equality in real code; compare with an epsilon or Double.compare.
L3

Tricky Interview Points

Q1) How many primitive types does Java have?

Answer Eight: byte, short, int, long, float, double, char, boolean. String is a class, not a primitive.

Q2) What is the default value of a local int?

Answer There is none. Locals must be assigned before use. Fields of type int default to 0.

Q3) Why do we write 10L and 1.5f?

Answer Integer literals are int; 10L is long. Floating literals are double; 1.5f is float so it can be assigned to a float variable without a cast.

Q4) Is char signed?

Answer No. char is an unsigned 16-bit UTF-16 code unit.

Q5) What is the difference between print and println?

Answer println appends a newline (the system line separator). print does not.

Q6) Why does nextLine() return empty after nextInt()?

Answer nextInt() leaves the newline in the buffer. The following nextLine() consumes that empty remainder.

L4

Simple Programs

Primitives, cast, and formatted output

public class TypesDemo {
    public static void main(String[] args) {
        int whole = 10;
        double widened = whole;      // implicit
        int truncated = (int) 9.99;  // 9
        long big = 3_000_000_000L;
        System.out.println(widened);
        System.out.println(truncated);
        System.out.println(big);
    }
}

Read two numbers and print the sum

import java.util.Scanner;

public class SumInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println("sum = " + (a + b));
        sc.close();
    }
}
L5

Problem Solving Tricks

  • Prefer int for whole numbers unless you need long range. Prefer double for science; BigDecimal for currency.
  • After nextInt() / nextDouble(), call nextLine() before reading a full line.
  • printf("%n") is portable; "\n" is always LF.
  • var only works for locals with initializers — not for fields or method parameters.
L6

Practice Programs List

  1. Print the min and max of every primitive using Integer.MIN_VALUE, Byte.MAX_VALUE, and so on.
  2. Read a first name and age, then print a sentence with printf.
  3. Cast 130 to byte and explain the wrap-around result.
  4. Show that 0.1 + 0.2 == 0.3 is false and print both sides.
  5. Write a program that mixes nextInt and nextLine correctly.