Language Keywords and Reserved Terms
Java defines specific vocabulary that carries built-in functionalities, prohibiting their use as custom variable, method, or class names. These are classified into active keywords and unused reserved terms.
- Keywords: Reserved words currently utilized by the compiler to define structural and operational semantics. Examples include data type definers (
int,boolean), access modifiers (public,private), flow controllers (if,for,return), and object-oriented constructs (class,extends,implements). Java contains approximately 50 such keywords. - Reserved Words: Terms not implemented in current Java versions but withheld for potential future integration. Notable examples are
gotoandconst. Additionally, literals liketrue,false, andnullcannot be used as identifiers.
Identifier Naming Conventions
Identifiers assign names to variables, methods, classes, and packages. They must adhere to strict syntactic rules:
- Composed exclusively of letters, digits, underscores (
_), or dollar signs ($). - The starting character must be a letter, underscore, or dollar sign; digits are prohibited as the first character.
- Cannot conflict with Java keywords or reserved literals.
- Case-sensitive (
UserIdanduserIDrepresent distinct entities).
Beyond syntax, industry best practices dictate specific casing styles:
- PascalCase (UpperCamelCase): Applied to classes and interfaces. Every word begins with a capital letter, e.g.,
CustomerAccount. - camelCase (lowerCamelCase): Applied to variables and methods. The initial word is lowercase, subsequent words capitalized, e.g.,
calculateInterest. - SCREAMING_SNAKE_CASE: Applied to constants. All uppercase letters separated by underscores, e.g.,
MAX_RETRY_LIMIT.
Variables and Scope
A variable serves as a named memory container holding a specific data type. Declaring a variable requires specifying its type followed by its name, and optionally initializing it with a value.
int currentSpeed = 60;
double accountBalance = 1250.50;
String clientName = "Jane Doe";
boolean isVerified;
isVerified = true;Variable scope determines accessibility boundaries within a program:
- Local variables: Declared within methods or blocks, accessible only inside that specific boundary.
- Instance variables: Declared inside a class but outside methods. Each object instance maintains its own unique copy.
- Class (static) variables: Declared with the
staticmodifier. A single instance is shared across all objects of the class.
Data Type Categories
Java mandates explicit type declaration for variables, dividing types into primitives and references.
Primitive Types
Eight built-in types representing raw values stored directly in memory:
- Integer group:
byte(1 byte),short(2 bytes),int(4 bytes, default numeric type),long(8 bytes, requiresLsuffix). - Floating-point group:
float(4 bytes, requiresFsuffix),double(8 bytes, default decimal type). - Character group:
char(2 bytes, Unicode character). - Boolean group:
boolean(logicaltrueorfalse).
Reference Types
These store memory addresses pointing to actual objects residing in the heap. They encompass classes, interfaces, enums, and arrays. The default value for uninitialized reference variables is null. Unlike primitives, references provide access to object methods and fields.
Type Casting Mechanisms
Converting between data types happens either automatically or manually.
- Widening (Implicit) Casting: Occurs when moving from a smaller capacity type to a larger one (e.g.,
inttodouble). No data truncation occurs, and the compiler handles it automatically.short minorValue = 120; int majorValue = minorValue; // Automatic promotion - Narrowing (Explicit) Casting: Required when moving from a larger capacity type to a smaller one (e.g.,
doubletoint). Potential precision loss requires explicit parenthetical casting.double preciseMetric = 98.76; int roundedMetric = (int) preciseMetric; // Manual truncation, becomes 98
Note that boolean cannot be cast to or from any other primitive type.
Operators Overview
Java utilizes diverse operators to manipulate variables and evaluate expressions:
- Arithmetic:
+(addition),-(subtraction),*(multiplication),/(division),%(modulus),++(increment),--(decrement). - Assignment:
=, and compound forms like+=,-=,*=. - Relational:
==(equality),!=(inequality),>,<,>=,<=. - Logical:
&&(AND),||(OR),!(NOT). - Bitwise:
&,|,^,~,<<,>>,>>>. - Ternary:
condition ? valueIfTrue : valueIfFalse. - Instance check:
instanceofevaluates if an object belongs to a specific class hierarchy.