Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parameter Passing

 
Gus Hayes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am a newbie to Java and found this tutorial on the web and would appreciate if someone could help me with it.
Thanks in advance,
Gus



class PassObj

{

int n1;

int n2;

// constructor

PassObj()

{

n1 = 0;

n2 = 0;

}



PassObj(int p1, int p2)

{

n1 = p1;

n2 = p2;

}



void multiply(PassObj p1)

{

int temp;

temp = p1.n1 * p1.n2;

System.out.println("Multiplication is " + temp);

}

public static void main(String args[])

{

PassObj obj1 = new PassObj(5,6);

PassObj obj2 = new PassObj();

obj2.multiply(obj1);

}

}



This is a program I got from a Java tutorial online. If I give my understanding of it could someone please tell me if I am correct or tell me where I have gone wrong. I am a newbie so all help is greatly appreciated.



1. The class PassObj has two int variables n1 & n2.

2. These as initialised to zero.

3. In Main, there are 2 instances of the class PassObj created. These are obj1 & obj2.

4. The multiply method is called with parameters 5 & 6 passed to it. The multiplication is done and the result is shown in the console.

My main issues are what is the point of:



PassObj(int p1, int p2)

{

n1 = p1;

n2 = p2;

}

and does the instantiation PassObj obj1 = new PassObj(5,6); actually pass parameters to the above constructor.
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what you want exactly. But, i will try to help. PassObj(int p1, int p2) is to allow you to initialize the variables of a PassObj instance.
The instantiation PassObj obj1 = new PassObj(5,6); actually pass parameters to the above constructor - yes, but here parameters are passed by copy.
How to check when things are passed by copy or value? Try the code below.



And read this link too.
http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html
 
vinay chaturvedi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.
 
vinay chaturvedi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gus Hayes wrote :
The multiply method is called with parameters 5 & 6 passed to it.


*First of all the multiply method is called with the PassObj object passed to it.

The point of :

is to assign the values p1 and p2 to instance variables n1 and n2.

When you are instantiating PassObj "PassObj obj1 = new PassObj(5,6);" the PassObj(int p1, int p2) constructor will be called and values will be assigned.

The constructor call is shown in bold in above line.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find that tutorial? We require you quote it, so as not to breach copyright.
 
Gus Hayes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies Guys ... The source of the tutorial is http://www.java2all.com
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the tutorial name: was it this example?

Lots of people get confused about pass by value and pass by reference, particularly when people write things like “Primitives are passed by value and Reference types are passed by reference” (see for example this link (No 6)). You can see examples of real‑life pass‑by‑value; cats do it naturally (‍), as you can read in this thread (if you have a week to spare).

Briefly: if you pass anything to a method, you cannot change the original value. If you could, that would be pass‑by‑reference. You can however change the state of a mutable object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic