Introduction:-
Java has a large list of keywords. They are extensively used in writing the source code. Java keywords are basic things that a programmer should know in order to code. Understanding Java keywords is difficult for some programmers. Today I will take you through a guide which will make you familiar with the basics of Java keywords. I will provide you with the definition of most frequent Java keywords used. I will try my best to make you understand the fundamentals behind their existence in Java programming language. If you are beginner to Java, it will help you in many possible ways. If you are going for SCJP (Sun Certified Java Programmer) exam than this hub is a must read for you. Let us first look at list of keywords in Java in ascending order:
1. abstract: - Java provides abstract keyword to signify something empty. By empty we mean that it has no definition or implementation. Keyword abstract is used before the classes and methods. At the time of declaring a class we mark it as abstract. An abstract class cannot be instantiated. It means an abstract class cannot have Objects. Generally when we place keyword abstract before a class, we make sure that this class is basically an abstract implementation or generic class and the functional characteristics can only be added by extending the class. We cannot create objects of abstract class but we can make their sub-class and create objects of that. By abstract method, we mean that method doesn't have any body. An abstract method doesn't have any statements to execute. Classes that contain abstract method must also be assigned as abstract class and implementation to their abstract method must be provided by extending those classes. If subclasses fail to provide implementation to super-class abstract methods, than they are also marked as abstract.
2. assert: - Java provides us with a debugging keyword called as assert. In Java, assert is a normal Java statement which a programmer puts in the code, that he/she thinks will always be true. Generally, assert statements are being used by the programmer for the debugging purpose. Assertion statements can be turned on or off. While running a program if we enable assertion than wherever our assert statement gets failed Assertion Error is thrown by the program and whole program gets terminated. Generally, assertions are placed by programmer in the code where he believes that statement will be true. After enabling assertion if there is a failure, programmer gets an idea that in code there is some bug. The code fails with an Assertion Error.
3. boolean: - A boolean keyword in Java is a data type for a variable which can store two values, true or false. Generally, boolean keyword is a data type which has size of 1 bit. In Java I am not sure about the size that whether it store 1 bit or not.
4. break: - In Java, 'break' keyword is of quite high importance. As the name suggest, break statement breaks code from one point and transfer flow to other part of program to execute. Generally let say in if block you have placed a break statement. If your code enters if statement an encounters break statement it will stop executing further if block and come out of if block and continue to execute. That is when you encounter break statement execution breaks that block of code and control gets transfer to next statement just after the block. Generally break statement are mostly used with switch statement. If break is used within a labeled block, than as soon as break gets encountered, the program starts executing just after labeled block.
5. byte :- In Java, byte keyword is used as a data type for storing 8 bits of information for an integer. If it’s used before a method definition than that method, when called will always return a byte value.
6. case :- In Java, case keyword is used within switch statements. A condition in switch statement is compared with the cases in switch statement. Whatever case matches with the switch expression that case is executed.
7. catch :- In Java, catch keyword has a group of statements that catches exception, if there is any exception in try block proceeding to it. The statements have a way to deal with the exception or have a way to let programmer know that something is needed to be correct.
8. char :- In Java, char keyword is used as a data type which can store 16 bit Unicode character. If the keyword is placed before method declaration than the method upon execution returns a value which is a char.
9. class :- In Java, class keyword is used to define a class. Generally a class acts as a blue print for an object. It defines implementation for the object. It has statements defining variables, methods, inner classes etc. An Object when instantiated for a particular class has all the physical implementation of what is defined in the class.
10. const :- It is a reserved keyword in Java but it has no use in Java and Java has provided it no function to perform.
11. continue :- In Java, the continue keyword is used when we want rest of the statement after continue keyword to get skipped and want program to continue with the next iteration.
12. default :- In Java, default keyword is used within switch statement. It is used their optionally, if no Java case matches with the expression than the default case is executed by the program.
13. do :- In Java, do keyword is used to implement do-while loop. Generally, do keyword is used when we want to loop a block of statement once. After that the boolean condition gets evaluated, if condition is yes the loop execute again, but if condition comes out to be false the loop exits.
14. double :- In Java, double keyword is used as a data type for storing 64 bits of information for a float type. If it’s used before a method definition than that method when called will always return a double value.
15. else :- In Java, else keyword is used along with if keyword to create an if-else statement. Generally, a condition is evaluated in if block brackets. If the condition evaluates to true if block body gets executed. If the condition is evaluated to false else block body gets executed.
16. enum :- It is used in Java language to declare a data type consisting of a set of named values. They are constants.
17. extends :- In Java, extends keyword is used specify the super class of a subclass, also in interface declaration to specify one or more super interfaces. If we take an example say let say class A extends class B, here class A adds functionality to class B by adding fields or methods. It goes same with interfaces.
18. final :- Generally, final keyword is used to make elements constant. That is once assigned cannot be changed. A final class cannot be sub-classed. A final variable cannot be assigned a new value after it has been assigned to a value. A final method cannot be overridden.
19. finally :- In Java, finally keyword is used to define a block of statements after try to catch blocks. This block executes after try block, or after catch block, or before any return statement.
20. float :- In Java, float keyword is used as a data type for storing 32 bits of information for a float type. If it’s used before a method definition than that method when called will always return a float value.
21. for :- In Java, for keyword is used to create looping statement that is for loop. In this for loop a counter variable is initialized, than a boolean condition is evaluated and compared with counter variable or any expression which turns out to either true or false. If the condition comes out to be true a block of statements following for keyword executes, if condition comes out to be false for loop terminates.
22. goto :- In Java, a goto keyword is a reserved keyword which is not used in the Java language and has no function provided to it.
23. if :- In Java, if keyword is used (optionally) along with else keyword to create an if-else statement. Generally, a condition is evaluated in if block brackets. If the condition evaluates to true if block body gets executed. If the condition is evaluated to false else block body gets executed.
24. implements :- In Java, implements keywords is used in class declaration. Generally, implements keyword implements functionality of interfaces. Interfaces are abstract in nature; their functionality is implemented in the class which implements it.
25. import :- In Java, import statement is used at the start of a Java file, just after package declaration. Generally, import statement gets those classes in a program whose functionality we want to use in the program. It is also used in importing static members.
26. instanceof :- In Java, instanceof operator is a binary operator which is used to test an IS-A relationship between a class and object. Object is first operand and Class or Interface is a second operand.
27. int :- In Java, int keyword is used as a data type for storing 32 bits of information for an integer type. If it’s used before a method definition than that method when called will always return an int value.
28. interface :- In Java, interface is a special type of structure which contains abstract methods, constant fields which are generally static or final.
29. long :- In Java, long keyword is used as a data type for storing 64 bits of information for an integer type. If it’s used before a method definition than that method when called will always return a long value.
30. native :- This keyword is generally used in declaring method signifying that method's implementation is not in the same source file but in different source file and in different language too.
31. new :- Java has a keyword called as new, which has been used to create a new instance of a class on heap. It is used is object creation and providing memory to it.
32. package :- Packages have been created by the package keyword. In the package classes are kept which constitute a relation between each other.
33. private :- In Java, private keyword can be used in declaration of methods, fields, inner class. This makes the methods, fields and inner class access to own class members.
34. protected :- In Java, protected keyword can be used in declaration of methods, fields, inner class. This makes the methods, fields and inner class access to own class members, to sub class and classes of same package.
35. public :- In Java, public keyword can be used before class, methods, fields. This makes the class , methods and fields accessible from any other class.
36. return :- In Java, return keyword is used to pass a value from a method to caller method. It also signifies end of execution of a method.
37. short :- In Java, short keyword is mainly used when we want to declare a field that can hold 16 bit of integer data. It is also placed before method declaration which signifies that method will return only short integer.
38. static :- In Java, static keyword is used when you want to declare method, fields and inner class as class members rather object members, generally static members belong to a class rather than instance of that class. Therefore only one copy is made for them and used by the class as its own implementation.
39. strictfp :- To ensure portability of floating point we ensure that they are platform independent.
40. super :- In Java, super keywords is used to access overridden methods and hidden members of super class by its subclass. It is also used in constructor of a subclass to pass control over super class constructor.
41. switch :- In Java switch keyword is used to have multi-decision making statements. Generally switch is mostly used with keywords case, break and default. This decision making block evaluates an expression first. After the expression is evaluated the value is compared with various case statements. Any value that matches with the expression, case associated with it gets executed. If no expression matches the default case gets executed.
42. synchronized :- In java, synchronized keyword is used before the method declaration and before block of code to get mutual lock for an object. It means that the method or block of code gets locked by the object till it gets fully unlocked or gets fully executed by that object. Generally classes, fields and interfaces cannot be declared as synchronized. The static method gets code synchronization by class itself.
43. this :- In Java, this keyword is used as a reference to currently executable object. Generally if we want to access class members we can use this keyword to access them. If we want to refer to a current object we use this keyword. We also use this keyword to transfer control from one constructor to another in the same class.
44. throw :- In Java, when we want to throw a declared exception, the execution points to the catch block which has provided statements to overcome the exception. If no catch block is declared in the current method than the exception is passed to the calling method. It continues till it found exception handler. If no exception handler is found over the stack that exception is then transferred to the Uncaught Exception handler.
45. throws :- In Java, we use throws keyword in the method declarations. If we get an exception in a method and method has no implementation for that exception, than method throws an exception. The exception thrown has been handled by the method who has called to current method in execution.
46. transient :- Whenever we declare a variable as transient, that variable is not the part of Object serialization. When an object gets saved through default serialization all the non-transient variable retain their value after deserialization. If you want the variable should have default value after deserialization than make it transient.
47. try :- In Java, whenever we want to do exception handling we use try keyword. Generally try block is used where we are sure that exception can occur. In try block statements are executed normally and as soon as exception occur it is handled my catch keyword following try block. It must have at least one catch or finally block.
48. void :- In Java, when we use void before any method declaration, we make sure that method doesn't return any value.
49. volatile :- In Java, volatile keyword provides a lock free mechanism. If a variable is assigned as volatile than all the threads accessing it can modify its current value without keeping a separate copy. A volatile keyword is accessed by all threads simultaneously. They operate on current value of variable rather than cached value.
50. while :- In Java, while keyword is used to create looping statement that is while loop. In this while loop a boolean condition is evaluated and compared with counter variable or any expression which turns out to either true or false. If the condition comes out to be true a block of statements following while keyword executes, if condition comes out to be false the while loop terminates.
51. false :- In Java, false keyword is the literal for the data type Boolean. Expressions are compared by this literal value.
52. null :- In Java, null keyword is the literal for the reference variable. Expressions are compared by this literal value. It is a reserved keyword.
53. true :- In Java, true keyword is the literal for the data type Boolean. Expressions are compared by this literal value.