21 topics

Java help, from public static void main to your first null pointer

Comparing two strings in Java with == is the single most common bug in first-year Java, and it is nasty because it sometimes works. Literals get pooled, so "cat" == "cat" is true, while a string built at runtime fails the same test. That inconsistency teaches the wrong lesson, and it is the sort of thing worth fixing early rather than after it has cost you an assignment.

Where students get stuck

Two strings that look identical come out as not equal

The == operator on objects compares references — whether both variables point at the same object — not the characters inside. Java keeps a pool of string literals, so two identical literals really are one object and == accidentally returns true. A string read from input or built with concatenation is a fresh object, so == returns false even though the text matches. Always use first.equals(second) for content, and equalsIgnoreCase when case should not matter.

Why does everything have to be inside public static void main

static means the method belongs to the class itself, not to any object of that class. When the JVM starts there are no objects yet, so the entry point has to be callable without one. That is also why a static method cannot touch instance fields directly: those live on objects that may not exist. The usual fix in an assignment is to keep main tiny — create one object of your class, then call ordinary instance methods on it and do the real work there.

NullPointerException and I cannot see which thing is null

The exception means you called a method or read a field on a reference that points at nothing. Read the stack trace line number, then look at every dot on that line: the object to the left of each dot is a suspect. The usual causes are an object field you declared but never assigned with new, an array created with new int[5] of objects where each slot is still null, and a method that returns null on a not-found case. Recent Java versions name the exact expression in the message, so read the full message before adding print statements.

ArrayList<int> will not compile

Generics only work with reference types, and int is a primitive. Write ArrayList<Integer> and Java autoboxes your ints into Integer objects on the way in and unboxes them on the way out. Two consequences to remember: comparing two Integer objects with == compares references again, so use .equals or compare intValue; and an Integer can be null, which is how you get a NullPointerException from what looks like plain arithmetic.

My average comes out as a whole number

Dividing two ints in Java gives an int, so 7 / 2 is 3 and the remainder is discarded before your double ever sees it. Declaring the result as a double does not help, because the truncation already happened. Force one operand to be a floating-point value first: (double) total / count, or total / (double) count. The same rule bites when you divide by a sum stored as an int, so cast at the division, not afterwards.

What's covered

Java Programming topics you can work through with a tutor, generate practice on, or turn into flashcards and a study plan.

Java fundamentals

  • Primitive types, casting, and integer versus floating-point division
  • Control flow: if, switch, while, for, and enhanced for
  • Methods, parameters, and return types
  • Arrays, 2D arrays, and array bounds
  • Strings, StringBuilder, and String methods

Object-oriented programming

  • Classes, constructors, and the this keyword
  • Encapsulation with private fields, getters, and setters
  • Inheritance, super, and method overriding
  • Polymorphism and dynamic dispatch
  • Abstract classes versus interfaces
  • Overriding equals and hashCode correctly

Collections and generics

  • ArrayList, LinkedList, and when each is faster
  • HashMap, HashSet, and TreeMap
  • Generic classes and methods
  • Iterators and the enhanced for loop
  • Comparable and Comparator for sorting

Errors, files, and tooling

  • Checked versus unchecked exceptions and throws declarations
  • try, catch, finally, and try-with-resources
  • File input and output with Scanner and BufferedReader
  • Compiling, classpath problems, and reading javac errors
  • JUnit tests and assertions

Java Programming questions

My course marks style and Javadoc. Does that get covered?

Yes. Paste or share the marking rubric and the grading feedback works against it, so naming conventions, field visibility, method length and Javadoc tags get flagged the way your marker would flag them. Style marks are usually the easiest ones to recover, because the rules are fixed and the fixes are mechanical.

Can it help when the compiler error makes no sense?

That is one of the better uses of it. Javac errors like cannot find symbol or incompatible types point at a line that is often not the real problem, and the first job is separating the reported line from the cause. Share the screen and it reads the full error list, because the second and third errors are frequently just fallout from the first.

Will using it count as academic misconduct?

That depends on your institution's policy, and you should read it rather than take our word. The safe use is the same as with a teaching assistant: getting an explanation of a concept or help diagnosing your own bug is normally fine, and submitting code you cannot explain is not. If your course bans AI assistance outright, that ban applies here too.

Can it quiz me before a written Java exam?

Yes, and written Java exams have a particular shape — trace this loop and state the output, spot the compile error, write a method by hand without an IDE. Generated practice quizzes come back with full worked solutions, so you can check a trace step by step rather than only seeing whether the final answer matched.

Stuck on java programming right now?

Talk it through out loud, share your screen, and watch it worked out step by step on a whiteboard.

Start free — no card