Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 240 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187.1K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 443 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Question about reference Concept

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
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
-
the question is... Why a Integer value is not working like a reference value?, it is working like a primitive valueBecause 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: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.valor = (valor + valor4);
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
-
>
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 -
the question is... Why a Integer value is not working like a reference value?, it is working like a primitive valueBecause 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: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.valor = (valor + valor4);
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. -
Thanks for the link.... it was very helpful
-
Thank you
-
919981 wrote:A prime example why I don't value these certifications.
I am studing for the OCJA
Also, isn't this something that should be in the OCJP certification instead the architect one? -
>
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. -
933581 wrote:The question was completely answered almost two months ago.
I think that this answers your question.>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.
A prime example why I don't value these certifications.
>
I would like to know why you don't value those certifications.
This discussion has been closed.