public class Test {
public static void main(String[] args) {
int max = 0;
max(1, 2, max);
System.out.println(max);
}
public ststic void max(
int value1, int value2, int max) {
if (value1 > value2)
max = value1;
else
max = value2;
}
}

Recommended Answers

All 12 Replies

Is this for homework?
What's your theory as to why this code will not work (apart from spelling errots!)

pass by value means call a method which take parameters
like that {max(1, 2, max);} we call a method max and it's taken a 3 parameter as an integer values.
but please don't post your home work again. to learn programming you must try with your self at first .

your function is incorrect buddy...why do u need a third variable being passed to the function max?

your function is incorrect buddy...why do u need a third variable being passed to the function max?

because if he wouldn't,it would not work.
the method does require three integers to be passed, so he'll need to pass a third one.
I agree that it's useless, since it's not really used in the function itself, so he might be better off with a method that returns an int, instead of a void, but when you read the code and take a guess about what it's supposed to do, you'll see there's nothing wrong with the usage of that third parameter, in this bit of code, it's needed to get the correct output.

next to that, for a beginning developper it's not bad to take a look at a piece of code written like that, and think about what it does (different)

the only error I see in his code, is that he wrote 'ststic' instead of 'static'

there's nothing wrong with the usage of that third parameter, in this bit of code, it's needed to get the correct output.

Sorry, that's wrong.
The whole point of this seems to be about "pass by value". All Java method parameters are passed by value. This means that the use of the third parameter to return a value here is NOT going to work.

Sorry, that's wrong.
The whole point of this seems to be about "pass by value". All Java method parameters are passed by value. This means that the use of the third parameter to return a value here is NOT going to work.

I never said it would
what I did say was: if a method has a signature with three integers as parameters, if you call it with only two, it won't work.
I also said: it's a good thing for starters to read a piece of code like this, and see what it does (different, as in, against what they want/expect it to do)

I was simply responding to your statement "there's nothing wrong with the usage of that third parameter, in this bit of code, it's needed to get the correct output."

There is something very much wrong with the usage. The third parameter is not needed to get the correct output. In fact it's not used for anything meaningful.It's illustrating a way to use of parameters that works in a C-type language with pass by reference, but can never work in Java because all parameters are passed by value.

I was simply responding to your statement "there's nothing wrong with the usage of that third parameter, in this bit of code, it's needed to get the correct output."

There is something very much wrong with the usage. The third parameter is not needed to get the correct output. In fact it's not used for anything meaningful.It's illustrating a way to use of parameters that works in a C-type language with pass by reference, but can never work in Java because all parameters are passed by value.

I agree to that, I should have chosen my words a bit more carefull. but omitting the third param wouldn't lead to the correct output either :)
I only ment to say, that since the signature of the method required a third integer as a parameter, he was not wrong to put a third integer as parameter in the function call.
but I also stated, that, indeed, it was not very usefull

OK, that's cool.
J

because if he wouldn't,it would not work.
the method does require three integers to be passed, so he'll need to pass a third one.
I agree that it's useless, since it's not really used in the function itself, so he might be better off with a method that returns an int, instead of a void, but when you read the code and take a guess about what it's supposed to do, you'll see there's nothing wrong with the usage of that third parameter, in this bit of code, it's needed to get the correct output.

next to that, for a beginning developper it's not bad to take a look at a piece of code written like that, and think about what it does (different)

the only error I see in his code, is that he wrote 'ststic' instead of 'static'

Well its a very bad design, if u look at the code its right, but wat good is it to pass a value wen u can return the max out of the two...

Still missing the point? The code is NOT right. Try it.

Well its a very bad design, if u look at the code its right, but wat good is it to pass a value wen u can return the max out of the two...

I never said it was good design. I did say passing the third value was useless.
if you run this code, I'm quite sure you will find that, even after running the complete app, the value of max will still be zero, so it's not just bad design, it doesn't do what it 's (or might be) supposed to do.

I did say, however, that if you call a function, of which the method signature requires three integer parameters, he's right to pass on the third as well, useless as it might be.

I believe (just my opinion, so don't kill me if I'm wrong) that his professor wants him to test that code, to notice that max doesn't change of value through the course of the application, and, start wondering for himself, why that might be, and, better still, to figure out the way to actually get the maximum out of two integers using a method by himself.

no one's debating the point that it's bad design, it's actually lousy design, but maybe that's one of the points of his assignment, that he should discover just that fact

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.