Forum Stats

  • 3,828,056 Users
  • 2,260,853 Discussions
  • 7,897,440 Comments

Discussions

Question about reference Concept

922984
922984 Member Posts: 3
Hi
I am studing for the OCJA and I was reading that when you send primitive parameters to a method, you have a copy of them, that means that the original value, won't be change when you come back from the method, but, when you sent objects insted of primitive values, they work by reference, thet means the value from the object will be afected during the method process

So, I tryied to send a Method with a Integer value instead of int, because I know Integer is used like a object, but the value was not afected at the end of the method

the question is... Why a Integer value is not working like a reference value?, it is working like a primitive value
Thank you for your help

This is the code:

Integer valor = new Integer(1);
System.out.println("Argument: value = " + valor);
addfour(valor);
System.out.println("After method call: value = " + valor);

-----------

private static void addfour(Integer valor){
Integer valor4 = new Integer(4);
System.out.println("Parameter: value = " + valor);
valor = (valor + valor4);
System.out.println("Leaving method: value = " + valor);
}

The output is :
Argument: value = 1
Parameter: value = 1
Leaving method: value = 5
After method call: value = 1

Best Answer

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Mar 10, 2012 12:31AM Answer ✓
    the question is... Why a Integer value is not working like a reference value?, it is working like a primitive value
    Because you changed the value of the reference you were passed. That doesn't do anything to the original Integer, it just makes the reference point to a different Integer.

    You did that here:
    valor = (valor + valor4);
    If Integer had a method to change its value, which it doesn't, and you had called it, which you didn't, you would have seen that change in the original Integer. But none of those things is true.
    The Integer class is immutable.
    That's not the reason. Even if he had used a mutable class instead of Integer he wouldn't have seen what he expects.

Answers

  • Unknown
    edited Mar 10, 2012 12:23AM
    >
    Why a Integer value is not working like a reference value?
    >

    The Integer class is immutable. So, you can't change its value after it has been created.

    See this JavaWorld article for the details.
    http://www.javaworld.com/javaworld/javaqa/2000-06/01-qa-0602-immutable.html
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Mar 10, 2012 12:31AM Answer ✓
    the question is... Why a Integer value is not working like a reference value?, it is working like a primitive value
    Because you changed the value of the reference you were passed. That doesn't do anything to the original Integer, it just makes the reference point to a different Integer.

    You did that here:
    valor = (valor + valor4);
    If Integer had a method to change its value, which it doesn't, and you had called it, which you didn't, you would have seen that change in the original Integer. But none of those things is true.
    The Integer class is immutable.
    That's not the reason. Even if he had used a mutable class instead of Integer he wouldn't have seen what he expects.
  • 922984
    922984 Member Posts: 3
    Thanks for the link.... it was very helpful =)
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    919981 wrote:
    I am studing for the OCJA
    A prime example why I don't value these certifications.

    Also, isn't this something that should be in the OCJP certification instead the architect one?
  • 936584
    936584 Member Posts: 13
    >
    when you sent objects insted of primitive values, they work by reference, thet means the value from the object will be afected during the method process
    >

    That's not what Java tutorials say, read about Passing Reference Data Types Arguments in this link:
    http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

    You'll find this:

    >
    Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
    >

    I think that this answers your question.

    >
    A prime example why I don't value these certifications.
    >

    I would like to know why you don't value those certifications.
  • 796440
    796440 Member Posts: 19,179 Gold Trophy
    933581 wrote:
    I think that this answers your question.
    The question was completely answered almost two months ago.
    >
    A prime example why I don't value these certifications.
    >

    I would like to know why you don't value those certifications.
    Probably for the same reason I don't, and a lot of people don't: They don't actually show that you are competent in the area they're certifying. People "earn" them by cramming for the exam, just so that can put another acronym on their resume, and a week later they've forgotten what they supposedly learned. And they've probably never used 90% of those concepts in practice.
This discussion has been closed.