AlgoGalaxy

AlgoGalaxy

University
CodeRush
Jobs
Tools
Insights
Explore
Personalize
KotlinGoPHPRustGraphsArraysBit ManipulationDynamic ProgrammingGreedyHeapLinked ListStackStringsTreesTrieBacktrackingBinary SearchBSTComplexity AnalysisData Structures & Algorithms

Java: Java | Generics - Foundations and Vocabulary

Let us study Java | Generics like we would in a careful Java review. We will read a small executable example, predict its output, then change one boundary only after we can explain the original contract.

Foundations and Vocabulary is not a checklist to memorize. It is the part of Java design where clear names, deliberate ownership, and observable failures save us from debugging a vague symptom later.

What We Will Practise Together

We are practising more than syntax. For every example, state what Java guarantees, what the caller can observe, and what should happen when the input is missing, invalid, or late.

  • Keep the contract smaller than the explanation around it.
  • Use the type system and resource boundaries to make invalid states difficult.
  • Test the normal path and the closest failure path together.
  • Do not log secrets or pretend a failure is a success.

Example 1: Make a generic container

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

record Box<T>(T value) {}

public class Main {
  public static void main(String[] args) {
    Box<String> box = new Box<>("lesson");
    System.out.println(box.value());
  }
}

Expected Result

lesson

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 2: Write a generic method

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

public class Main {
  static <T> T first(T left, T right) { return left; }

  public static void main(String[] args) {
    System.out.println(first("cyan", "amber"));
  }
}

Expected Result

cyan

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 3: Use the compiler to protect a list

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Integer> scores = List.of(7, 9, 10);
    System.out.println(scores.get(1));
  }
}

Expected Result

9

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 4: Read through an extends wildcard

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

import java.util.List;

public class Main {
  static double total(List<? extends Number> values) {
    return values.stream().mapToDouble(Number::doubleValue).sum();
  }

  public static void main(String[] args) {
    System.out.println(total(List.of(2, 3, 4)));
  }
}

Expected Result

9.0

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 5: Write through a super wildcard

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

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

public class Main {
  static void addDefaults(List<? super Integer> values) { values.add(5); }

  public static void main(String[] args) {
    List<Number> values = new ArrayList<>();
    addDefaults(values);
    System.out.println(values.get(0));
  }
}

Expected Result

5

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 6: Bound a reusable calculation

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

public class Main {
  static <T extends Number> double twice(T value) { return value.doubleValue() * 2; }

  public static void main(String[] args) {
    System.out.println(twice(6));
  }
}

Expected Result

12.0

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 7: Keep API meaning in the type

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

record Id<T>(String value) {}
record User(String name) {}

public class Main {
  public static void main(String[] args) {
    Id<User> id = new Id<>("user-7");
    System.out.println(id.value());
  }
}

Expected Result

user-7

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 8: Avoid raw types

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

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

public class Main {
  public static void main(String[] args) {
    List<String> names = new ArrayList<>();
    names.add("Asha");
    System.out.println(names.size());
  }
}

Expected Result

1

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 9: Let inference remove repetition

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

import java.util.Map;

public class Main {
  public static void main(String[] args) {
    var status = Map.of("ready", true);
    System.out.println(status.get("ready"));
  }
}

Expected Result

true

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Example 10: Review producer and consumer roles

Let us trace this Java decision together. First say what the input represents. Then identify the exact rule Java applies. Finally compare the expected output with what you predicted before editing the snippet.

A strong implementation keeps the nearest failure, type boundary, or resource owner visible. That makes this example safe to reuse instead of merely impressive in isolation.

Java

import java.util.List;

public class Main {
  static void printAll(List<? extends CharSequence> values) {
    values.forEach(System.out::println);
  }

  public static void main(String[] args) { printAll(List.of("input", "result")); }
}

Expected Result

input
result

Teacher check: alter one input or boundary, predict the new output, and explain the behavior in Java terms before you move on.

Teacher Review

Before we close foundations and vocabulary, narrate one example without looking at the code. If you can state the input, rule, output, and boundary in order, you have a reusable Java decision rather than an isolated answer.

Now carry one example into a small project. Keep its expected result beside the code while you change it. That is how we build reliable Java judgment one visible contract at a time.

Practice Before You Move On

Choose one snippet and make a small deliberate change. Write the predicted result first, then run it. If the result surprises you, reduce the example until every step is explainable.

logo of AlgoGalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

AlgoGalaxy University

University HomeTechnology LearningCoding ProblemsLearning ProgressCareer OpportunitiesTechnology InsightsUtilNexa ToolsAlgoGalaxy HomeContact Support

AlgoGalaxy CodeRush

CodeRush HomeProblem SetsProgress TrackerLearn TechnologyTechnology JobsEngineering InsightsDeveloper UtilitiesAlgoGalaxy HomePrivacy Policy
Privacy Policy

 • 

Terms of Service

Copyright © AlgoGalaxy 2026