Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Top 50 Java Keywords-The Complete Reference and Guide

Introduction:-
Java has a large list of keywords. They are extensively used in writing the source code. Java keywords are basic things that a programmer should know in order to code. Understanding Java keywords is difficult for some programmers. Today I will take you through a guide which will make you familiar with the basics of Java keywords. I will provide you with the definition of most frequent Java keywords used. I will try my best to make you understand the fundamentals behind their existence in Java programming language. If you are beginner to Java, it will help you in many possible ways. If you are going for SCJP (Sun Certified Java Programmer) exam than this hub is a must read for you. Let us first look at list of keywords in Java in ascending order:

1. abstract: - Java provides abstract keyword to signify something empty. By empty we mean that it has no definition or implementation. Keyword abstract is used before the classes and methods. At the time of declaring a class we mark it as abstract. An abstract class cannot be instantiated. It means an abstract class cannot have Objects. Generally when we place keyword abstract before a class, we make sure that this class is basically an abstract implementation or generic class and the functional characteristics can only be added by extending the class. We cannot create objects of abstract class but we can make their sub-class and create objects of that. By abstract method, we mean that method doesn't have any body. An abstract method doesn't have any statements to execute. Classes that contain abstract method must also be assigned as abstract class and implementation to their abstract method must be provided by extending those classes. If subclasses fail to provide implementation to super-class abstract methods, than they are also marked as abstract.

2. assert: - Java provides us with a debugging keyword called as assert. In Java, assert is a normal Java statement which a programmer puts in the code, that he/she thinks will always be true. Generally, assert statements are being used by the programmer for the debugging purpose. Assertion statements can be turned on or off. While running a program if we enable assertion than wherever our assert statement gets failed Assertion Error is thrown by the program and whole program gets terminated. Generally, assertions are placed by programmer in the code where he believes that statement will be true. After enabling assertion if there is a failure, programmer gets an idea that in code there is some bug. The code fails with an Assertion Error.

3. boolean: - A boolean keyword in Java is a data type for a variable which can store two values, true or false. Generally, boolean keyword is a data type which has size of 1 bit. In Java I am not sure about the size that whether it store 1 bit or not.

4. break: - In Java, 'break' keyword is of quite high importance. As the name suggest, break statement breaks code from one point and transfer flow to other part of program to execute. Generally let say in if block you have placed a break statement. If your code enters if statement an encounters break statement it will stop executing further if block and come out of if block and continue to execute. That is when you encounter break statement execution breaks that block of code and control gets transfer to next statement just after the block. Generally break statement are mostly used with switch statement. If break is used within a labeled block, than as soon as break gets encountered, the program starts executing just after labeled block.

5. byte :- In Java, byte keyword is used as a data type for storing 8 bits of information for an integer. If it’s used before a method definition than that method, when called will always return a byte value.

6. case :- In Java, case keyword is used within switch statements. A condition in switch statement is compared with the cases in switch statement. Whatever case matches with the switch expression that case is executed.

7. catch :- In Java, catch keyword has a group of statements that catches exception, if there is any exception in try block proceeding to it. The statements have a way to deal with the exception or have a way to let programmer know that something is needed to be correct.

8. char :- In Java, char keyword is used as a data type which can store 16 bit Unicode character. If the keyword is placed before method declaration than the method upon execution returns a value which is a char.

9. class :- In Java, class keyword is used to define a class. Generally a class acts as a blue print for an object. It defines implementation for the object. It has statements defining variables, methods, inner classes etc. An Object when instantiated for a particular class has all the physical implementation of what is defined in the class.

10. const :- It is a reserved keyword in Java but it has no use in Java and Java has provided it no function to perform.

11. continue :- In Java, the continue keyword is used when we want rest of the statement after continue keyword to get skipped and want program to continue with the next iteration.

12. default :- In Java, default keyword is used within switch statement. It is used their optionally, if no Java case matches with the expression than the default case is executed by the program.

13. do :- In Java, do keyword is used to implement do-while loop. Generally, do keyword is used when we want to loop a block of statement once. After that the boolean condition gets evaluated, if condition is yes the loop execute again, but if condition comes out to be false the loop exits.

14. double :- In Java, double keyword is used as a data type for storing 64 bits of information for a float type. If it’s used before a method definition than that method when called will always return a double value.

15. else :- In Java, else keyword is used along with if keyword to create an if-else statement. Generally, a condition is evaluated in if block brackets. If the condition evaluates to true if block body gets executed. If the condition is evaluated to false else block body gets executed.

16. enum :- It is used in Java language to declare a data type consisting of a set of named values. They are constants.

17. extends :- In Java, extends keyword is used specify the super class of a subclass, also in interface declaration to specify one or more super interfaces. If we take an example say let say class A extends class B, here class A adds functionality to class B by adding fields or methods. It goes same with interfaces.

18. final :- Generally, final keyword is used to make elements constant. That is once assigned cannot be changed. A final class cannot be sub-classed. A final variable cannot be assigned a new value after it has been assigned to a value. A final method cannot be overridden.

19. finally :- In Java, finally keyword is used to define a block of statements after try to catch blocks. This block executes after try block, or after catch block, or before any return statement.

20. float :- In Java, float keyword is used as a data type for storing 32 bits of information for a float type. If it’s used before a method definition than that method when called will always return a float value.

21. for :- In Java, for keyword is used to create looping statement that is for loop. In this for loop a counter variable is initialized, than a boolean condition is evaluated and compared with counter variable or any expression which turns out to either true or false. If the condition comes out to be true a block of statements following for keyword executes, if condition comes out to be false for loop terminates.

22. goto :- In Java, a goto keyword is a reserved keyword which is not used in the Java language and has no function provided to it.

23. if :- In Java, if keyword is used (optionally) along with else keyword to create an if-else statement. Generally, a condition is evaluated in if block brackets. If the condition evaluates to true if block body gets executed. If the condition is evaluated to false else block body gets executed.

24. implements :- In Java, implements keywords is used in class declaration. Generally, implements keyword implements functionality of interfaces. Interfaces are abstract in nature; their functionality is implemented in the class which implements it.

25. import :- In Java, import statement is used at the start of a Java file, just after package declaration. Generally, import statement gets those classes in a program whose functionality we want to use in the program. It is also used in importing static members.

26. instanceof :- In Java, instanceof operator is a binary operator which is used to test an IS-A relationship between a class and object. Object is first operand and Class or Interface is a second operand.

27. int :- In Java, int keyword is used as a data type for storing 32 bits of information for an integer type. If it’s used before a method definition than that method when called will always return an int value.

28. interface :- In Java, interface is a special type of structure which contains abstract methods, constant fields which are generally static or final.

29. long :- In Java, long keyword is used as a data type for storing 64 bits of information for an integer type. If it’s used before a method definition than that method when called will always return a long value.

30. native :- This keyword is generally used in declaring method signifying that method's implementation is not in the same source file but in different source file and in different language too.

31. new :- Java has a keyword called as new, which has been used to create a new instance of a class on heap. It is used is object creation and providing memory to it.

32. package :- Packages have been created by the package keyword. In the package classes are kept which constitute a relation between each other.

33. private :- In Java, private keyword can be used in declaration of methods, fields, inner class. This makes the methods, fields and inner class access to own class members.

34. protected :- In Java, protected keyword can be used in declaration of methods, fields, inner class. This makes the methods, fields and inner class access to own class members, to sub class and classes of same package.

35. public :- In Java, public keyword can be used before class, methods, fields. This makes the class , methods and fields accessible from any other class.

36. return :- In Java, return keyword is used to pass a value from a method to caller method. It also signifies end of execution of a method.

37. short :- In Java, short keyword is mainly used when we want to declare a field that can hold 16 bit of integer data. It is also placed before method declaration which signifies that method will return only short integer.

38. static :- In Java, static keyword is used when you want to declare method, fields and inner class as class members rather object members, generally static members belong to a class rather than instance of that class. Therefore only one copy is made for them and used by the class as its own implementation.

39. strictfp :- To ensure portability of floating point we ensure that they are platform independent.

40. super :- In Java, super keywords is used to access overridden methods and hidden members of super class by its subclass. It is also used in constructor of a subclass to pass control over super class constructor.

41. switch :- In Java switch keyword is used to have multi-decision making statements. Generally switch is mostly used with keywords case, break and default. This decision making block evaluates an expression first. After the expression is evaluated the value is compared with various case statements. Any value that matches with the expression, case associated with it gets executed. If no expression matches the default case gets executed.

42. synchronized :- In java, synchronized keyword is used before the method declaration and before block of code to get mutual lock for an object. It means that the method or block of code gets locked by the object till it gets fully unlocked or gets fully executed by that object. Generally classes, fields and interfaces cannot be declared as synchronized. The static method gets code synchronization by class itself.

43. this :- In Java, this keyword is used as a reference to currently executable object. Generally if we want to access class members we can use this keyword to access them. If we want to refer to a current object we use this keyword. We also use this keyword to transfer control from one constructor to another in the same class.

44. throw :- In Java, when we want to throw a declared exception, the execution points to the catch block which has provided statements to overcome the exception. If no catch block is declared in the current method than the exception is passed to the calling method. It continues till it found exception handler. If no exception handler is found over the stack that exception is then transferred to the Uncaught Exception handler.

45. throws :- In Java, we use throws keyword in the method declarations. If we get an exception in a method and method has no implementation for that exception, than method throws an exception. The exception thrown has been handled by the method who has called to current method in execution.

46. transient :- Whenever we declare a variable as transient, that variable is not the part of Object serialization. When an object gets saved through default serialization all the non-transient variable retain their value after deserialization. If you want the variable should have default value after deserialization than make it transient.

47. try :- In Java, whenever we want to do exception handling we use try keyword. Generally try block is used where we are sure that exception can occur. In try block statements are executed normally and as soon as exception occur it is handled my catch keyword following try block. It must have at least one catch or finally block.

48. void :- In Java, when we use void before any method declaration, we make sure that method doesn't return any value.

49. volatile :- In Java, volatile keyword provides a lock free mechanism. If a variable is assigned as volatile than all the threads accessing it can modify its current value without keeping a separate copy. A volatile keyword is accessed by all threads simultaneously. They operate on current value of variable rather than cached value.

50. while :- In Java, while keyword is used to create looping statement that is while loop. In this while loop a boolean condition is evaluated and compared with counter variable or any expression which turns out to either true or false. If the condition comes out to be true a block of statements following while keyword executes, if condition comes out to be false the while loop terminates.

51. false :- In Java, false keyword is the literal for the data type Boolean. Expressions are compared by this literal value.

52. null :- In Java, null keyword is the literal for the reference variable. Expressions are compared by this literal value. It is a reserved keyword.

53. true :- In Java, true keyword is the literal for the data type Boolean. Expressions are compared by this literal value.

How to concatenate two strings together in Java source code

Introduction :-
In Java, Concatenating two strings together is of high importance. Java programmer who is a beginner must know how to concatenated two string ?. Learning Java by example is the most effective proven technique. Java fundamentals are necessary to get an idea for writing an efficient code. Our program code in this leaf will need Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.Java provides many mechanism to concatenated two strings. I will be discussing two of the mechanism here.

1. String concatenate operator (+).
2. String class method concat().

So to learn Java effectively , you should know these two mechanism to concatenate two strings. Let us begin with both the methods by an example and its explanation.
Concatenation of two Strings Explanation :-
By Concatenation of two Strings we mean that adding or appending one string to another string. We can also state that it is adding of two strings together. Concatenation of strings comes handy when dealing with the strings in a complex code. Generally concatenation of strings can occur in many ways. Most of the mechanism used is been coded into the sample code below. Let us look into the code step-by-step and see what it does?.


Program to demonstrate working of String Concatenation

public class Concatenation { 
    public static void main(String[ ] args) { 
        String ruler1 = " a ";
        String ruler2 = ruler1 + "b" + ruler1;
        String ruler3 = ruler2 + "c" + ruler2;
        String ruler4 = ruler3 + "d" + ruler3;
        
        System.out.println(ruler1);
        System.out.println(ruler2);
        System.out.println(ruler3);
        System.out.println(ruler4);
               
        String ruler5 = " 1 ";
        String ruler6 = "2";
        String ruler7 = ruler5.concat(ruler6);
        System.out.println(ruler7);
    }
}

Output and Explanation of working of above sample code :-
Above program shows us the working of string concatenation operator + and String class concat method. When we run above program we get output as:
a
a b a
a b a c a b a
a b a c a b a d a b a c a b a
1 2
Line 4 to 6:- All the three lines shows the code how + operator is used to concatenate the strings together. Line 4 code String ruler2 = ruler1 + "b" + ruler1; takes ruler1 string value which is " a " appends value "b" to it. So our new value becomes " a b". Now our code takes value " a b" and appends " a " to it. So after line 4 gets completely executed ruler2 gets value as " a b a ". Same execution is done for line 5 and 6.

Line 15:- This line shows us the code for the String method called as concat(). This method takes one string as an argument and appends it to the value of the string calling that method. So in our case ruler5.concat( ruler6 ) takes ruler5 value and appends ruler6 value to it and assign that value to ruler7. In our case it takes " 1 " and appends "2" to it. Finally ruler7 gets the value as " 1 2".

Line 8 to 11 and Line 16:- All these 5-6 lines just prints the values of ruler1, ruler2, ruler3, ruler4 and ruler7 on the console.

Command-Line Arguments in Java programming language


Introduction :-

Hello friends, having question in mind, How to use Command-Line Arguments in Java ?. In this hub I will be teaching you how to utilize java command-line utilities?. Learning Java by example is the most effective proven technique. Java fundamentals are necessary in order to get an idea for writing a efficient code. Our program code in this hub will require Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.




Click here to download complete source code 

Command-Line Argument Explanation :-
 
A normal Java application before running, can accept multiple command line arguments from the programmer via console or IDE runtime configuration. The arguments are specified by the user at the command line which are required by the Java applications. The user enters command line arguments while launching the program by java command. This passing of arguments by java command is optional. The main method gets arguments from command line and stores it in an array of String. The array elements are initialized by whatever user types after class-name in java command. Let us take an example, suppose we want to launch a class say Hubpages.java with command line arguments, we type command after compilation as :

java Hubpages I am hubber
 
Here Hubpages is the name of class we want to launch, "I", "am", "hubber" would be three String passed to program's main method, which will store all three in the form of array of Strings. Generally, space between I, am and hubber makes them separate strings. If we want them to be one string we use double quotes "". In our example we can pass it as one single string as "I am hubber". Let us look at the below sample code and try to figure out what would be the output.

Program to demonstrate working of Command-Line Arguments

public class Argument {
   public static void main(String[ ] args) {
        System.out.print("Hi, ");
        System.out.print(args[0] + " " );
        System.out.print(args[1] + " " );
        System.out.println(". How are you?");
    }
}


Output and Explanation of working of above sample code :-

When we run above program by command say : java Argument Java Programs We get the following output on the console window:
Hi, Java Programs . How are you?
Here we pass two command-line arguments "Java" and "Programs". These two strings are passed as an argument to main method and gets stored in the form of an array. Where args[0] is assigned "Java" value and args[1] is assigned "Programs" value. These values are printed on console by System.out.print method.

Parsing Numeric Command-Line Arguments :- 

Suppose the programmer wants to enter a numeric value on command-line and let say for an example he inputs 10. Now the problem in this case is that though he had input value as 10 which is a numeric value of data type int, main method will store it as a string which is equal to "10". In order to use numeric value in program we need to parse string tointeger value. To do parsing we use Integer class which has method called as parseInt. This is static method and has argument which is a String. This method takes the String value and parses to numeric type which we can use in our program numerically. Let us look into through sample code below :

public class Argument {
   public static void main(String[ ] args) {
        
       int number1 = Integer.parseInt(args[0]);
       int number2 = Integer.parseInt(args[1]);


       int sum = number1 + number2;
       
       System.out.print("The sum of two number is : " + sum);
       
    }
}


Output and Explanation of working of above sample code :-

When we run above program by command say : java Argument 10 20 We get the following output on the console window:
The sum of two number is : 30
Here we pass two command-line arguments "10" and "20". Remember these two values are strings and they need to be first convert into numeric value. For this we have used Integer class method called as parseInt. It takes string and parses it to integer value. If value provided to this method is not a number, we get an exception such as NumberFormatException.
 
© 2021 Learn Java by Examples Template by Hubberspot