Welcome to my blog, hope you enjoy reading

Friday, 11 January 2013

Keywords in java


Keywords are identifiers like publicstatic and class that have a special meaning inside Java source code and outside of comments and Strings. Four keywords are used in Hello World, publicstatic,void and class.

Keywords are reserved for their intended use and cannot be used by the programmer for variable or method names.
There are fifty reserved keywords in Java 1.1, 51 in Java 1.2, 52 in Java 1.4, and 54 in Java 5. The forty-eight that are actually used in are listed below. Don't worry if the purposes of the keywords seem a little opaque at this point. They will all be explained in much greater detail later.

Keywords Used in Java 1.1

booleandeclares a boolean variable or return type
bytedeclares a byte variable or return type
chardeclares a character variable or return type
doubledeclares a double variable or return type
floatdeclares a floating point variable or return type
shortdeclares a short integer variable or return type
voiddeclare that a method does not return a value
intdeclares an integer variable or return type
longdeclares a long integer variable or return type
whilebegins a while loop
forbegins a for loop
dobegins a do while loop
switchtests for the truth of various possible cases
breakprematurely exits a loop
continueprematurely return to the beginning of a loop
caseone case in a switch statement
defaultdefault action for a switch statement
ifexecute statements if the condition is true
elsesignals the code to be executed if an if statement is not true
tryattempt an operation that may throw an exception
catchhandle an exception
finallydeclares a block of code guaranteed to be executed
classsignals the beginning of a class definition
abstractdeclares that a class or method is abstract
extendsspecifies the class which this class is a subclass of
finaldeclares that a class may not be subclassed or that a field or method may not be overridden
implementsdeclares that this class implements the given interface
importpermit access to a class or group of classes in a package
instanceoftests whether an object is an instanceof a class
interfacesignals the beginning of an interface definition
nativedeclares that a method is implemented in native code
newallocates a new object
packagedefines the package in which this source code file belongs
privatedeclares a method or member variable to be private
protecteddeclares a class, method or member variable to be protected
publicdeclares a class, method or member variable to be public
returnreturns a value from a method
staticdeclares that a field or a method belongs to a class rather than an object
supera reference to the parent of the current object
synchronizedIndicates that a section of code is not thread-safe
thisa reference to the current object
throwthrow an exception
throwsdeclares the exceptions thrown by a method
transientThis field should not be serialized
volatileWarns the compiler that a variable changes asynchronously
Two other keywords, const and goto, are reserved by Java but are not actually implemented. This allows compilers to produce better error messages if these common C++ keywords are improperly used in a Java program.
Java 1.2 adds the strictfp keyword to declare that a method or class must be run with exact IEEE 754 semantics.

Java 1.4 adds the assert keyword to specify assertions.

Java 5 adds assert and enum.

true and false appear to be missing from this list. In fact, they are not keywords but rather boolean literals. You still can't use them as a variable name though.