This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static binding with function overloading in java and memory space use with function overload ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static binding with function overloading in java and memory space use with function overload ?" Watch "static binding with function overloading in java and memory space use with function overload ?" New topic
Author

static binding with function overloading in java and memory space use with function overload ?

shyam ji gautam
Ranch Hand

Joined: Sep 17, 2011
Posts: 48
class OverloadDemo
{
void triangleArea(float base, float height)
{
float area;
area = base * height / 2.0f;
System.out.println(“Area = “ + Area);
}

void triangleArea(float side1, float side2, float side3)
{
float area,s;
s = (side1 + side2 + side3) / 2.0;
area = Math.sqrt(s*(s-side1) * (s-side2) * (s-side3) );
System.out.println(“Area = “ + area);
}
}



class MainOverloadDemo
{
public static void main(String args[])
{
OverloadDemo ovrldDemo = new OverloadDemo();
ovrldDemo.triangleArea(20.12,58.36);

ovrldDemo triangleArea(63.12,54.26,95.24);
}
}

my first question is
is it static binding or not ?
according to me here compiler is aware at compile time that need to call void triangleArea(float base, float height)
Because no of parameter different and the JVM invokes void triangleArea(float base, float height) in the class OverloadDemo at run time


but i also knows that All the instance method calls are always resolved at runtime, and here void triangleArea(float base, float height) is also a instance method so i am confuse which statement is right ?


and third doubt is that when we use method overloading then memory space uitilization is improve how with refernce to java ?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

shyam ji gautam wrote:my first question is

Shyman: Please UseCodeTags (←click). I've also locked your other (duplicate) thread.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
subhash kumar
Ranch Hand

Joined: Jul 14, 2010
Posts: 63
Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

When you are compiling your code, compiler is statisfied by comparing the method calls with the
method definition ,this is static binding.

When your are running this code JVM will decide which method is called based upon the number
and datatype of parameters to arguments, this is dynamic binding.

method overloading is not used from the prospective of memory improvement, it fits in different usage
like sum can be operated on two integers also and two floats also So more towards funtionality.


Subhash Kumar
Attitude is everything
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Why are you using float arithmetic? Avoid it as much as possible; use doubles, or (if possible) BigDecimal. I don’t think you can use BigDecimal here, however.
Yes, you are overloading. Yes, you can call that static binding, though maybe compile‑time binding is a better term. You can actually see what is happening by compiling those classes and inspecting their bytecode.You should see which methods are called, and that the compiler has chosen which method to invoke. They will be different.
You can do the same for overridingNow create Cat, Dog and Horse classes, like this, and override the eat() methodThat is runtime binding. If you print the bytecode, you will see all those calls are the Animal#eat() method. But when you execute that code, you will see the Doc Cat or Horse versions are actually called. That is how you implement polymorphism.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
shyam ji gautam wrote:
my first question is
is it static binding or not ?
according to me here compiler is aware at compile time that need to call void triangleArea(float base, float height)
Because no of parameter different and the JVM invokes void triangleArea(float base, float height) in the class OverloadDemo at run time

Yes it is static binding for overloaded methods.
shyam ji gautam wrote:
but i also knows that All the instance method calls are always resolved at runtime, and here void triangleArea(float base, float height) is also a instance method so i am confuse which statement is right ?

No. Overloaded methods are resolved at compile time.

shyam ji gautam wrote:
and third doubt is that when we use method overloading then memory space uitilization is improve how with refernce to java ?

in which way???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
subhash kumar wrote: . . . When your are running this code JVM will decide which method is called based upon the number and datatype of parameters to arguments, this is dynamic binding. . . .
Gaurangkumar Khalasi has correctly answered that point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static binding with function overloading in java and memory space use with function overload ?
 
Similar Threads
I need help on an equals method
Using super keyword
(new to java)variables might not have been initialized
Exception problem.
static binding with function overloading in java and memory space use with function overload ?