0% found this document useful (0 votes)
18 views5 pages

Keywords, Identifiers & Literals in Java

Uploaded by

ssyblus690
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Keywords, Identifiers & Literals in Java

Uploaded by

ssyblus690
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Keywords, Identifiers & Literals in Java

1. Keywords

• Definition: Reserved words in Java that have a special meaning for the compiler.

• Rules: Cannot be used as names for variables, classes, or methods.

• Examples:
class, public, static, if, else, for, int, new, return, etc.

• Total: 67 (including reserved words like goto and const).

Example:

public class Hello { // "public" and "class" are keywords


public static void main(String[] args) {
int num = 5; // "int" is a keyword
}
}

Java Keywords with Meanings

Keyword Meaning (Short & Simple)

abstract Defines a class/method that cannot be directly instantiated or


must be implemented by a subclass.

assert Used for debugging; tests assumptions in code.

boolean Data type for true or false.

break Exits a loop or switch early.

byte 8-bit integer data type.

case Defines a branch in a switch statement.

catch Handles exceptions thrown in a try block.

char Single 16-bit Unicode character.

class Defines a class.

const* Reserved word, not used.

continue Skips the rest of loop iteration and jumps to next.

default Defines the default block in switch.


do Starts a do-while loop.

double 64-bit floating-point number.

else Defines alternate branch in if.

enum Defines a set of named constants.

extends Shows inheritance (class extends another class).

final Declares constants, prevents inheritance/overriding.

finally Block that always executes after try-catch.

float 32-bit floating-point number.

for Defines a for loop.

goto* Reserved word, not used.

if Executes a block conditionally.

implements A class uses an interface.

import Imports other classes/packages.

instanceof Tests if an object is of a specific type.

int 32-bit integer data type.

interface Defines a contract (like a blueprint) for classes.

long 64-bit integer data type.

native Used for methods implemented in another language (like C).

new Creates new objects.

package Defines a group of classes/interfaces.

private Access modifier: accessible only in same class.

protected Access modifier: accessible in package & subclasses.

public Access modifier: accessible everywhere.

return Exits a method and optionally returns a value.

short 16-bit integer data type.

static Defines class-level members (no object needed).


strictfp Enforces strict floating-point rules.

super Refers to parent class.

switch Executes one block out of many options.

synchronized Ensures thread safety by locking a block/method.

this Refers to current object.

throw Used to throw an exception.

throws Declares exceptions a method may throw.

transient Prevents a field from being serialized.

try Starts a block to handle exceptions.

void Declares a method with no return value.

volatile Ensures variable updates are visible across threads.

while Defines a loop that runs while condition is true.

Newer Java Keywords (Modules & Modern Features)

Keyword Meaning

exports Exports a package in a module.

module Defines a module.

open Opens a module for deep reflection.

opens Opens a package for reflection.

provides Defines a service a module provides.

requires Declares dependencies of a module.

to Specifies target module in exports or opens.

transitive Makes a dependency available to other modules.

uses Declares a service used by a module.

var Local variable type inference (Java 10).

yield Returns a value from a switch expression.


record Defines an immutable data class (Java 14).

sealed Restricts which classes can extend a class (Java 15).

non-sealed Opposite of sealed, allows open extension.

permits Declares which classes can extend a sealed class.

2. Identifiers

• Definition: Names given to variables, methods, classes, packages, etc.

• Rules for identifiers:

1. Can contain letters, digits, underscore _, and dollar sign $.

2. Cannot start with a digit.

3. Cannot be a keyword.

4. Case-sensitive (Age and age are different).

• Examples: myVariable, Student, _total, $price.

Example:

int age = 20; // "age" is an identifier

3. Literals

• Definition: Fixed constant values directly written in the program.

• Types of literals:

o Integer literals: 10, -25, 0

o Floating-point literals: 3.14, -0.5, 2.0

o Character literals: 'A', '9', '#'

o String literals: "Hello", "Java"

o Boolean literals: true, false

o Null literal: null

Example:

int x = 100; // 100 is an integer literal


double pi = 3.14; // 3.14 is a floating-point literal
char grade = 'A'; // 'A' is a character literal
String name = "Raj"; // "Raj" is a string literal
boolean flag = true; // true is a boolean literal

You might also like