PDA

View Full Version : [SOLVED] [Java]Default value for function parameters?



fiddler616
November 28th, 2008, 10:17 PM
Hello,
This is a quickie: Is there a way to assign a default value to a parameter in Java? For example, in Python you can do:


def spam(eggs = "tasty"):
print eggs
spam() # "tasty"
spam("gross") #Returns "gross"

Similar syntax in Java has failed me, and Google was obstinate (the only definitive answer came from a forum thatīs way too 1337 for the likes of me...so Iīm not sure I believe it)

Thanks

tinny
November 28th, 2008, 11:00 PM
Hello,
This is a quickie: Is there a way to assign a default value to a parameter in Java? For example, in Python you can do:


def spam(eggs = "tasty"):
print eggs
spam() # "tasty"
spam("gross") #Returns "gross"

Similar syntax in Java has failed me, and Google was obstinate (the only definitive answer came from a forum thatīs way too 1337 for the likes of me...so Iīm not sure I believe it)

Thanks

No. However there are a couple of hacks...

Passing a null value can indicate that a default value should be used.


public void spam(String eggs) {
if (eggs == null) {
//Default
eggs = "tasty"
}
System.out.println(eggs);
}


Or method over loading



public void spam(String eggs) {
System.out.println(eggs);
}

public void spam() {
System.out.println("tasty");
}


Personally id use method overloading

wrtpeeps
November 29th, 2008, 01:27 AM
+1 for overloading.

Reiger
November 29th, 2008, 02:16 AM
+1 for overloading.

Yup, *especially* if you're writing code others will have to work with later.

fiddler616
November 29th, 2008, 03:39 PM
Thanks...however, I seem to be doing something wrong.
(Before you start grilling me, the Java Iīm learning in school uses some kind of special conceptual library called Karel J Robot--you have different classes of robots, they can run about on a grid, etc. Itīs "real Java"--itīs just...weird=
)



. . .
public void go(int dist, boolean safe)
{
for(int ii = 1; ii < dist; ii++)
{
if(safe = true)
{
if(frontIsClear()) //No walls in the way of moving
{
move();
}
}
else //safe = false
{
move();
}
}
}

public void go(int dist) //same as go but with safe defaulted to true
{
for(int ii = 1; ii < dist; ii++)
{
if(frontIsClear())
{
move();
}
}
}


Whenever I call go, safe is always true, even if I explicitly set it to false.


(Before I get trouble from someone:
A)Yes, this is a school course.
B)No, this is not homework--I've gotten immensely bored with repeating instructions, and took it upon myself to read ahead, do research, and make a "master class" which has commonly used methods that I'm tired of defining (it being Thanksgiving Break and all...) They're still working out how nested ifs work--Python, you've saved me :)
)

bobrocks
November 29th, 2008, 05:45 PM
Thanks...however, I seem to be doing something wrong.
(Before you start grilling me, the Java Iīm learning in school uses some kind of special conceptual library called Karel J Robot--you have different classes of robots, they can run about on a grid, etc. Itīs "real Java"--itīs just...weird=
)



. . .
public void go(int dist, boolean safe)
{
for(int ii = 1; ii < dist; ii++)
{
if(safe = true)
{
if(frontIsClear()) //No walls in the way of moving
{
move();
}
}
else //safe = false
{
move();
}
}
}

public void go(int dist) //same as go but with safe defaulted to true
{
for(int ii = 1; ii < dist; ii++)
{
if(frontIsClear())
{
move();
}
}
}


Whenever I call go, safe is always true, even if I explicitly set it to false.


(Before I get trouble from someone:
A)Yes, this is a school course.
B)No, this is not homework--I've gotten immensely bored with repeating instructions, and took it upon myself to read ahead, do research, and make a "master class" which has commonly used methods that I'm tired of defining (it being Thanksgiving Break and all...) They're still working out how nested ifs work--Python, you've saved me :)
)

If I understand your intentions correctly here is some steps to get you going

1) you don't want to be duplicating code. Try something more like



. . .
public void go(int dist, boolean safe)
{
for(int ii = 1; ii < dist; ii++)
{
if(safe = true)
{
if(frontIsClear()) //No walls in the way of moving
{
move();
}
}
else //safe = false
{
move();
}
}
}

public void go(int dist) //same as go but with safe defaulted to true
{
go(dist, true);
}


2) hint - == is not the same as =.

fiddler616
November 29th, 2008, 05:51 PM
If I understand your intentions correctly here is some steps to get you going

1) you don't want to be duplicating code. Try something more like
2) hint - == is not the same as =.
1)Thatīs a good idea, thanks!
2)Aach, youīre right. I used to do this in Python, and got over it, and now itīs back. Argh. More proof that *BASIC (TI-BASIC in my case) ruins your brain.

Solved, awesome!

drubin
November 29th, 2008, 09:11 PM
No. However there are a couple of hacks...

Passing a null value can indicate that a default value should be used.


public void spam(String eggs) {
if (eggs == null) {
//Default
eggs = "tasty"
}
System.out.println(eggs);
}


Or method over loading



public void spam(String eggs) {
System.out.println(eggs);
}

public void spam() {
System.out.println("tasty");
}


Personally id use method overloading

+1 for overloading. (this is how Java does default params) :)

Personally I prefer this approach as it is more default params then duplicating code and having the same thing in each method.



public void spam(String eggs) {
System.out.println(eggs);
}

public void spam() {
spam("tasty");
}