• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing arguments to methods and returning values from a method  RSS feed

 
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) We passes arguments to methods and called method uses parameters to accepts those arguments.
2) We can pass multiple arguments to a method but their type and order must be same with that of parameters in the called method.

3) Java is pass-by-value means pass-by-copy
For e.g. -
int x = 100;
emp.display(x); //just a copy of x is passed which is 100 by concept of pass by value in java


void display(int y){
y = 90; //doesn't change the value of x, x remains 100
}

4) In Java, we can send multiple return values from a method to the caller method by stuffing all the values in the same type of array and passing that array back to its same type of variable(array).

5) We can even return byte where int were expecting or float where double were expecting (known as implicit widening in Java or implicit promote to larger types). On the other hand, larger to smaller such as returning int where byte were expecting by doing type casting to smaller types(precision values could be lost here).

No idea whether Java completely support or against of pass by reference or not.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:No idea whether Java completely support or against of pass by reference or not.

An oft-asked question, particularly by people coming from C or C++.

And the answer is: No, Java does not support 'pass by reference' as meant by those languages.

However, nor does it pass copies of objects around (as you can in C). In Java, an object is only ever referred to by its reference (which is why they are called 'reference types') and what Java does is pass references by value.

This article may help to explain it; and there are also explanations available on this site (I believe one is in the Campfire Stories section, and it's called 'Pass by value please').

Winston
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

And the answer is: No, Java does not support 'pass by reference' as meant by those languages.

However, nor does it pass copies of objects around (as you can in C). In Java, an object is only ever referred to by its reference (which is why they are called 'reference types') and what Java does is pass references by value.

This article may help to explain it; and there are also explanations available on this site (I believe one is in the Campfire Stories section, and it's called 'Pass by value please').

Winston


But why it is not pass by reference. Could you please explain that in your simple words? Just FYI, I visited the link given by you and does not help so much for me.



Can we do something like this:-
emp.display(com);



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:But why it is not pass by reference. Could you please explain that in your simple words?

No, because the business is not simple.

I think the article I posted got one thing absolutely right: Java should never have called their object pointers "references", because it already had a prior meaning, and "pass by reference" has a very specific meaning, which does not exist in Java.
I think "target" or "location" would have been a much better choice.

My suggestion is that you do some reading on the subject (Google is your friend), specifically because it is not simple.

Can we do something like this:-
emp.display(com);

Yes, but that has nothing to do with passing by reference.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!