Loading…
Loading…
Language
Articles and problems tagged Java.
Learn the remaining kinds of Java nested classes: local classes inside methods, capturing effectively final variables, anonymous inner classes and their syntax, var with anonymous types, double brace initialization, the getEnclosingClass trick, and static nested classes and records.
Understand Java inner classes: why they exist, how an inner object accesses the private fields of its outer object, the hidden this$0 reference, OuterClass.this and outer.new Inner() syntax, static members in inner classes (Java 16), and how the compiler translates them.
Go deeper with Java lambdas: closures and effectively final variables, this inside a lambda, writing methods that accept lambdas, the complete java.util.function tables, combining predicates and functions, String.transform, and building comparators with comparing, thenComparing and nullsFirst.
Master Java method references (::): object::instanceMethod, Class::instanceMethod, Class::staticMethod, this:: and super::, how overloads are chosen, the null difference from lambdas, and constructor references such as Person::new and Person[]::new for streams and generic arrays.
Learn Java lambda expressions from scratch: why lambdas exist, every syntax form (inferred types, block bodies, no parameters, var, unnamed _), functional interfaces and @FunctionalInterface, java.util.function types like Predicate, Supplier and BiFunction, and primitive specializations.
Three classic uses of Java interfaces: callbacks with listener interfaces and timers, custom sort orders with Comparator and its contract, and object cloning with Cloneable, shallow vs deep copies, covariant clone, array clone, and modern alternatives like copy constructors.
Since Java 8 and 9, interfaces can contain code. Learn static factory methods in interfaces, private helper methods, default methods and why they enable interface evolution, source vs binary compatibility, and the rules for resolving default method conflicts with X.super.method().
Learn Java interfaces from the ground up: interfaces as contracts, implementing Comparable to sort objects, the compareTo rules and pitfalls with doubles, interface variables and instanceof, extending interfaces, constants, multiple interfaces, and interfaces versus abstract classes.
Seven practical rules for Java class hierarchies: put common code in superclasses, avoid protected fields, inherit only for true is-a relationships, keep inherited methods meaningful, honor the substitution principle, prefer polymorphism over type tests, and use reflection sparingly.
Finish Java reflection: create arrays of any type with java.lang.reflect.Array, grow arrays generically like Arrays.copyOf, call methods with Method.invoke, invoke constructors with arguments, handle InvocationTargetException, and compare reflection with method handles and lambdas.
Use Java reflection to inspect any class at runtime: Field, Method, Constructor and RecordComponent, getDeclaredFields vs getFields, Modifier and AccessFlag, sealed and record metadata, reading and writing fields with setAccessible, module restrictions, and a generic toString.
Start with Java reflection: what it is for, getting Class objects with getClass, Class.forName and .class literals, odd array type names, comparing Class objects, creating instances by name, a primer on checked exceptions, and loading resources with getResource and getResourceAsStream.
Master pattern matching for switch (Java 21+): type patterns, record patterns, unnamed variables, case null, guards with when, exhaustiveness rules, dominance, constant labels, MatchException, and scope and fall-through rules, with runnable examples.
Java sealed classes and interfaces (final since Java 17) let you list exactly which classes may extend a type. Learn sealed, permits, final and non-sealed, the placement rules, exhaustive switch without default, records and enums in sealed hierarchies, and when to choose sealed types.
Java enums are full classes with a fixed set of instances. Learn to compare with ==, use name, valueOf, values, ordinal and compareTo, add fields, constructors and methods, write constant-specific behavior, switch over enums, and use EnumMap and EnumSet.
Learn when and how to use abstract classes in Java: declaring abstract methods as placeholders, mixing fields and concrete methods, why abstract classes can't be instantiated, abstract variables referring to concrete subclasses, the template method pattern, and abstract classes versus interfaces.
Write Java methods that accept any number of arguments with varargs (Type... args): how the compiler packs arguments into an array, passing arrays directly, rules and restrictions, overloading with varargs, the null ambiguity, @SafeVarargs and heap pollution, and performance tips.
Java's wrapper classes Integer, Double, Boolean and friends explained: why ArrayList<int> isn't allowed, how autoboxing and unboxing work, the == trap and the Integer cache, NullPointerException on unboxing, parseInt and other static helpers, MIN_VALUE pitfalls, and performance costs.
Use Java's ArrayList, the resizable array: declaring ArrayList<Employee> with var and the diamond operator, add, get, set, insert and remove, size versus capacity, ensureCapacity and trimToSize, for-each loops, converting to arrays, Java 21 getFirst/getLast, and raw-type compatibility.
How Java's hashCode and toString methods work and how to override them: the equals/hashCode contract, why HashSet breaks without it, Objects.hash and Arrays.hashCode, avoiding collisions, writing toString with getClass().getName(), printing arrays, and identity versions of both.