skip to content

Core Language Features

The everyday mechanics that neither fit under OOP nor under fundamentals: strings, arrays, packages and imports, static members, and initialization order. Interviewers touch this constantly because strings and initialization order power many puzzle questions.

part ofJavaoverview, primer and where to startread it →
on this pageshow

explore

questions

109 · 5 sections

What do indexOf and lastIndexOf do in Java, and how do their fromIndex variants and return values behave?

level: juniorimportance: must knowfreq 70%
basics
~10 s

indexOf finds the first position of a character or substring and returns that index, or -1 if not found. lastIndexOf does the same but finds the last (rightmost) occurrence.

open as a page

How does String.substring() work in Java, including its index arguments and what happens to the original string?

level: juniorimportance: must knowfreq 78%
basics
~10 s

substring(begin, end) returns the part of the string from begin up to but not including end. The original string is unchanged because strings are immutable; you get a new string back.

open as a page

Why do we use StringBuilder or StringBuffer to build strings instead of repeatedly concatenating with the + operator in a loop?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Java Strings are immutable, so each + creates a brand-new String object. In a loop that copies the text over and over, which is slow and wasteful. StringBuilder edits one mutable buffer in place, so it is much faster.

open as a page

How does the + operator work for joining strings in Java, and what does it produce?

level: juniorimportance: must knowfreq 80%
basics
~10 s

The + operator joins two strings into a new string. Because String objects can't be changed, every + makes a brand-new String holding the combined text; the originals stay untouched.

open as a page

In Java, what is the difference between using == and equals() to compare two String objects?

level: juniorimportance: must knowfreq 90%
basics
~10 s

== checks whether two String variables point to the same object in memory. equals() checks whether the two strings contain the same characters. To compare text, always use equals().

open as a page

What happens in Java when you access an array element with an index that is outside the array's valid range?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Java checks every array access at runtime. If the index is below 0 or at/above the array's length, it throws an ArrayIndexOutOfBoundsException instead of reading wrong memory.

open as a page

Why is a Java array fixed-length once created, and how do you handle a collection that needs to grow?

level: juniorimportance: must knowfreq 60%
basics
~20 s

An array's length is set when you create it and can never change. To grow, you either create a bigger array and copy the elements over, or use a class like ArrayList that does that copying for you.

open as a page

What are the ways to declare an array variable in Java, and is there a difference between int[] a and int a[]?

level: juniorimportance: must knowfreq 70%
basics
~20 s

You can write the brackets after the type, like int[] a, or after the variable name, like int a[]. Both declare an array of ints and behave identically. The first style is preferred in Java.

open as a page

How do you get the size of an array in Java, and why is it a field rather than a method?

level: juniorimportance: must knowfreq 68%
basics
~10 s

You read the public length field, like a.length (no parentheses). It tells you how many elements the array has. It is fixed for the array's lifetime and you cannot change it.

open as a page

How do you create (instantiate) an array in Java with the new keyword, and what does the size argument do?

level: juniorimportance: must knowfreq 75%
basics
~20 s

Use new with the element type and a length, like new int[5]. This allocates an array of 5 ints on the heap. The length is fixed for the array's lifetime, and you fill the slots by index afterwards.

open as a page

Why don't you need to import String, Integer, or Object, and what makes the java.lang package special?

level: juniorimportance: must knowfreq 58%
basics
~10 s

Every Java file automatically imports the java.lang package, where String, Integer, Object, System, and similar core types live. So you can use them by simple name without writing any import.

open as a page

What is the difference between a single-type import and a wildcard (on-demand) import in Java, and when would you prefer each?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A single-type import names one class, like import java.util.List;. A wildcard import, like import java.util.*;, lets you use any class from that package. Single imports are clearer and usually preferred.

open as a page

What is a package declaration in Java, and how does it relate to the directory structure of your source files?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A package declaration is the first line of code in a Java file, like package com.example.app;. It groups related classes under a name. The folders on disk must match the package name: com.example.app lives in com/example/app/.

open as a page

Why can you use String, System, and Integer without importing them, and what is special about java.lang?

level: juniorimportance: should knowfreq 50%
basics
~10 s

String, System, Integer and similar core types live in the package java.lang, which Java imports automatically into every file. So you don't write import java.lang.String; — it's always already available.

open as a page

What is the reverse-domain-name convention for Java packages, and why is it used?

level: juniorimportance: should knowfreq 55%
basics
~20 s

You name packages by reversing your organization's internet domain. If your domain is example.com, your packages start with com.example. This makes names globally unique, since domains are unique, so your classes won't clash with anyone else's.

open as a page

What is a static initializer block in Java, and why would you use one instead of just initializing static fields inline?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A static block is code inside static { ... } that runs once when the class is first loaded. You use it to set up static fields when the setup needs more than one line, like a loop, a try/catch, or several steps.

open as a page

What does the `static` keyword mean when applied to a field or method in Java?

level: juniorimportance: must knowfreq 80%
basics
~20 s

static means the member belongs to the class itself, not to any one object. There is one shared copy for all instances, and you call it on the class (e.g. Math.max) without creating an object.

open as a page

When a class has multiple static initializer blocks and static field initializers interleaved, in what order do they execute?

level: middleimportance: must knowfreq 60%
basics
~20 s

They run top to bottom, in the exact order they appear in the source code. Static field initializers and static blocks are treated as one combined sequence, and each runs once when the class is initialized.

open as a page

Why can't a static method directly access instance variables or instance methods, and how do you work around it?

level: middleimportance: must knowfreq 72%
basics
~20 s

A static method runs without any particular object, so there is no this to point at instance data. Instance variables belong to objects, so the static method has no object to read them from. To use them, pass or create an object and access them through it.

open as a page

How does static initializer execution relate to instance initializers and constructors across a class hierarchy when you create an object?

level: middleimportance: should knowfreq 40%
basics
~20 s

Static blocks run first and only once (when the class is first loaded). Then for each new object: the parent's instance setup and constructor run, then the child's. Static = once per class; instance initializers/constructors = once per object.

open as a page

What are the rules for initializing a final field in Java, and what does 'definitely assigned exactly once' mean?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A final field must be given a value exactly once and can never be changed afterward. You can set it where you declare it, in an instance initializer block, or in every constructor. If you try to leave it unset or assign it twice, the code will not compile.

open as a page

In Java, what is the difference between static initialization and instance initialization, and when does each one run?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Static initializers (static fields and static blocks) run once, when the class is first loaded. Instance initializers (instance fields and instance blocks) run every time you create an object with new, just before the constructor body.

open as a page

What is a 'blank final' field and what must every constructor do for it?

level: middleimportance: must knowfreq 62%
basics
~20 s

A blank final is a final field declared without a value, like private final int id;. Because it has no initializer, every constructor must assign it exactly once before the constructor ends. If any constructor leaves it unset, the code won't compile.

open as a page

Walk through the exact order of operations when you call new on a subclass: default values, super constructor, instance initializers, field initializers, and the constructor body. Why is this order important?

level: middleimportance: must knowfreq 68%
basics
~20 s

On new: fields first get default values (0/null/false), then the parent constructor runs fully, then this class's instance field initializers and instance blocks run top-to-bottom, and finally the constructor body runs. Parent is always fully built before the child.

open as a page

How do field initializers and instance initializer blocks interact when assigning final fields, and in what order do they run relative to the constructor?

level: seniorimportance: should knowfreq 40%
basics
~20 s

Field initializers and instance initializer blocks run in the order they appear in the source, after the superclass constructor and before the rest of the constructor body. A final field can be set in one of these places or in the constructor, but only once total.

open as a page