This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Reference or a pointer to object  RSS feed

 
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't feel what is difference between a reference and a pointer to object.
When I pass the arguments to methods, I am very confused about String and StringBuffer or StringBuilder..


Thanks in advance.
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:
I don't feel what is difference between a reference and a pointer to object.
When I pass the arguments to methods, I am very confused about String and StringBuffer or StringBuilder..



In another language, specifically C++, which has a concept of both references and pointers, the difference is very clear. A reference is similar to a pointer, except you don't really have to know how to deal with the memory address. The C++ compiler will do all the work necessary to make the reference variable as an "alias" for the variable being referred to. And a pointer gives full access to the memory address, you can do anything with it, including full pointer arithmetic with it. The downside, of course, is that beginners tend to struggle understanding pointers.

With Java, there are no pointers, not in the C++ sense, that is. Also, there is no need to have pointer vs reference declarations either (as there are no pointers). So, this makes Java much more "elegant" or "dumb down", depending if you are a fan of pointers...


So, back to your question, there is really no "difference" to feel. There is only one way to declare variables to objects, and they behave only in the way that is specified.... so, I am going to speculate that your issue is more related to an understanding of the behavior with specific examples, than understanding the difference between two concepts (one of which does not exist with Java). Can you elaborate your confusion a bit? Preferably, with examples?

Henry
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry for your detailed response.

When I learn Pascal I don't understand pointers.

English is not my native language, so I am sorry for my grammar errors.

 
Sheriff
Posts: 13479
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dana Ucaed wrote:I don't feel what is difference between a reference and a pointer to object.


There is none. A reference variable is a pointer to an object.

When I pass the arguments to methods, I am very confused about String and StringBuffer or StringBuilder..


The difference between a String and the other two is that the String is immutable; you can't change the value of a String.

Yes, this concept can be quite confusing to beginners.  Just think of a reference as a "label" attached to the real object.

When you write:
the name variable is a label you attach to the String object, "Dana". That object will alway spell out "Dana" because String objects are immutable.

Now, if you write

You have taken the label name and attached it to a different String object, "Ucaed".  The "Dana" object is now unreferenced (there is no "label" in your program you can use to find it in memory) and therefore will soon cease to exist once the garbage collector finds it and sweeps it up, thus removing it permanently from memory. The point here is that the object doesn't change, only the reference is changed so that it points to a different object in memory.

In contrast, a StringBuffer and StringBuilder are mutable. You can change their contents. When you write

you are creating an object that can hold a sequence of characters. That sequence can be changed by calling various methods of the object like append() and insert(). When you write this:

you have changed the contents of the StringBuilder and the System.out.println() call will display whatever the object that the sb reference points to contains. When you add even more characters to the StringBuilder object,

this will display the new content of the StringBuilder object. The sb variable is still pointing to the same object in memory though.

Now let's say you want to start over from scratch and add more lines of code:

One line 8, you made your sb reference variable point to a different StringBuilder object. The object that you created on line 1 is now unreferenced and is basically lost and left floating around in memory until the garbage collector comes around to sweep it up.

Now we finally can get to where you're passing these things as parameters to methods. When a method is declared to have a reference type parameter, it will only take the reference value, not the object that is referenced. That is, it only takes a copy of the location in memory of the actual object. So, if you do anything to the parameter inside your method, you are only affecting that local reference.  

If the parameter references an immutable object, the same thing that went on before with String will happen; you won't be able to change the object, only the reference can be changed to point to a different object.

If the parameter references a mutable object, then whatever changes you make *to* that object *via* the reference will be visible from outside the method. Since parameters in Java are always copies of the values that were passed in, changes to a reference inside the method do not affect the references that were passed in to the method.

That is, if you have

on line 8, someBuilder points to the StringBuilder created on line 1. The value of the someBuilder reference is a *copy* of the value of the sb variable when it was passed to the addLastName() method on line 2.
When you append a value to the StringBuilder *via* the someBuilder reference, you are affecting the same object as what sb references.

On line 10, you change someBuilder to point to a different StringBuilder object.
So, on line 11 and 12, you are NOT using the same StringBuilder object that you were using on line 8 and certainly not the same object as what sb is still pointing at. That's why on line 12, you'd get a different output from what you get on line 3.
 
Junilu Lacar
Sheriff
Posts: 13479
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find that what I said about references vs pointers to be in conflict with what Henry wrote, that there are no pointers in Java.

Here's how to reconcile that: There are no "pointers" in Java that you can manipulate the way you manipulate pointers in languages like C and C++.

The name "reference" is just a way to highlight this difference. A reference is a pointer in that it points to where an object is located in memory. You cannot, however, manipulate this pointer in any way other than to assign it a different value which you can obtain only from creating a new object in memory or from another reference/pointer. This eliminates the kind of risks that are present in programming languages that support pointer arithmetic and manipulation.
 
Junilu Lacar
Sheriff
Posts: 13479
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the JLS says references are pointers: https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3.1

The authors of the JLS wrote:
The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.


 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You might find that what I said about references vs pointers to be in conflict with what Henry wrote, that there are no pointers in Java.

Here's how to reconcile that: There are no "pointers" in Java that you can manipulate the way you manipulate pointers in languages like C and C++.



Or stated in another way, Java references are the closest thing that it has to pointers.  So, when someone use the term "pointers" with Java, while it may irk some ranchers (probably mainly C++ developers) -- for the most part, we all know what you mean...

Henry
 
Ranch Hand
Posts: 186
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, we have references, these References are only for objects though. E.g.
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing I don't understand was:

Although String and StringBuilder are reference type , they are different behavior when I passed as parameters.

As Junilu Lacar wrote: StringBuilder are mutable.

Thank you Lacar for you explications with samples.

 
Junilu Lacar
Sheriff
Posts: 13479
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!  
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!