Important Java Keywords – That You Must Know (Part1)


1. Objective

In this Java tutorial, we are going to study the important Java Keywords. We will look at Java keyword list. Also, we are discussing 2 of them in detail and that is enum and strictfp. Moreover, we will study declaration and imperative purposes of enum, enum values and enum valuesOf technique. In addition, we will discuss strictfp Java keyword and its important focuses.

So, let us start Important Java Keywords.

Important Java Keywords

Important Java Keywords

2. What is Keyword in Java?

Java keyword is a reserved word, that has a special meaning associated with it. For easy identification, they are usually highlighted in Java. Out of 50 keywords, 48 are in use while 2 are not.

Let’s discuss Java Syntax to Learn Java Programming

Let us study some of the important Java Keywords in detail.

3. Important Java Keyword List

  • abstract: It utilizes to accomplish abstraction. It is a non-access to modifier relevant for classes and methods.
  • enum: It utilizes to characterize enum in Java.
  • instanceof: It is utilized to know whether the object is a case of the predefined write (class or subclass or interface).
  • private: It is an access modifier. Anything proclaimed private can’t be seen outside of its class.
  • protected: If you need to enable a component to be seen outside your present class, however just to classes that subclass your class specifically, at that point announce that component ensured.
  • public: Anything proclaimed openly can be gotten to from anyplace.

Access Modifiers –

  • static: It is utilized to make a member (block, method, variable, nested classes) that can be utilized independent from anyone else, without reference to a particular case.
  • strictfp: It is utilized for confining, floating, point figuring and calculations and guaranteeing the same outcome on each stage while performing tasks in the floating point variable.
  • synchronized: It is utilized to get synchronization in java, applicable for blocks, methods.

Learn how to Read Java Console Input | 3 Ways To Read Java Input.

  • transient: Factors modifier utilized as a part of serialization. At the season of serialization, in the event that we would prefer not to spare estimation of a specific variable in a record, at that point we utilize transient keyword.
  • volatile: If modifier tells the compiler that the variable adjusted by unstable can be changed startlingly by different parts of your program.

Abstract Class in Java with Example Program

4. Types of Java Keywords

a. enum

Enumerations effectively represent a gathering of named constants in a programming language. For instance, the 4 suits in a deck of playing cards might have 4 enumerators named Club, Diamond, Heart, and Spade, having a place with an identified sort named Suit. Different illustrations incorporate normal identified composes (like the planets, days of the week, hues, bearings, and so forth.).

Java Enums utilize when we know every single conceivable value at compile time. For example, decisions on a menu, adjusting modes, summon line banners, and so on. It isn’t vital that the arrangement of constants in an enum write remain settled forever.

i. Declaration of enum in Java

  • enums cannot be declared inside a method but can be declared inside or outside a class.

Have a look at Command Line Arguments In Java | Clone() Method In Java

enum Color

{

   RED, GREEN, BLUE;

}

public class Test

{

   public static void main(String[] args)
   {
       Color c1 = Color.RED;
       System.out.println(c1);
   }
}
// inside a class.
public class Test
{
   enum Color
   {
       RED, GREEN, BLUE;
   }
    public static void main(String[] args)
   {
       Color c1 = Color.RED;
       System.out.println(c1);
   }
}

In the first place line inside enum ought to be rundown of constants and afterwards different things like methods, variables and constructor. As indicated by Java naming traditions, it is suggested that we name consistent with every single capital letter.

ii. Imperative purposes of enum

  • Each enum inside executed by utilizing Class.
  • Each enum steady speaks to a question of sort enum.
  • enum sort can be passed as a contention to switch proclamation.

Constructor in Java – Types of Constructor in Java

import java.util.Scanner;
enum Day
{
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
   THURSDAY, FRIDAY, SATURDAY;
}
public class Test
{
   Day day;
   public Test(Day day)
   {
       this.day = day;
   }
   public void dayIsLike()
   {
       switch (day)
       {
       case MONDAY:
           System.out.println("Mondays are bad.");
           break;
       case FRIDAY:
           System.out.println("Fridays are better.");
           break;
       case SATURDAY:
       case SUNDAY:
           System.out.println("Weekends are best.");
           break;
       default:
           System.out.println("Midweek days are so-so.");
           break;
       }
   }
   public static void main(String[] args)
   {
       String str = "MONDAY";
       Test t1 = new Test(Day.valueOf(str));
       t1.dayIsLike();
   }
}
  • Constants in enum are always public static and final. We cannot have child enums as we use finale.
  • We can declare the main method inside the enum and hence, we can use a command prompt to call it.
enum Color
{
   RED, GREEN, BLUE;
   public static void main(String[] args)
   {
       Color c1 = Color.RED;
       System.out.println(c1);
   }
}

iii. Enum and Inheritance

  • All enums verifiably broaden java.lang.Enum class. As a class can just broaden one parent in Java, so an enum can’t expand whatever else.
  • toString() technique is overridden in java.lang.Enum class, which returns enum consistent name.
  • enum can execute numerous interfaces.

Explore Pair Class in Java With Techniques

iv. values(), ordinal() and valueOf() techniques

  • These methods are available inside java.lang.Enum.
  • values() technique can be utilized to restore all qualities exhibit inside enum.
  • An order is essential in enums. By utilizing ordinal() method, each enum steady file can be discovered, much the same as cluster file.
  • valueOf() technique restores the enum steady of the predefined string values if exists.
enum Color
{
   RED, GREEN, BLUE;
}
public class Test
{
   public static void main(String[] args)
   {
       Color arr[] = Color.values();
       for (Color col : arr)
       {
           System.out.println(col + " at index "
                            + col.ordinal());
       }
       System.out.println(Color.valueOf("RED"));
           }
}

Output:

RED at index 0

GREEN at index 1

BLUE at index 2

RED

v. enum and constructor

  • enum can contain constructor and it is executed independently for each enum consistent with the constant of enum class loading.
  • We can’t make enum questions unequivocally and consequently, we can’t summon enum constructor specifically.

vi. enum and methods

  • enum can contain concrete methods just i.e. no any theoretical technique.
enum Color
{
   RED, GREEN, BLUE;
   private Color()
   {
       System.out.println("Constructor called for : " +
       this.toString());
   }
   public void colorInfo()
   {
       System.out.println("Universal Color");
   }
}
public class Test
{    
   public static void main(String[] args)
   {
       Color c1 = Color.RED;
       System.out.println(c1);
       c1.colorInfo();
   }
}

Output:

Constructor called for: RED

Constructor called for: GREEN

Constructor called for: BLUE

RED

Universal Color

Read Java Image Processing – A Complete Guide 2018

b. strictfp Keyword

strictfp is a keyword in java utilized for limiting floating point values and guaranteeing the same outcome on each stage while performing tasks in the coasting point variable.

Floating point computations are platform subordinate i.e. distinctive output (floating-point variables) is accomplished when a class document is kept running on various platforms (16/32/64 bit processors). To settle these kinds of issue, strictfp catchphrase was presented in JDK 1.2 form by following IEEE 754 guidelines for coasting point counts.

Important focuses:

  • strictfp modifier utilizes with classes, interfaces and techniques as it were.
  • strictfp can’t utilize with unique strategies. Notwithstanding, it can be utilized with theoretical classes/interfaces.
  • Since techniques for an interface are certainly dynamic, strictfp can’t be utilized with any method inside an interface.
public class Test
{
   public strictfp double sum()
   {
       double num1 = 10e+10;
       double num2 = 6e+08;
       return (num1+num2);
   }
   public static strictfp void main(String[] args)
   {
       Test t = new Test();
       System.out.println(t.sum());
   }
}

So, this was all about Important Java Keywords. Hope you like our explanation.

Let’s discuss Java Annotations – Examples & Types of Annotations in Java

5. Conclusion

In this java Keywords tutorial, we had an overview of the important Java keywords and studied two in detail. In the next Java tutorial, we are going to learn about the abstract, volatile, super, static, final, and transient keyword. Furthermore, if you have any query, feel free to ask in the comment section.

For reference