Chapter 415 min read
Java Basic Syntax & Core Concepts
Mastering Java syntax rules, identifiers, modifiers, variables, enums, and keywords.
Released1995
Core MottoStrict Conventions & Object-Oriented Blueprint
CreatorsOracle / Sun Microsystems
1. Core Object-Oriented Terms
A Java program is a collection of objects communicating by invoking each other's methods.
Object
States & Behaviors:An object has states (e.g., color, name, breed) and behaviors (e.g., barking, eating).
Class Instance:An object is a concrete instance created from a class template.
Class
Blueprint Template:A class defines the state attributes and behavior methods supported by objects of its type.
Methods & Instance Variables
Methods:Represent behaviors where logic is executed and data is manipulated.
Instance Variables:Unique sets of variables assigned to each object instance, defining its individual state.
2. Essential Syntax Rules
Following strict naming and structural conventions is required for Java code to compile.
Case Sensitivity & File Naming
Case Sensitivity:Identifiers like Hello and hello are distinct and treated differently.
Program File Name:The file name must match the public class name exactly, appending .java (e.g., MyFirstJavaProgram.java).
Class Names:PascalCase — first letter of every word should be capitalized (e.g., MyFirstJavaClass).
Method Names:camelCase — starts with a lowercase letter, inner words capitalized (e.g., myMethodName()).
Main Method Entry Point
Execution Point:Java processing always starts from public static void main(String args[]), which is mandatory for executable programs.
java
public static void main(String args[]) {
// Execution starts here
}3. Identifiers & Modifiers
Rules for naming components and controlling access levels across Java classes.
Java Identifiers
Naming Rules:Must begin with a letter (A-Z or a-z), currency symbol ($), or underscore (_).
Subsequent Characters:Can contain any combination of letters, numbers, $, or _.
Restrictions:Reserved keywords cannot be used as identifiers.
Legal Examples:age, $salary, _value, __1_value
Illegal Examples:123abc, -salary
Java Modifiers
Access Modifiers:default, public, protected, private
Non-Access Modifiers:final, abstract, strictfp
Classroom Discussion Starter
Why does Java enforce strict case sensitivity and class-to-filename matching?
Takeaway Goal: Matching filenames and public class names allows the compiler and JVM to locate compiled bytecode files efficiently across different operating systems.
4. Variables & Java Enums
Java supports local, instance, and class variables, alongside restricted value lists using Enums.
Types of Variables
Local Variables:Declared inside methods or block scopes.
Instance Variables:Non-static variables bound to specific object instances.
Class Variables:Static variables shared across all instances of a class.
Java Enums
Restricted Values:Introduced in Java 5.0 to restrict variables to predefined constants, reducing bugs.
Declaration:Can be defined independently or nested inside classes.
java
class FreshJuice {
enum FreshJuiceSize { SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]) {
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM;
System.out.println("Size: " + juice.size);
}
}
// Output: Size: MEDIUM5. Reserved Java Keywords
Reserved words cannot be used as identifiers (variables, classes, or method names).
Key Reserved Keywords
Primitive Types:boolean, byte, char, double, float, int, long, short
Control Flow:break, case, continue, default, do, else, for, if, switch, while
Access & Inheritance:class, enum, extends, implements, interface, package, private, protected, public
Exception Handling:catch, finally, throw, throws, try
Modifiers & Scope:abstract, final, native, static, strictfp, synchronized, transient, volatile
Unused Reserved Words:const, goto
6. Comments & Blank Lines
Documentation techniques used to improve code readability while being ignored by the compiler.
Single-line & Multi-line Comments
Single-line Comments:Defined using //
Multi-line Comments:Wrapped between /* and */
Blank Lines:Lines containing only whitespace are completely ignored by the compiler.
java
public class CommentExample {
/* This is a multi-line comment.
* It spans across multiple lines.
*/
public static void main(String []args) {
// This is a single-line comment
System.out.println("Comments Example");
}
}7. Introduction to Inheritance & Interfaces
Core OOP principles for code reusability and establishing structural contracts.
Inheritance
Code Reusability:Derive a subclass from an existing superclass using the extends keyword to reuse fields and methods.
Interfaces
Communication Contract:An interface defines method signatures that implementing subclasses must fulfill using the implements keyword.