Skip to main content

Java KeyWords


Java Keywords

Some identifiers are reserved in Java which has separate functionality and meaning such type of reserved identifiers are called reserved words.

Java has 54 reserved keywords. We can divide them into the following categories.

  1. Primitive types and void: 9 keywords

    1. boolean: creates a boolean variable. The only possible values are true and false and the default value is false.
    2. byte: creates a byte variable. A byte takes 8-bits and ranges from -128 to 127.
    3. char: used to create a character variable. It takes 2-bytes and it’s unsigned. The value ranges from 0 to 65,536.
    4. short: create a short variable of 2-bytes. The value ranges from -32,768 to 32,767.
    5. int: create an integer variable, takes 4-bytes and the range is from -2,147,483,648 to 2,147,483,647
    6. long: creates a long variable, takes 8-bytes and the range is from -9,223,372,036,854,775,808 to
      9,223,372,036,854,775,807.
    7. float: creates a signed floating point variable using 4-bytes.
    8. double: creates a signed double using 8-bytes.
    9. void: used with methods to specify that it doesn’t return anything.

2.Keywords for flow control: 11

    1. if: used to create if statement.
    2. else: used in conjunction with if to create an if-else statement.
    3. switch: used to create switch-case statements.
    4. case: used in conjunction with switch to create switch-case statements.
    5. default: used with the switch-case statements for the default case. From Java 8 onwards, it can also be used to create default methods in the interfaces. We can also use it to declare default value in an annotation
    6. for: used to create a for loop.
    7. do: used in conjunction with while to create a do-while loop.
    8. while: can be used to create while loop or do-while loop.
    9. break: used in the loops to end the execution of the current loop body.
    10. continue: used in the loops to skip the execution of the current cycle and proceed with the next cycle
    11. return: used to return value from a method.

3.Keywords for Exception Handling: 6

    1. throw: used to throw exceptions.
    2. throws: used with methods to specify the exceptions that the method may throw.
    3. assert: added in Java 1.4 to create assertions.
    4. try: used to create a block of code for exception handling.
    5. catch: used in conjunction with try block to catch the exceptions and process them.
    6. finally: used with try-catch block. The finally block code is always executed.

4.Keywords for modifiers: 11 

    1. strictfp: used to restrict the precision and rounding of floating point calculations to enure portability.
    2. public: used with class, methods, and fields to define their scope. The private identifiers can be accessed from anywhere.
    3. protected: used with inner class, methods, and fields. The protected members are accessible only from within the class, the sub-classes and the other classes in the same package.
    4. private: the private keyword is used with class variables, methods, and inner classes. The private members are accessible only within the class code.
    5. abstract: used to implement abstraction in Java. It’s used with a class declaration to create an abstract class. It can also be used with methods inside an abstract class to declare abstract methods. The abstract methods must be implemented by the subclass. We can’t create an instance of an abstract class.
    6. static: can be used with fields, methods, and inner class. The static members belong to the class and shared by all the instances of the class.
    7. final: used with class, fields, and methods. The final class can’t be extended. The final fields value can’t be changed, once assigned. The final method can’t be overridden in the subclass.
    8. transient: used with class fields to declare that they won’t be part of serialization. When an object is serialized, only non-transient fields are part of the serialization process. When the object is deserialized, the transient fields are assigned with their default values.
    9. volatile: used with class fields to declare that their value might change by other threads. It was intended to use in case of multithreading, but it has several issues and it’s better to stick with synchronization.
    10. synchronized: used with a method or to create a code block. It’s used to create a code block that can be executed by only one thread at a time. It’s very useful in maintaining data consistency in a multithreaded environment.
    11. native: used with java method declaration to specify that the method is not implemented in the same Java class, but rather in another language. For example, System class currentTimeMillis() and arraycopy() are native metho

5.class related key words : 7

     
    1. class: used to create a class.
    2. interface: to create an interface.
    3. package: defines the package for the class, interface, or enum definitions.
    4. extends: used to create a subclass by extending another class.
    5. implements: used to implement an interface.
    6. importused to import a class so that we can use its functions.
    7. enum: added in Java 1.5 to create an enum

6.Object related key words:4

    1. new: used to create an instance by calling the constructor.
    2. instanceOf : An operator to check if an object is instance of a class.
    3. super: used incase of inheritance to access superclass methods, constructors, and variables.
    4. this :used to get access to the current object.

7.Unused keywords: 2

    1. const
    2. goto

8.Miscellaneous Keywords : 4

    1. _ (underscore): added in Java 1.9 for underscore literals.
    2. null: used to define null values of a variable.
    3. true: a boolean literal, returned when a condition is true.
    4. false: a boolean literal, returned when a condition is false.

Comments

Popular posts from this blog

Yahoo! Calendar "Add Event" Seed URL Parameters

I can't seem to find any official documentation on this, so here are my notes. Some information gathered from  http://richmarr.wordpress.com/tag/calendar/ Other information gathered through trial and error, and close examination of the "Add Event" form on Yahoo!'s site. Yahoo! Calendar URL Parameters Parameter Required Example Value Notes v Required 60 Must be  60 . Possibly a version number? TITLE Required Event title Line feeds will appear in the confirmation screen, but will not be saved. May not contain HTML. ST Required 20090514T180000Z Event start time in UTC. Will be converted to the user's time zone. 20090514T180000 Event start time in user's local time 20090514 Event start time for an all day event. DUR value is ignored if this form is used. DUR 0200 Duration of the event. Format is HHMM, zero-padded. MM may range up to 99, and is converted into hours appropriately. HH values over 24 hours appear to be modulated by 24. Durations t...

Java literals:

Java literals:           A constant value which can be assigned to a variable is known as Literal.If we are assigning any outside range value for any data type ,we will get a compile time error saying Possible Loss of Precision found int required byte. For the integral data types (int ,byte,short,long) : we are allowed to specify a literal value in any   one of the following three forms. ---> Decimal literal (normal way) --->Octa literal (prefixed with 0 ) --->Hexa decimal (prefixed with 0x ) int x=10 ------------> Decimal int x=010 ------------>Octa int x=0X10 -------------->Hexa In the Hexa decimal notation for the extra digits we are allowed to specify either small or upper case a,b,c or A,B,C ( this is one of the few places where java is not case sensitive ). Example: class Sample { public static void main(String add[]) { int i = 10; int j=010; int k=0x10; System.out.println( i+”….”+j...