Sunday, August 17, 2014

Java Keyword Cheat Sheet - Meaning and Usage

Knowing keywords of a programming language is very important to understand its features, and Java has rich set of keyword, going along with rich set of functionalities. The first keyword, I learn while writing Java program was public, static and void, not surprisingly they are picked from main method. Our instructor explained meaning of each keyword, saying that its, always remember why main is public, static and void main Java. From then, I have learned and used almost every keywords in Java language, except some of the reserved one e.g. goto. Some lesser known and confusing keywords includes strictfp, transient and volatile. You can actually divide all keyword in Java on different categories, depending upon their meaning and context on which they are used. For example, private, protected and public keyword are collectively known as access modifiers and part of Java's wonderful encapsulation and data hiding feature. byte, short, char, int, long, float and double are also keyword but they are collectively known as primitive types in Java and represent numeric values both integral and floating points. synchronized and volatile are used in multi-threading, transient is used in serialization process, while static and final are modifiers, which makes a variable, class or a method special. final, is particularly important because it can seriously limit your ability to extend a class, its used to denote completeness. So if you want to make sure that no one overrides a particular method of a class, you make it final. A final variable is like constants in Java, they can not be reinitialized. They are often initialized at the same time they are created, except blank final variables, which are actually initialized at constructors. There are few more keywords to represent a code unit, known as class and interface. A class can have both state and behavior, where state is provided by member variables, while behavior is givn by methods. Similarly you have abstract as keyword. Here is the list of Java keyword with their meaning and examples, sorted in alphabetical order for quick reference.