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 pageshowhide
explore
- Strings38 questions
- String Immutability5 questions
- String Literal Pool and intern()4 questions
- equals() vs == for Strings4 questions
- String Concatenation5 questions
- StringBuilder vs StringBuffer5 questions
- Common String API Methods6 questions
- Text Blocks4 questions
- String Internal Representation5 questions
- Arrays35 questions
- Array Declaration and Creation5 questions
- Array Bounds Checking5 questions
- Multidimensional and Jagged Arrays5 questions
- Array Covariance and ArrayStoreException4 questions
- Arrays Utility Methods6 questions
- System.arraycopy and Array Copying5 questions
- Array vs ArrayList Trade-offs5 questions
- Packages and Imports16 questions
- Package Declaration and Structure5 questions
- Import Declarations6 questions
- Static Imports5 questions
- Static Members10 questions
- Static Variables and Methods5 questions
- Static Initializer Blocks5 questions
- Initialization10 questions
- Initialization Order5 questions
- final Field Initialization5 questions
questions
109 · 5 sectionsWhat do indexOf and lastIndexOf do in Java, and how do their fromIndex variants and return values behave?
basics
~10 sindexOf 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.
How does String.substring() work in Java, including its index arguments and what happens to the original string?
basics
~10 ssubstring(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.
Why do we use StringBuilder or StringBuffer to build strings instead of repeatedly concatenating with the + operator in a loop?
basics
~20 sJava 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.
How does the + operator work for joining strings in Java, and what does it produce?
basics
~10 sThe + 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.
In Java, what is the difference between using == and equals() to compare two String objects?
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().
What happens in Java when you access an array element with an index that is outside the array's valid range?
basics
~10 sJava 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.
Why is a Java array fixed-length once created, and how do you handle a collection that needs to grow?
basics
~20 sAn 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.
What are the ways to declare an array variable in Java, and is there a difference between int[] a and int a[]?
basics
~20 sYou 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.
How do you get the size of an array in Java, and why is it a field rather than a method?
basics
~10 sYou 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.
How do you create (instantiate) an array in Java with the new keyword, and what does the size argument do?
basics
~20 sUse 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.
Why don't you need to import String, Integer, or Object, and what makes the java.lang package special?
basics
~10 sEvery 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.
What is the difference between a single-type import and a wildcard (on-demand) import in Java, and when would you prefer each?
basics
~10 sA 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.
What is a package declaration in Java, and how does it relate to the directory structure of your source files?
basics
~20 sA 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/.
Why can you use String, System, and Integer without importing them, and what is special about java.lang?
basics
~10 sString, 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.
What is the reverse-domain-name convention for Java packages, and why is it used?
basics
~20 sYou 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.
What is a static initializer block in Java, and why would you use one instead of just initializing static fields inline?
basics
~20 sA 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.
What does the `static` keyword mean when applied to a field or method in Java?
basics
~20 sstatic 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.
When a class has multiple static initializer blocks and static field initializers interleaved, in what order do they execute?
basics
~20 sThey 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.
Why can't a static method directly access instance variables or instance methods, and how do you work around it?
basics
~20 sA 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.
How does static initializer execution relate to instance initializers and constructors across a class hierarchy when you create an object?
basics
~20 sStatic 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.
What are the rules for initializing a final field in Java, and what does 'definitely assigned exactly once' mean?
basics
~20 sA 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.
In Java, what is the difference between static initialization and instance initialization, and when does each one run?
basics
~20 sStatic 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.
What is a 'blank final' field and what must every constructor do for it?
basics
~20 sA 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.
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?
basics
~20 sOn 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.
How do field initializers and instance initializer blocks interact when assigning final fields, and in what order do they run relative to the constructor?
basics
~20 sField 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.