Core Java

Postmortem of Method Overloading in Java.

Hello Code geeks,
I’m back again to share with you all an interesting overloading concept which I have just learnt.

Java Overlaoding


Please have a look at the following code,

public class OverloadDemo {

	public static void  hello(Integer i)
	public static void  hello(String s)
	public static void main(String[] args) {


What will be the output of the program?
How many of you have noticed that there is a compilation error in the above code. Well, yes there is a compilation error at Line 16 which says “The method hello(Integer) is ambiguous for the type OverloadDemo”

I was playing around with some programs implementing method overloading. I got this error. So, let’s jump in right away and figure out what’s the root cause.

So, here we have method hello which is overloaded with two different parameters.
1.hello(Integer i)
2.hello(String s)
So, Here if we pass a value like hello(1), the Integer version of it will be called and output will be Integer and if we call like this hello(“One”), the String version of it will be called and output will be String.

So, why are we getting a compilation error once we try to pass hello(null). Because, as String and Integer are classes and they both can have null values.When compiler executes it, it will be ambiguous for it, as which method it has to call. Is it hello(Integer)? Which can also take a null argument or Is it hello(String) which as well can take a null argument again.

So, for solving this, changes the Integer to int.Then everything works fine.Now, as we identified the root cause for this issue, this can be fixed by whatever the way it is possible.

Forgot to mention that, I’m preparing for OCJP7 Up gradation exam.So, that’s the reason I’m coming across such things while I’m coding.This might look simple, but in the OCJP we might not actually get it right unless we’re aware or came across of such a scenario.As I always say, OCJP is all about tricky questions.

Have a Good Night.


How to get the size of resultset in Java without iterating though ResultSet object?

Often many of us would come across such a situation where we need to know the size of the resultset returned from a database query without iterating the ResultSet object.

I also encountered a similar situation where, I needed to create an object with all the information of a particular row.

Consider this example.I have a person_info table, where I have the information like person_id,first_name,last_name,middle_name,mobile,city,gender,date_of_birth,age etc.

Now I need to query the database and for every row returned I need to create an JSONObject array like,

JSONObject json[]=new JSONObject[SIZE];

So, we have to initialize the size before iterating through ResultSet object.

After doing a lot of mistakes, found the right way to do this. Here it is.

 Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 ResultSet rs=st.executeQuery("SELECT * from persons");

 int SIZE= 0;
 if (rs != null)
 size = rs.getRow();

System.out.println("SIZE: "+size);

Happy Coding 🙂