Skip to main content

Parameter in Java

8 replies [Last post]
rafa318
Offline
Joined: 2008-03-17
Points: 0

Hi people

I would like create a method but i don't know how much parameter i can have in my method, how much parameter can i have in my method?

Tks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jawed_ali
Offline
Joined: 2008-06-20
Points: 0

every parameter are pass by value in java.
The real thing pass when we passing a parameter is bit value of parameter.
When we pass a reference of a object then the bit value of reference is pass and
reference parameter is now pointing the object which is pointing by the passing reference.
For example
String s = new String("abcd"); //here s is a reference of String object
f(s); //and here a function f is calling and reference s is pass
// here is definition of function f
public void f(String s1) {
//some code here
}
//now the bit value of reference s1 is same as reference s
and reference s1 is also pointing the object which is pointing by reference s

walterln
Offline
Joined: 2007-04-16
Points: 0

> all descendants from Object are passed by reference

No, the reference to an Object is passed by value. In your example, the doesnothing reference s is a copy of the reference s in main, that is why assigning it to "abc" does not change where the main reference s points to (the String object "def").

Both of the s refer (at the start of doesnothing) to the same String "def" though, so could modify the same Object (but Strings in Java are immutable, so not in this example).

tarbo
Offline
Joined: 2006-12-18
Points: 0

I know, I know. [i]References[/i] are passed by value, but the object itself is passed by reference.

I'm not completely Java illiterate, you know? ;) Give me some credit, here.

Besides, when someone asks "Can you use parameters in Java," I want to keep it simple.

tarbo
Offline
Joined: 2006-12-18
Points: 0

You may be interested in going through the [url=http://java.sun.com/docs/books/tutorial/]Java Tutorial[/url], especially the part on [url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html]variables[/url].

You can't choose to pass an object by reference or by value in Java. All primitive types are passed by value, all descendants from Object are passed by reference. Assignment to parameters in a method doesn't affect the references or values outside of the method:

[code]
void doesnothing(String s) {
s = "abc";
}

static void main(String[] args) {
String s = "def";
doesnothing(s);
System.out.println(s); // yields "def"
}
[/code]

There is no equivalent to the 'reference' (&) and 'dereference' (*) operators in C. The closest you can come to a pointer is probably an array.

[code]
void doessomething(String[] s) {
s[0] = "abc";
}

static void main(String[] args) {
String[] s = new String { "def" };
doessomething(s);
System.out.println(s[0]); // yields "abc"
}
[/code]

Hope this clarifies,
Jonathan

pelz
Offline
Joined: 2008-06-08
Points: 0

Your second example more exactlier:

[code]
void doessomething(String[] s) {
s[0] = "abc";
}

static void main(String[] args) {
String[] s = new String[] { "def" };
doessomething(s);
System.out.println(s[0]); // yields "abc"
}
[/code]

pelz
Offline
Joined: 2008-06-08
Points: 0

You can have so much parameter what you need.

If you do not need any e.g.:
public void myMethod()

If you want to have only one parameter e.g.:
public int myMethod(int p1)

If you want to have five parameters e.g.:
public double myMethod(int p1, double p2, String p3, int p4, int p5)

And so!

Message was edited by: pelz

Message was edited by: pelz

rafa318
Offline
Joined: 2008-03-17
Points: 0

Do you know if i can use parameter for reference in java?

pelz
Offline
Joined: 2008-06-08
Points: 0

I think this link helps you: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html

Do you think about the reference type as in C++?
E.g.
[code]
int myMethod(int& i1) // ???
[/code]

I think, this syntactic is not possible in Java. But the class objects are reference type automatical, if they are parameters in a method.