Fundamentals 8 min read

Unlock Java Basics: Bytecode, Reference Passing, and Common Interview Puzzles

This article walks through core Java concepts—including the compilation to bytecode, how reference types are passed, try‑catch‑finally return rules, servlet lifecycle stages, constructor fundamentals, and detailed analyses of several interview‑style code snippets—providing clear explanations and expected outputs.

FunTester
FunTester
FunTester
Unlock Java Basics: Bytecode, Reference Passing, and Common Interview Puzzles

Java Compilation Process

In Java, the compiler translates source code into bytecode stored in .class files (Answer A). The class loader loads these files into the JVM, where the execution engine converts bytecode to executable code, which the operating system finally runs as machine code.

Reference Passing in Java

When a reference type is passed as an argument, only the reference is copied; the actual object is not duplicated.

Diagram illustrating reference passing
Diagram illustrating reference passing

Try‑Catch‑Finally Return Rules

The following code demonstrates how return statements interact with finally blocks:

public class Demo{
    public static void main(String[] args){
        System.out.print(getNumber(0));
        System.out.print(getNumber(1));
        System.out.print(getNumber(2));
        System.out.print(getNumber(4));
    }
    public static int getNumber(int num){
        try{
            int result = 2 / num;
            return result;
        }catch (Exception exception){
            return 0;
        }finally{
            if(num == 0){
                return -1;
            }
            if(num == 1){
                return 1;
            }
        }
    }
}

Key rules:

Both catch and finally blocks are optional, but at least one must exist.

If an exception occurs in the try block, execution jumps to the catch block, skipping remaining try statements.

The finally block always executes, regardless of whether an exception was thrown.

If a return occurs in try or catch, the finally block runs before the method returns.

A return inside finally overrides any previous return.

The only way to skip finally is to terminate the JVM with System.exit(1).

Servlet Lifecycle Overview

The servlet lifecycle consists of five stages: loading, instantiation, initialization, request processing, and unloading.

Loading: the container loads the servlet class via its class loader.

Instantiation: the container creates a servlet instance by invoking the constructor.

Initialization: the container calls the init() method.

Request processing: for each client request, the container creates a thread and invokes service(), which dispatches to doGet() or doPost() as appropriate.

Unloading: the container calls destroy() to release resources.

Constructor Knowledge Check

Correct statements about constructors:

A constructor’s priority is lower than that of static members or static blocks.

A constructor has no return type.

The primary purpose of a constructor is to initialize a new object.

The constructor is automatically invoked when an object is created with new.

Loop and Boolean Method Puzzle

The following program prints a sequence based on a custom out(char) method that returns true after printing the character.

public class Print{
    static boolean out(char c){
        System.out.println(c);
        return true;
    }
    public static void main(String[] argv){
        int i = 0;
        for(out('A'); out('B') && (i<2); out('C')){
            i++;
            out('D');
        }
    }
}

Execution steps:

Print ‘A’.

Print ‘B’; condition true (i=0); enter loop.

Increment i, print ‘D’, then print ‘C’.

Repeat: print ‘B’; condition true (i=1); enter loop, print ‘D’ and ‘C’.

Next iteration: print ‘B’; condition false (i=2); exit loop.

Final output: ABDCBDCB.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaprogrammingcode analysisinterviewbasics
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.