public class Test {

    public static int f1( int m1 ) 
        return ( m1 + 1 );

    public static double f1( double m1 ) 
        return ( m1 * m1 );

    public static void main( String[] arg ) {
        double x1, x3;
        x1 = 2.0;
        x2 = 5.0;
        x3 = f1( x1 );
        System.out.println("x3 = “ + x3 );

I know it prints out 4.0 but how does it get this answer. Im cunfused as to where you get # for m1?

Recommended Answers

All 2 Replies

Ok i figured it out incase anyone needed to know as well. Essentially since x1 is a double you ise the second method and you substitute x1 for m1 making it 2.0 * 2.0 = 4.0.

Glad you figured it out. This is very basic Java/C++/C# coding. The type of variable that is passed to an overloaded function (which f1() is) will determine which instance of the function is invoked. Since you passed a double (x1), it invoked public static double f1( double m1 ) and not the integer version.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.