Java Syntax, Variables, Identifiers, and Reference Types
Java Program Structure and Output Operations
File Naming Conventions and Class Definition:
The name of a Java source file must exactly match the name of the
public classdefined inside it.Java is strictly case-sensitive, requiring identical capitalization between the file name and the class name.
Statement Termination:
Every statement or instruction in Java must end with a semicolon
;.Omitting a semicolon results in a syntax error during compilation.
Output Commands (
printvs.println):System.out.print(): Outputs the specified value to the console without advancing to a new line. Subsequent prints will continue on the same line.System.out.println(): Outputs the specified value to the console and automatically appends a newline, moving the output cursor to the line below for the next output operation.Example Syntax:
System.out.println("hello world");System.out: The standard output stream object.printlnorprint: The specific method name invoked to perform output.Argument: The literal string placed within quotes inside parentheses, such as
"hello world".
Escape Sequences:
(Newline Character): An escape sequence that creates a new line in output, causing execution/display to jump directly to the line below.
Execution Error Classification:
Compilation Errors: Errors detected by the compiler before the program runs (e.g., syntax mistakes, missing semicolons, or mismatched file/class names).
Runtime Errors: Errors that occur during the actual execution of the program.
Variables, Data Types, and Memory Model
Variable Creation and Mutability:
Defining Data Types: Creating a new variable requires explicitly declaring its data type.
Variable Modification: If a data type is omitted when assigning a value, Java interprets the command as modifying an existing variable rather than instantiating a new variable (similar to replacing an item inside an existing bag).
Key Vocabulary:
Declaration: Specifying the variable name (identifier) and its data type.
Initialization: Assigning an initial value to a variable.
Primitive Data Types (
char):The
chardata type stores a single individual character variable.Primitive data types store their actual value directly in the memory allocated to the variable.
Reference Data Types and Strings:
The
StringClass: Used to store text sequences containing more than character (e.g.,"I am a String").Composition: A
Stringis an object composed of underlying primitivecharelements that together form the full character sequence.Reference Memory Storage Architecture:
Instead of storing the object's full data directly inside the variable, Java stores the object in heap memory and stores a reference (memory address) to that object inside the variable.
House and Address Analogy: The created object is like a physical house located in memory, while the reference variable holds the street address of that house.
Naming Convention for Reference Types: The first letter of a reference data type is always capitalized (e.g.,
String), whereas primitive data types begin with a lowercase letter.
Java Identifiers, Naming Rules, and Style
Legal Identifiers:
An identifier is a user-defined name for variables, classes, or methods.
Allowed Characters:
Any number of upper or lower-case letters (
a-z,A-Z).Digits from to .
Dollar sign (
$).Underscore (
_).Illegal Identifier Characteristics:
Identifiers cannot start with a numeric digit ( to ).
Spaces are strictly forbidden within variable names.
Reserved Keywords: Cannot use Java reserved keywords as identifiers (e.g.,
public,int,class,void).
Identifiers with Style and Conventions:
Camel Case Naming (
lowerCamelCase):Variables begin with a lowercase letter, and every subsequent concatenated word is capitalized (e.g.,
sampleCamelCase).Descriptive Naming: Variable names must be descriptive to reflect the data they hold and improve code readability.
Documentation and Code Comments
Single-Line Comments:
Denoted by
//.Comments out all content from the
//sequence to the end of that specific line.
Multi-Line (Block) Comments:
Denoted by
/*and*/.Comments out everything contained between the opening
/*marker and the closing*/marker across multiple lines.