Java Flow Control
Object Oriented Programming
Keywords & Identifiers In Java
In this tutorial, you will learn a basic unerstanding of Keywords and their list, Identifiers and rules for naming Identifiers and valid and invalid variables.
Java has a set of reserved words that are known as keywords. These keywords have a specific meaning in the language and cannot be used as identifiers. If we try to use a keyword as an identifier, we will get a compile-time error. Here is a list of some of the keywords in Java:
- abstract: Java abstract keyword is used to declare abstract class, abstract methods. It is basically used to hide implementation details from outsiders and only functionality is provided.
- assert: assert keyword is used for debugging.
- boolean: boolean Keyword is a data type that can only store true and false values(1 or 0).
- break: break Keyword is used to break a loop or a switch block. It breaks the current execution flow of the program at specified conditions.
- byte: byte Keyword is to declare a variable that can hold 8-bit data values. It can store whole numbers from -128 and 127.
- case: case keyword is used with the switch statements to mark blocks of text.
- catch: catch keyword is used to catch the exceptions generated by try statements. This block must be followed by the try block.
- char: char keyword is used to declare a variable that can hold unsigned 16-bit Unicode characters. It is used to store a single character.
- class: class keyword is used to declare a new class.
- continue: continue keyword is used to continue the next iteration of a loop. It is used to skip the current iteration of the loop and resume to the next iteration.
- default: default keyword is used to specify the default block of code in a switch statement.
- do: do keyword is used in the control statement to declare a loop. In do loop, the loop is executed atleast once and then check for the specified condition.
- double: double keyword is used to declare a variable that can hold 64-bit floating-point numbers. It is used to store whole numbers from 1.7e−308 to 1.7e+308
- else: else keyword is used to indicate the alternative branches in an if statement. Used in conditional statements.
- enum: enum keyword is used to define a fixed set of constants.
- extends: extends keyword is used to indicate that a class is derived from another class or interface.
- final: final keyword is used to declare constant variable values.
- finally: finally keyword indicates a block of code in a try-catch structure. Finally block will be executed by default.
- float: float keyword is used to hold a 32-bit floating-point number. Store whole numbers from 3.4e−038 to 3.4e+038
- for: for keyword is used to start a for loop. It is used to execute a set of instructions repeatedly when some conditions become true.
- if: if keyword is used to make a conditional statement. It executes the if block if the condition is true.
- implements: implements keyword is used to implement an interface.
- import: import keyword import packages, classes, and interfaces.
- instanceof: instanceof is used to checks whether an object is an instance of a specific class or an interface
- int: int is a data type that can hold a 32-bit signed integer. Store whole numbers from -2147483648 to 2147483647
- interface: interface keyword is used to declare a special type of class that only contains abstract methods
- long: long keyword is used to declare a variable that can hold a 64-bit integer. Store whole numbers from -9223372036854775808 to 9223372036854775808
- new: new keyword is used to create new objects.
- package: package keyword is used to declare a Java package that includes the classes.
- private: private keyword is an access modifier. It is used to indicate that attributes, methods, and constructors, making them only accessible within the declared class
- protected: protected keyword is an access modifier. It is used to indicate attributes, methods, and constructors, making them accessible in the same package and subclasses
- public: public keyword is an access modifier. It is used to indicate that classes, attributes, methods and constructors, making them accessible by any other class
- return: return keyword is used to return from a method when its execution is complete.
- short: short is a data type that can hold a 16-bit integer. Store whole numbers from -32768 to 32767.
- static: static keyword is used for memory management mainly. single copy to be shared by all instances of the class.
- super: super keyword is a refer immediate parent class variable. this keyword is used to access data members or fields of the parent class. By using this keyword we can call the variable, method, and constructor of the superclass.
- switch: Java switch keyword contains a switch statement that executes code based on test value.
- this: this keyword refers to the current object in a method or constructor.
- throw: throw keyword is mainly used to throw a custom exception or user-defined exceptions.
- try: try keyword is used to start a block of code that will be tested for exceptions. try block contain multiple catch blocks and single finally block.
- void: void keyword is used to declare that a method does not have a return value.
- while: while keyword is used to start a while loop. while loop comes under repetition statements.
Identifiers in Java are symbolic names used for identification, such as class names, method names, variable names, or labels. It's important to follow some rules when defining identifiers to ensure that your code is valid and easy to read.
There are some rules, which we need to follow While defining an identifier.
- An identifier can only have alphabates (a-z, A-Z), numeric characters (0-9) and underscore (_).
- The first letter of an identifier should be either a letter (a-z, A-Z) or underscore (_). So a valid identifier can't start with a numeric character.
- We can't use the Java reserved keywords as an identifier such as int, float, double, char, etc.
- No special characters like semicolon, period, whitespaces, slash, or comma are permitted to be used as a identifier character.
- Java identifiers are case-sensitive.
- As a good practice, we should always provide meaningful and short names to identifiers.
- As per the standard conventions, identifier length should be a minimum of 4 to a maximum of 15 characters.
- Some examples of valid Identifiers in java: HashVariable, Hash12 ,hashvariable, a, i, _hashvariable , Hash_Variable , hash_code_variable .
- Some examples of Invalid Identifiers in java: Hash Variable, 12Hash, hash+variable, hash-variable, Hash_&_Variable , hash'variable .
Examples of Valid and Invalid Identifiers:
Here are some examples of valid identifiers in Java:
And here are some examples of invalid identifiers in Java:
- Hash Variable (contains a whitespace)
- 12Hash (starts with a numeric character)
- hash+variable (contains a special character)
- hash-variable (contains a special character)
- Hash_&_Variable (contains a special character)
- hash'variable (contains a special character)