• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Defining method parameters

Hi,

I have a theoretical question about the use of method parameters in Java. I read that method parameters cannot be defined as either 'in, out, or in out' as they often can be in some other programming languages. Is this correct, and what is the impact of this on Java code?

Any help appreciated.

Thanks
0
nhay59
Asked:
nhay59
  • 5
  • 2
  • 2
2 Solutions
 
CEHJCommented:
It is correct. All parameters are 'in' and all are passed by value
0
 
phuocnhCommented:
Hi CEHJ!
You arenot absolutely right!
Primitive arguments, such as an int or an array, are passed by value, the rest are passed by reference.
When invoked, a method or a constructor receives the value of the variable passed in and the method cannot change its value.
To get a better idea of what this means, let's look at a method called getRGBColor within a class called Pen. This method is attempting to return three values by setting the values of its arguments:

    public class Pen {
        private int redValue, greenValue, blueValue;
        ...
        //This method does not work as intended.
        public void getRGBColor(int red, int green, int blue) {
            red = redValue;
            green = greenValue;
            blue = blueValue;
        }
    }

This simply does not work. The red, green, and blue variables exist only within the scope of the getRGBColor method. When that method returns, those variables are gone and any changes to them lost.

Let's rewrite the getRGBColor method so that it does what was intended. First, we need a new type of object, RGBColor, that can hold the red, green, and blue values of a color in RGB space:

    public class RGBColor {
        public int red, green, blue;
    }

Now we can rewrite getRGBColor so that it accepts an RGBColor object as an argument. The getRGBColor method returns the current color of the pen by setting the red, green, and blue member variables of its RGBColor argument:

    public class Pen {
        private int redValue, greenValue, blueValue;
        ...
        public void getRGBColor(RGBColor aColor) {
            aColor.red = redValue;
            aColor.green = greenValue;
            aColor.blue = blueValue;
        }
    }

The changes made to the RGBColor object within the getRGBColor method persist after the method returns, because aColor is a reference to an object that exists outside the scope of the method.
Reference http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
Phuoc H. Nguyen
0
 
CEHJCommented:
>>
the rest are passed by reference.
>>

No. *Nothing* is passed by reference. If what you're saying were correct, the following would work

void changeObject(Object old) {
    old = new Object();
}

but it won't ;-)

http://mindprod.com/jgloss/callbyvalue.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
phuocnhCommented:
I think the implementation of Java make people misunderstand here
Object old;
old is not a object whose type is Object.
old is a pointer which point to the object.
If you view old is an object, you must accept java pass-by-reference.
If you view old is a pointer, ok. java is pass-by-value strictly.
I think the following article will help nhay59 get more information
http://javadude.com/articles/passbyvalue.htm
Thanks CEHJ about your good discussion
Phuoc H. Nguyen
0
 
CEHJCommented:
This is more like what's going on to use an analogy, which really isn't passing by reference

void (const *someType copyOfAPointer)
0
 
nhay59Author Commented:
Hi,

Thanks for all the replies. Do you think the fact that method parameters are all 'in' has any effect on the quality of Java code? What effect does this actually have on the code?

Thanks for the help
0
 
CEHJCommented:
Off the top of my head, here are two, plus and minus effects

a. resource use is increased due to pass by value
b. readability and maintainability is made easier by pass by value

In most cases, b. is by far the more important
0
 
nhay59Author Commented:
Hi,

That's great. Thanks for the help.

Have a great week.
0
 
CEHJCommented:
:-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now