Java Notes
Java Notes
Beginner to intermediate study notes for programming practice
These notes are designed for quick revision, beginner-friendly explanation, and practical programming or
database study. Each section includes key ideas and a small checklist so the document can be used for
class preparation, interview revision, or project practice.
How to use this document: read a section, type or write a small example, test yourself with the checklist, and
then revise the topic after one or two days.
Contents
1. 1. What Java Is
2. 2. Basic Program Structure
3. 3. Variables and Data Types
4. 4. Operators and Expressions
5. 5. Selection Statements
6. 6. Loops
7. 7. Methods
8. 8. Classes and Objects
9. 9. Arrays and ArrayList
10. 10. Inheritance and Polymorphism
11. 11. Exceptions and File Basics
12. 12. Revision Checklist and Practice Plan
Page 1
Java Notes
1. What Java Is
Java is a high-level, object-oriented programming language used for desktop applications, Android
development, web back ends, enterprise systems, and academic programming courses. A Java program is
usually written in a .java file, compiled into bytecode, and then executed by the Java Virtual Machine. This
design makes Java portable because the same compiled program can run on different operating systems
when a compatible JVM is available.
A simple Java program normally begins with a class. The main method is the entry point, which means
execution starts there. Java is strict about structure, spelling, capitalization, braces, and semicolons.
Because Java is strongly typed, every variable must have a declared type, such as int, double, boolean,
char, or String.
Important habits in Java include naming classes with capital letters, naming variables and methods with
camelCase, keeping each class focused on one responsibility, and testing each method separately.
Key points
JDK includes compiler and tools
JRE provides runtime support
JVM executes bytecode
Source file extension is .java
Compiled file extension is .class
Study task
Write a small example or explanation for this topic. Then identify one common error related to what java is
and describe how to fix it. This habit strengthens practical understanding and helps in exams, labs, and
interviews.
Page 2
Java Notes
2. Basic Program Structure
A Java class contains fields, constructors, and methods. The most common first program contains public
class Main and public static void main(String[] args). The word public means the class or method can be
accessed from outside. The word static means the method belongs to the class rather than to a specific
object. The word void means the method does not return a value.
Curly braces define blocks. A missing brace is one of the most common beginner errors. Indentation does
not change how Java runs, but it makes the program easier to read. Statements usually end with a
semicolon. A block such as an if statement or loop may contain many statements inside braces.
Java output commonly uses [Link] for a new line and [Link] for output without moving
to the next line. Formatted output can use [Link] when a specific number of decimal places is
required.
Key points
Class names should match file names for public classes
main must be spelled correctly
println prints and moves to next line
printf is useful for decimals
Study task
Write a small example or explanation for this topic. Then identify one common error related to basic program
structure and describe how to fix it. This habit strengthens practical understanding and helps in exams, labs,
and interviews.
Page 3
Java Notes
3. Variables and Data Types
A variable is a named memory location that stores a value. Primitive types include int for whole numbers,
double for decimal numbers, boolean for true or false values, and char for a single character. String is not a
primitive type, but it is used very frequently to store text.
Type choice matters. Use int for counts, ages, indexes, and whole-number totals. Use double for
measurements, money calculations in beginner programs, averages, and division that may produce a
decimal. Use boolean for conditions such as isPassed or hasLicense.
Assignment uses one equals sign. Comparison uses two equals signs. For Strings, use equals instead of ==
when comparing actual text. Constants are often declared with final, for example final double TAX_RATE =
0.08.
Key points
int stores whole numbers
double stores decimal values
boolean stores true or false
String stores text
final creates constants
Study task
Write a small example or explanation for this topic. Then identify one common error related to variables and
data types and describe how to fix it. This habit strengthens practical understanding and helps in exams,
labs, and interviews.
Page 4
Java Notes
4. Operators and Expressions
Java supports arithmetic operators such as +, -, *, /, and %. The percent symbol gives the remainder.
Integer division removes the decimal part, so 5 / 2 gives 2 when both values are integers. To get 2.5, at least
one value must be a double.
Relational operators compare values: <, <=, >, >=, ==, and !=. Logical operators combine conditions: &&
means and, || means or, and ! means not. These are important in conditions and loops.
Operator precedence decides which operation happens first. Parentheses are the safest way to make the
intended order clear. For example, average = (a + b + c) / 3.0 is clearer and avoids integer division
problems.
Key points
% gives remainder
&& requires both conditions true
|| requires at least one condition true
! reverses a condition
Study task
Write a small example or explanation for this topic. Then identify one common error related to operators and
expressions and describe how to fix it. This habit strengthens practical understanding and helps in exams,
labs, and interviews.
Page 5
Java Notes
5. Selection Statements
Selection statements allow a program to make decisions. The if statement runs a block only when a
condition is true. The if-else statement chooses between two blocks. An else-if chain is useful when there
are several possible categories such as grades, age groups, or menu choices.
Conditions must evaluate to boolean. Avoid writing conditions that are too long. If a condition becomes
difficult to understand, store part of it in a boolean variable with a clear name.
A switch statement is useful when one variable is compared with several exact values. Modern Java
supports improved switch forms, but the traditional switch with case and break is still common in courses.
Key points
Use == for primitive comparison
Use .equals for String comparison
else belongs to the nearest unmatched if
switch is good for fixed choices
Study task
Write a small example or explanation for this topic. Then identify one common error related to selection
statements and describe how to fix it. This habit strengthens practical understanding and helps in exams,
labs, and interviews.
Page 6
Java Notes
6. Loops
Loops repeat code. A for loop is usually best when the number of repetitions is known. A while loop is best
when repetition depends on a condition. A do-while loop runs at least once because the condition is checked
after the body.
Every loop should make progress toward stopping. An infinite loop happens when the stopping condition
never becomes false. Common loop tasks include counting, summing, finding a maximum, searching a list,
validating input, and processing file data.
Loop indexes often start at 0 and continue while i < length. This pattern is especially important for arrays and
ArrayLists. Off-by-one errors happen when a loop runs one time too many or one time too few.
Key points
for loop: known count
while loop: condition based
break exits loop
continue skips to next iteration
watch for off-by-one errors
Study task
Write a small example or explanation for this topic. Then identify one common error related to loops and
describe how to fix it. This habit strengthens practical understanding and helps in exams, labs, and
interviews.
Page 7
Java Notes
7. Methods
A method is a reusable block of code that performs a task. Methods help divide a large program into smaller
parts. A method can receive parameters and can return a value. For example, a method named
calculateArea might receive length and width and return the area.
The return type appears before the method name. If a method returns nothing, its return type is void. A
method should usually do one clear job. This makes testing easier and reduces repeated code.
Method overloading means writing methods with the same name but different parameter lists. Java decides
which method to call based on the number and types of arguments.
Key points
Parameters receive input
return sends back a value
void returns no value
Overloading uses same name with different parameters
Study task
Write a small example or explanation for this topic. Then identify one common error related to methods and
describe how to fix it. This habit strengthens practical understanding and helps in exams, labs, and
interviews.
Page 8
Java Notes
8. Classes and Objects
Object-oriented programming is a major part of Java. A class is a blueprint, and an object is an instance
created from that blueprint. For example, a Student class can describe the data and behavior of a student,
while student1 and student2 are objects.
Fields store object data. Constructors initialize new objects. Methods define object behavior. Encapsulation
means keeping fields private and providing controlled access using public methods such as getters and
setters.
The keyword this refers to the current object. It is often used in constructors when parameter names match
field names. For example, [Link] = name assigns the parameter value to the object field.
Key points
Class is blueprint
Object is instance
Constructor initializes object
private fields protect data
this means current object
Study task
Write a small example or explanation for this topic. Then identify one common error related to classes and
objects and describe how to fix it. This habit strengthens practical understanding and helps in exams, labs,
and interviews.
Page 9
Java Notes
9. Arrays and ArrayList
An array stores multiple values of the same type in fixed-size indexed positions. Array indexes start at 0. If
an array has length 5, valid indexes are 0 through 4. Accessing index 5 would cause an error.
ArrayList is a resizable list from [Link]. It can grow and shrink while the program runs. Common methods
include add, get, set, remove, and size. ArrayList stores objects, so primitive values are stored using
wrapper classes such as Integer and Double.
Enhanced for loops are convenient when every item must be visited. Regular indexed loops are better when
the index is needed or when items are being changed by position.
Key points
Array length is fixed
ArrayList size can change
Use [Link]() not [Link]
Indexes begin at 0
Study task
Write a small example or explanation for this topic. Then identify one common error related to arrays and
arraylist and describe how to fix it. This habit strengthens practical understanding and helps in exams, labs,
and interviews.
Page 10
Java Notes
10. Inheritance and Polymorphism
Inheritance allows one class to reuse and extend another class. The existing class is called the superclass,
and the new class is called the subclass. A subclass can add fields and methods or override inherited
methods.
Polymorphism means one reference type can refer to different object types. For example, an Animal
reference can refer to a Dog object or a Cat object if Dog and Cat extend Animal. When an overridden
method is called, Java uses the actual object type at runtime.
Inheritance should represent an is-a relationship. A Car is a Vehicle, but a Car is not an Engine. Overusing
inheritance can make programs confusing, so composition is sometimes a better design.
Key points
extends creates inheritance
override changes inherited behavior
super calls superclass constructor or method
Polymorphism supports flexible code
Study task
Write a small example or explanation for this topic. Then identify one common error related to inheritance
and polymorphism and describe how to fix it. This habit strengthens practical understanding and helps in
exams, labs, and interviews.
Page 11
Java Notes
11. Exceptions and File Basics
An exception is an event that interrupts normal program flow. Common exceptions include
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, and
NumberFormatException. Java uses try-catch blocks to handle exceptions safely.
File input often uses Scanner or newer file utilities. When reading files, always consider what happens if the
file is missing, empty, or contains unexpected data. Good programs handle such cases instead of crashing
without explanation.
Debugging Java requires reading the error message carefully. The line number is a clue, not always the
complete answer. Trace the values of variables, test small pieces, and fix one error at a time.
Key points
try contains risky code
catch handles the error
finally can run cleanup code
Read stack traces from top relevant line
Study task
Write a small example or explanation for this topic. Then identify one common error related to exceptions
and file basics and describe how to fix it. This habit strengthens practical understanding and helps in exams,
labs, and interviews.
Page 12
Java Notes
12. Revision Checklist and Practice Plan
A strong Java revision plan includes daily practice with syntax, weekly practice with object-oriented design,
and regular debugging. Beginners should not only read code; they should type it, run it, break it, and repair
it. This process builds real understanding.
For exams and interviews, practice explaining code in simple language. Be ready to describe variables,
loops, conditions, methods, arrays, classes, inheritance, and exceptions. Also practice tracing code by hand
because many questions test output prediction.
Mini projects are excellent revision: a grade calculator, library system, contact manager, quiz game, bank
account simulation, and simple student records program. These projects combine input, conditions, loops,
arrays, classes, and methods.
Key points
Read one small topic, type the examples yourself, and explain the output in your own words.
Keep a notebook of errors. Write the error message, the cause, and the fix.
Practice with small programs before combining many concepts together.
Use comments to explain why code is written, not to repeat every simple line.
Revise core syntax often: variables, input, conditions, loops, functions, arrays, and files.
Study task
Write a small example or explanation for this topic. Then identify one common error related to revision
checklist and practice plan and describe how to fix it. This habit strengthens practical understanding and
helps in exams, labs, and interviews.
Page 13