Java Programming Fundamentals: Comprehensive
Study Note
1. Introduction to Java and its History
Java is uniquely defined as both a high-level, object-oriented programming
language and a robust computing platform. It is considered
a platform because it provides a complete execution environment—the Java
Runtime Environment (JRE)—which sits on top of the underlying operating
system. While the language evolves rapidly (with the curriculum referencing
the JRE 24 environment), the core philosophy remains: everything behaves as
an object.
Timeline and Trivia
• The Inventor: James Gosling and a small team at Sun Microsystems (The Sun
Company).
• The Pivot: Gosling initially chose C++ for his work but "gave up" on it,
finding it incapable of doing what he required. He modified C++ until it
became a new language.
• 1994 & "Oak": Originally named "Oak," the language was rebranded as Java.
• The Green Project: Java’s birthplace. The project’s specific goal was to
enable consumer electronic devices to communicate with one another using "a
special programming."
• Duke the Icon: A "little cartoon character" created by Joe Palrang as a
byproduct of the Green Project. Duke is the official mascot and icon of Java.
2. Comparative Advantages of Java
Java provides four critical advantages over procedural languages like C and
C++, as summarized in the table below:
Advantage Technical Description
Simpler Syntax Java removes complex C++ elements, oMering user-friendly
syntax and high-level constructs like built-in garbage
collection, exception handling, and multithreading.
Strong Type Java enforces strict type checking to catch errors
System at compile time rather than runtime, significantly
increasing program robustness.
Platform Java uses the "compile once, run anywhere" model. Code
Independence is compiled into generic bytecode rather than OS-specific
machine language.
Rich Standard The Java API provides extensive, easy-to-use support for
Library networking, I/O operations, and complex data
manipulation.
3. Java Architecture and Environment
Java’s architecture is a synergy of four intertwined technologies: the Java
Programming Language, the Java Class File, the Java API, and the Java Virtual
Machine (JVM).
Defining the Environment Components
• JDK (Java Development Kit): The full software development environment. It
contains the tools necessary to write and compile Java code, most notably the
compiler (javac).
• JRE (Java Runtime Environment): The software layer that runs other
software. It contains the JVM and the standard class libraries (e.g., Math, util)
required for execution.
• JVM (Java Virtual Machine): The engine that executes bytecode. By running
all code within this virtual machine, Java ensures the program remains
independent of the host platform's hardware and OS.
4. The Java Execution Process
Unlike typical procedural programs (like C) that compile directly into native
machine language (1s and 0s) "specifically tailored to one OS" (Windows,
Mac, or UNIX), Java utilizes a two-stage process.
The Path to Execution
• [COMPILE TIME] Source Code: Written in .java files (e.g., [Link]).
• [COMPILE TIME] Compiler (javac): Translates source code into Bytecode.
If errors exist, compilation fails here.
• [COMPILE TIME] Bytecode: Stored in .class files (e.g., [Link]). This is low-
level, generic code.
• [RUN TIME] JVM Class Loader: Loads the .class files and necessary API
classes.
• [RUN TIME] Bytecode Verifier: Ensures code safety and validity before
execution.
• [RUN TIME] Interpreter / JIT: The JVM interprets the bytecode or uses
the Just-In-Time (JIT) compiler to translate it for the specific OS/Hardware.
5. Development Setup and Basic Syntax
To begin development, you must install the JDK (the source documentation
references JDK 14 for setup, though the platform concepts apply to current
versions like Java 24). Suggested IDEs include NetBeans, Eclipse, and IntelliJ
IDEA.
Core Syntax and Rules
• Comments: Use // (C++ style) for single-line comments; the compiler ignores
everything following them on that line.
• Class Naming: Class names must be Capitalized and
use CamelCase (e.g., HelloWorld).
• Code Blocks: Every segment of Java code must be enclosed within curly
brackets {}.
• Execution Sequence (Terminal):
6. Java Tokens and Identifiers
Java uses identifiers to name variables, methods, and classes.
Rules for Valid Identifiers
Must consist of alphabets, digits, underscores (_), or dollar signs ($).
Mandatory Rule: Must not begin with a digit.
1. No white spaces allowed.
2. Case sensitivity applies (totalMark and totalmark are different).
3. Examples:
o Valid: average, height, pass_mark, totalMark, sum1.
o Invalid: 123 (starts with digit), (area) (contains parentheses).
Variable Scopes
• Instance Variables: Store individual states; values are unique to each object
instance. They are declared without the static keyword.
• Class Variables (Static Fields): Declared with the static modifier. Only one
copy exists, shared across all instances of the class.
7. Operators and Comparison Logic
Java utilizes Relational Operators (<, >, <=, >=, ==, !=) and Logical
Operators (&&, ||, !).
Mandatory Practices for Special Values
Floating-point numbers: Never use == for direct equality with double or Hloat types due
to precision limitations. Instead, test whether they are "close enough" to a target value.
• Strings: Do not use == to compare content. The == operator checks if two
references point to the same memory location, not if the text is the same.
o Use .equals() for content matching.
o Use .equalsIgnoreCase() for case-insensitive matching.
o Use .compareTo() for lexicographical (dictionary) ordering (returns < 0 if
the string comes before the target).
• Null: null represents a reference to nothing. It is distinct from an empty string
("").
o Note: Calling a method on a null reference will cause the program to
throw an exception.
8. Control Flow: Decision Making and Loops
Conditional Statements
• If Statements: Used for conditional execution. While curly brackets {} are
optional for single-statement actions, instructors strongly advise including them
as a matter of habit to prevent logic errors.
• Else-if Ladder: Used for multiple sequential tests. It often concludes with
a Default Statement (the final else) to catch any cases not explicitly handled.
• Ternary Operator (?:): A shorthand for simple if-else assignments (e.g., y = (x >
0) ? 1 : 0).
Switch Statement The switch block evaluates a single variable against
multiple case values.
• Requirement: Always include the break keyword after each case to prevent
"fall-through."
• Default Case: Always include a default label to handle unmatched values.
• Efficiency Tip: Multiple cases can be stacked (e.g., case 1: case 2:) to execute the
same block of code.
9. User Input and Interaction
Use the Scanner class for interactive programs. Standard methods
include nextLine() for Strings and nextInt() for integers.
The "Look Ahead" Error Prevention Solution To prevent crashes caused by
"Input Mismatch" (e.g., a user entering text when an integer is required), use
the hasNextInt() method:
Check validity: Use if ([Link]()) before reading.
1. Handle "Junk": If the check returns false, you must consume the invalid input
using String junk = [Link]();. This clears the scanner's buffer, preventing an infinite
loop or stalled execution.
2. Provide a fallback: Assign a default value or prompt the user again.