Learn different keywords in Java and its implementaions

1
1535
  • In this tutorial we will learn different keywords in java and its implementations where we will have brief description of each and every keyword used in java language explained with examples.
    • To learn different keywords in java and its implementations follow the steps given below :

    1. Java has keywords which are enum, abstract, boolean, char, int, float, byte, case, break, finally, default, do, if, else, for, continue, while, switch, short, long, return, double, private, public, protected, package, try, catch, throw, throws, void, const, this, super, static, synchronized, new, implement, instanceof, extend, class, volatile, transient, strictfp, assert, native, interface, import.
    2. Reserved Keywords : true, false, null, goto
    3. Here is the explanation of the keywords :
      1. abstract: – An abstract class & method is declared with keyword abstract. In this scenario abstract class can be extended but cannot be instantiated. In this case if a subclass of an abstract class does not implement the abstract methods of its superclass, subclass is also abstract.
      2. Syntax :

        Example : This example shows the use of abstract keyword

        Output :

      3. boolean: – A boolean variable indicate values only true or false. A boolean may not convert into numeric values. This is primitive data type. Default value is false.
      4. Syntax :

        Example : This example shows the use of boolean keyword

        Output :

      5. char: – A char variable used for storing a single Unicode character. This char keyword is used to declare method return value of type character. It has 16 bit size.
        \ ” − double quote
        \ – backslash.
        \f − form feed
        \’ − single quote
        \n − newline
        \b – backspace

      6. Example : This Java Example shows how to declare and use Java primitive char variable inside a java class.

        Output :

      7. int : – It defines constants MIN_VALUE and MAX_VALUE which represents the range of values for int type.
        int values are negative, positive or may be zero.
      8. Example : This Java Example shows how to declare and use Java primitive
        int variable inside a java class.

        Output :

      9. float : – This is java primitive type, Size is 32 bits. Its default value is 0.0
      10. Example : This Java Example shows how to declare and use Java primitive float variable inside a java class.

        Output :

      11. byte : – byte size is 8 bit integer value. It can store integer value in the range -128 to 127.Default value is 0.
      12. Example : This Java Example shows how to declare and use Java primitive byte variable inside a java class.

        Output :

      13. short : – Size of the short data type is 16 bit. Its min value is -32,768 and max value is 32,767. It is a java primitive data type. In this case default value is 0. A short is two times as small as int.
      14. Example : This example shows how short is used in a program.

        Output :

      15. long : – This is a java primitive type. Size of long data type is 64 bit. Minimum value is -2^63 and maximum value is 2^63-1. In this case default value is 0L. This type is used when large range than int is needed.
      16. Example : This example shows how an object of Long can be declared and used.

        Output :

      17. double : – This is a java primitive type. It may store floating point value. The size is 64 bit float primitive type.
      18. Example : This example shows how to use double in a class.

        Output :

      19. break : – The break keyword is used to exit a for loop, while loop, do loop or to the end of a case block in a switch statement.
      20. Example : This Java Example shows how to break the loop or condition in a class.

        Output :

      21. continue : – Continue is helpful for skipping the next iteration of a do, while or for loop statement.
      22. Example : This example shows how to use java continue statement to skip the iteration of the loop.

        Output :

      23. try, catch, finally : –
        try block is used for throwing the exception. Every try block must have one or more catch blocks or finally block.

        catch keyword is used for catching the exception which is thrown from the try block. Each catch block handles the various types of exceptions.

        finally keyword is used to define a block that is always executed in a series try, catch, finally statement. Every try block must have at least one catch or finally block. When try block is executed after that the code of finally block must be executed with or without exception, if any problem occur during execution in the catch block then also finally block must be executed at least once.

      24. Example : This Java Example shows how to use try catch and finally in a class.

        Output :

      25. do : – do loop body is always executed at least once. Semicolon is always required after the conditional expression.
      26. do keyword
        Example : This Java Example shows how to use do while loop to iterate in Java program.

        Output :

      27. while : – This keyword is indicates that the loop is executed continuously till condition is true.
      28. While Keyword

        Example : This example shows how while keyword is used in a class.

        Output :

      29. if – else : –
        if “is used for executing logical statements or conditional statements. An if statement has else block which is optional containing the code that is executed if the condition is false.

        else keyword is always used with the “if” keyword. The else block is optional and this block is executed when if condition is false.
      30. If statement
        Example : This example shows how if-else statement used in a class.

        Output :

      31. switch, case, default : –
        The switch keyword is used to select execution of one of various code blocks based on expression or condition. To exit the switch statement, break statement must be included at the end of the block.

        case does not have an implicit ending point. A break keyword must typically used at the end of each case block to exit the switch statement. Without a break statement, the flow of execution will flow into all defined cases and last follow default block.

        default is access modifier, when we don’t use any access modifier then by default it is treated as default access modifier. And it can access only within package this is used in the switch cases. It will execute if none of the defined cases are matched to the condition.
      32. Example : This example shows how to use switch-case and default in a class.

        Output :

      33. void, return : –

        When method does not return any type of value that time void keyword is used. This void keyword in the programming language returns the null value.

        The return keyword returns a value. The parentheses are optional surrounding the return value.
      34. Example : This example shows how to write void and return in a class.

        Output :

      35. private : – This keyword is access control modifier. This is applied to a method or a field. It can also apply to a constructor. This is a most restrictive level than access modifier. When we declare a method or variable and a constructor as a private then that a method or a variable and a constructor cannot access outside the class.
      36. Example :

      37. public : –This public keyword is used to declare a class, a method or a field public, that means this methods or a field can access anywhere like within same class, within same package, outside package in subclass and outside package. This is the lowest restrictive level.
      38. Example : This example shows how to use public a class.

        Output :

      39. protected : –This access modifier is accessible in within class, within package and within outside package by subclass only. Not accessible in outside package.
      40. Example : This example shows how to use protected in a class.

        Output :

        protected keyword

      41. package : – package keyword is used for declaring a java package. When source file does not containing a package statement that time classes defined in the file are in the default package.
      42. Example :

      43. throw : – This keyword is used for explicitly throwing exception. throw keyword is used within the method. We cannot throw multiple exceptions at a time. throw keyword is used with checked as well as unchecked exceptions.
      44. throws : – This throws keyword is used with only checked exception. throws is declare with method definition.
      45. const : – This keyword const is used in java for defining a constant. If you declaring any variable const then after you cannot alter that variable.
      46. this : – this is used to refer the current object. When ambiguity occur between parameter and instance variable then this keyword resolve this type of problem. This is a special keyword in java.
      47. super : – super is special keyword in java. Super keyword is used inside the sub-class. Through super keyword only public and protected methods are called.
      48. new : – new keyword is used to create array object or instance of a class. This new keyword is used for allocates memory dynamically. You can also used new keyword for creating simple java object.
      49. static : – Static variables are known as global variables or class variables. When you define variable as a static then there will be only one copy of that static variables created into JVM memory. This static variable shares only one copy throughout the class of instances. In java, static keyword is used for memory management. static variables are helpful to save memory.
      50. Example : This example shows how to use static and new in a class.

        Output:

      51. extend : – This extend keyword is used in a class declaration to define the superclass. Also used in an interface declaration to define one or more super interfaces. At a time a class may only extend one other class. In inheritance process of java extend keyword is used. It means it adds the method and variables of that extended class.
      52. Example : This example shows how to use extend in a class.

        Output :

      53. class : – class keyword is used to define a class. Class completed with curly braces and within that curly braces variables, methods are declared.
      54. Example :

        Output :

      55. import : – Import keyword is used in java to import user defined and built-in packages into java source file. So it will help you as your class can refer to a class that is into another package, by using its name.
      56. implement : – This implement keyword is used to declare the interfaces that are to be implemented by the class. This is used in class definition. One class may implement multiple interfaces. implement keyword is used for inheritance. implement keyword support to the multiple inheritances.
      57. interface : – When you declare a special type of class that class body contains only constant fields, static interfaces and abstract methods. Using interface keyword interfaces are declared. It cannot be instantiated, but implemented.
      58. synchronized : – This keyword is used for a method can accessed by only one thread at a time. It can be applied only to a method, not on a variables or classes.
      59. volatile : – In this case you can specify member variable that variable may be modified asynchronously by more than one thread. This keyword may not be implemented in many JVM.
      60. instanceof : – This keyword is used to determine the class of an object. It produces a boolean result.
      61. Example :

        Output :

      62. transient : – This can only be applied to fields or member variable. Transient variable is not saved when an object gets serialized. This is used to provide you some control over serialization process and gives you flexibility to exclude some of object properties from serialization process.
      63. strictfp : – For floating-point calculation use this strictfp keyword which ensures portability i.e. platform independent. This is used with class and variables only. When you want the answer from your code, then you need to declare the strictfp modifier.
      64. assert : – You can define assert statement with the help of assert keyword. It is used to test your assumptions about the program. While executing assertion, it is consider being true. If it fails, JVM will throw an error named AssertionError. It is mainly used for testing purpose.
      65. native : – This keyword is only applied to methods, not variables or classes.
      66. enum : – This is a type whose field consists of a fixed value of constant. It extends parent class called as Enum. It can implement interfaces. Using new operator we cannot create instance of enum. In java constructor of enum is always private. Constants of enum are implicitly final and static.
      67. for : – It is a pretest loop statement. In this case initialization and increment is an expression. A boolean expression also is an expression that can be true or false. In this case when loop is executed then first initialization expression is evaluated. This expression assigns the initial value to loop control variable (eg. j=1). The boolean expression is tested start of the each iteration of the loop. The loop terminates when it is false and expression is frequently a comparison (j<=10). After that increment expression is evaluated, that can increment the control variable (eg. j++).
      68. Example : This example shows how to use for loop in a class.

        Output :

    4. Explanation of Reserved Keywords :
      1. true: – This returns a boolean result which is true only.
      2. false : – This returns a boolean result which is false only.
      3. null : – This reserved keyword represents no value. Primitive type of variables null cannot be assigned like (boolean, byte, char, double, float, int, long, short).
      4. goto : – This goto keyword is used to jump to the respective line of code or execute a particular segment of code.
    5. Hence, we have successfully learnt different keywords in java and its implementations .

    1 COMMENT

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here