Java Keywords and Identifiers

Java Keywords and Identifiers, Java Language Fundamentals, Java Programming Syntax, Writing Java Statements and writing Java Code blocks.

Keywords and Identifiers in Java

Introduction:

Java keywords are also known as reserved words. Keywords are particular words that act as a key to a code. These are predefined words by Java so they cannot be used as identifiers.

Identifiers in Java are symbolic names used for identification. They can be a class name, variable name, method name, package name, constant name, and more.

Note: Java keywords or reserved words can not be used as an identifier.

Example:

int myNum = 100;

In the above Java statement, myNum is an identifier (User-defined), it is for identifying the variable.

String country=”India”;

In the above Java statement, country is an identifier (User-defined), it is for identifying the variable.

Java statements or steps or instructions are written using keywords, identifiers, data, and special characters.

Example:

int num =100;

In the above statement,

int is Data Type (Java keyword),

num is Identifier (variable vame),

= is assignment operator (special character) and ; (semicolon) is also a special character.

100 is data or value.

Java Keywords:

Java keywords or reserved words are particular words that act as a key to a code. We have “keywords” in every programming language, Java keywords are small letters, and Java is a case-sensitive language.

Java Primitive Data Types:

Java Primitive Data types are keywords, but Non-primitive data types are not keywords

byte – A data type that can store whole numbers from -128 and 127

short – A data type that can store whole numbers from -32768 to 32767

int – A data type that can store whole numbers from -2147483648 to 2147483647

long – A data type that can store whole numbers from -9223372036854775808 to 9223372036854775808

float – A data type that can store whole numbers from 1.7e−308 to 1.7e+308.

double – A data type that can store whole numbers from 1.7e−308 to 1.7e+308.

char – A data type that is used to store a single character.

boolean – A data type that can only store true and false values.

Java Modifiers:

Java access and non-access modifiers are keywords

public – An access modifier used for classes, attributes, methods, and constructors, making them accessible by any other class.

protected – An access modifier used for attributes, methods, and constructors, making them accessible in the same package and subclasses.

private – An access modifier used for attributes, methods, and constructors, making them only accessible within the declared class.

static – A non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class

final – A non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override)

abstract – A non-access modifier. Used for creating incomplete classes and methods.

etc,

Java Control Flow:

Java control flow (conditional/decision making, looping, and branching) statements are keywords.

if – Makes a conditional statement

else – Used in conditional statements

switch – Selects one of many code blocks to be executed

for – Creates a for loop

do – Used together with while to create a do-while loop.

while – Creates a while loop

break – Breaks out of a loop or a switch block

continue – Continues to the next iteration of a loop

return – Finished the execution of a method, and can be used to return a value from a method.

Others:

class – Defines a class

package – Declares a package

import – Used to import a package, class or interface

new – Creates new objects

try – Creates a try…catch statement

catch – Catches exceptions generated by try statements

etc,

Note: true, false, null are Java reserved words but not keywords, they are literals.

Java Identifiers

In any programming language, identifiers are used for identification purposes. In Java, an identifier can be a class name, method name, variable name, or label. For example :

public class Sample
{
public static void main(String[] args)
{
int num = 1000;
}
}

In the above java code, we have 5 identifiers namely :

Sample: Class name.

main: method name.

String: Predefined class name.

args: Variable name.

num: Variable name.

Rules for defining Java Identifiers

• There are certain rules for defining a valid java identifier. These rules must be followed, otherwise, we get a compile-time error.

• The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_’ (underscore).

• Identifiers should not start with digits([0-9]). For example “7myvaraible” is not a valid Java identifier.

• Java identifiers are case-sensitive.

• There is no limit on the length of the identifier.

• Reserved Words can’t be used as an identifier. For example “int while = 123;” is an invalid statement as while is a reserved word


Java Programming Language Syllabus

Java Step by Step Videos

Follow me on social media: