Question:
Can someone give me a really basic java program with a return statement and arguments in param?
SomeoneElse
2013-02-22 19:47:47 UTC
I don't fully understand this concept and couldn't really find anything solid online about it. So any information would be appreciated. Is declaring the variables in the parameters the same as just as declaring them in the method and if so then what's the point of using parameters? And does the return method print out the result or just store it in memory to be called at a later time.
Three answers:
Drew
2013-02-22 20:48:40 UTC
public class Example{



private int a; // instance variables are visible to the whole class.. local variables (inside a method)

private int b; //are only available to that method look into "Scope" of a variable online fr more info



public Example(int c,int d){ ///constructor .... initializes the 2 instance variables(class variables)

a=c;

b=d;

}



public int add(int x, int y){ ///returns an int ....that is the sum of whatever values are passed to it

return x+y;



}



public void printInstanceVariables(){ //prints the 2 instance variables out



System.out.println(a);

System.out.println(b);

}







public static void main(String[] args){



Example myExample = new Example(4,5);

int x = myExample.add(7,4);



//since the add method in myExample returns an object of type // int we can store in in an int variable



System.out.println(x); //outputs 11 to the command line

myExample.printInstanceVariables(); //outputs 9 to the command line





/*an object of type Example is created and the constructor of the Example class is invoked passing the values 4 and 5.......the constructor then initializes the 2 instance variables to the values passed...this is the structure of many classes in java...





*/

}



}
green meklar
2013-02-24 08:10:24 UTC
>Is declaring the variables in the parameters the same as just as declaring them in the method and if so then what's the point of using parameters?



They're not the same. The key difference is that variables declared as parameters can (and will) have their values set by the caller. For instance, if I have a method:



public static void blah(int n)

{

System.out.println(n+1);

}



then I could call it from some other part of the program like this:



blah(68093);



and the following would be printed to the console:



68094



See? The blah() method itself never stated that 68093 value; but because n was a parameter, the caller could pass 68093, and the method did its business with n being equal to that number. I could have passed any other number, and the behavior would have changed accordingly.



>And does the return method print out the result or just store it in memory to be called at a later time.



return is not a method, it is a statement. It does not print out the result, it just passes the result value back to the caller. The result value is only read if the caller treats the method call like an expression. For instance, if I have a method:



public static int qwer(int n)

{

return ((n*3)-1);

}



and elsewhere in the program I called it like this:



qwer(5);



then the return value would be lost (and indeed, the call would be useless, because in this case qwer() only does a local computation and does not interact with any data in any way other than by returning a value). However, I could say something like:



int a=qwer(5)+4;

System.out.println(a);



and the following would be printed to the console:



18



See how that works? Once qwer() completed (with n equal to 5), the result (which was 14, of course) was added to 4, and then that 18 was assigned as the value of a. The entire purpose of the return statement is to facilitate this sort of usage.



Technically speaking, Java has enough features that we could simulate the behavior of the return statement using other techniques, and make an entire program out of void methods. But it is simpler and easier to use return.
Robert
2013-02-23 04:31:20 UTC
The code below suits your purpose showing the overloading functionality on a member function area (which finds the area of a square):



public class asquare{

private int x,y;

int arearesult;

public asquare(int x, int y){

this.x=x;

this.y=y;

}

public int area(){

arearesult=this.x*this.y;

return arearesult;

}

public int area(int x, int y){

arearesult=x*y;

return arearesult;

}

public static void main(String[] args) {

asquare psq = new asquare(3,4);

System.out.println(psq.area() + " " + psq.area(3,4));

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...