Category Archives: Java Tutorial

Multiple Catch Blocks In Java & Java 7 Multi Catch Syntax

The multiple catch blocks are examined in the order in which they appear. First the subclass catch block should come and then its super class catch block. Multiple Catch Blocks In Java: Multiple catch blocks are required:- If a statement is prone to multiple exceptions. If the try block contains multiple problematic statements. The catch blocks are examined… Read More »

Adding StringBuffer To TreeSet In Java

When you try to add StringBuffer/ StringBuilder to Treeset, exception will be thrown because StringBuffer and StringBuilder classes do not implement Comparable interface. Points to Remember: String class implements Comparable interface StringBuffer class does not implement Comparable interface StringBuilder class does not implement Comparable interface The below code will throw an exception. Output: Exception in thread “main” java.lang.ClassCastException: java.lang.StringBuffer cannot… Read More »

Method Overloading And Ways To Overload A Method In Java

Method Overloading In Java The Java programming language supports overloading methods and Java can distinguish between methods with different method signatures. If a class has two or more methods with same name but different number of arguments or types of arguments or both, then it is known as Method Overloading.   Question: What is method signature? Answer: Method signature means three… Read More »

Overloading Methods That Differ Only By Static Keyword In Java

Lets see what happens when you try to overload two methods with same name, same input parameters but the only difference is that one method is static and the other is non-static. Example: Output: Test.java:7: error: method print(String) is already defined in class Test public void print(String name){ ^ 1 error The output says print() method is already… Read More »

How To Overload Static Methods In Java

Yes, you can overload static methods. Overload Static Methods: The answer is Yes. You can have two or more static methods with the same name, but with different input parameters. Have a look at this example to know how. Example: Output: Hi Hi ABC Happy Learning 🙂

Overloading main() method In Java

Yes, you can overload main() method in Java. Overloading main() method: Yes, you can overload main() method in java but the program doesn’t execute the overloaded main method when you run your program, you have to call the overloaded main method from the actual main method. Example: Output: Calling the actual main() method Example: Now calling the overloaded… Read More »

Default Array Values In Java

If the elements of array are not initialized, then they will be initialized with their default values. Default Array Values: If the elements of array are not initialized, then they will be initialized with their default values. 1) When the elements are of primitive data type: String    —> null int          —> 0 short … Read More »

final Keyword In Java

final is a keyword in Java. It is used with variables, methods and classes. final variable: When final is used with variables, the value of the variable becomes constant means you cannot modify(re-initialize) the value of a final variable. Initializing a final variable: 1) Initialize the value of the final variable when declared. A final variable is called blank final variable,if… Read More »

this Keyword In Java

this is a reference variable that refers to the current object. 1) It is used to refer current class instance variables. Output: Area of rectangle is: 30 unit square The above code could have been written like this: 2) It is used to refer current class constructor. Output: Calling default constructor… Calling parameterized constructor… Area of rectangle is:… Read More »

super Keyword In Java

super is a keyword in Java. It is a reference variable that is used to refer parent class objects. super keyword is related to Inheritance in Java. super Keyword: super is a keyword in Java. It is a reference variable that is used to refer parent class objects. super keyword is related to Inheritance in Java. Uses of super keyword: 1)… Read More »