[HD Pg 9, Sec. 1.4.1 - relation-between-a-class-an-object-and-a-reference]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ikos1989
Posts: 1
Joined: Wed Jan 30, 2019 12:22 am
Contact:

[HD Pg 9, Sec. 1.4.1 - relation-between-a-class-an-object-and-a-reference]

Post by ikos1989 »

Here is an extract:
"Another important difference is that you cannot make a reference variable point to a memory location directly. For example, you can set the int variable to 2250 but you can't do that to str i.e. you can't do str = 2250. It will not compile. You can set str to another string and if that new string resides at a memory location 2250, str will indeed contain 2250 but you can't just store the address of any memory location yourself in any reference variable."

Considering the information provided, it is possible to set any primitive to point to a particular memory address (2250 in this case). If we do "int n = 2200; n = 2250;" it will set the value of the variable "n" to "2250" (memory cell contents), but it will not change its address, will it? So, is it possible to set a primitive to point to a particular memory address (w/o using sun.misc.Unsafe class)?

Could you please help me to figure it out?
Thank you!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 9, Sec. 1.4.1 - relation-between-a-class-an-object-and-a-reference]

Post by admin »

You can set a primitive variable to contain any value (as long as the value fits in the variable type's size, of course) but the point that paragraph is trying to make is that the value contained in a primitive variable is not considered as a memory address. It is considered as just a value. The JVM does not use this value to go a memory location.

So no, it is not possible to point to any memory address because primitives never point to a memory address at all. Reference variables do.

It is like saying - A has 2250 bucks while B has an bank locker number 2250. So, A has the actual money while B has an address of a location containing money. A is a primitive and B is a reference. When you say A = 3000, you are saying that A now has 3000 bucks. But if you say B = 3000 (which you cannot do in Java), then you mean to say that B now points to some other locker.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Janissa
Posts: 1
Joined: Mon Jul 25, 2022 6:14 pm
Contact:

Re: [HD Pg 9, Sec. 1.4.1 - relation-between-a-class-an-object-and-a-reference]

Post by Janissa »

in the book it is mentioned that String str = "hello" contains the memory address 2222 where hello is stored. but while printing it prints hello and not 2222. however when I print some user-defined object then it gives a value like name@57723656 like this. we have to define toString() method to print actual values. I got confused. Can someone clarify here?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 9, Sec. 1.4.1 - relation-between-a-class-an-object-and-a-reference]

Post by admin »

1. When you try to do anything with a reference variable, the JVM interprets the value stored in that variable (i.e. 2222 in this case) as an address to the memory location where that object is located. The JVM then performs that operation on that object. It doesn't do anything else with the memory address itself.

2. Now, when you try to print an object (such as a String object), you call System.out.println(str). Essentially, you are passing the address 2222 to the println method. The println method then invokes the toString method on that object (which is located at memory address 2222). The println method then prints whatever is returned by the toString method.

3. The String class defines the toString method to return the contents of that string object, that is why the println method is able to print "hello" to the console. In case of other user-defined objects, the user needs to define a toString method to return a more meaningful value. If the user does not define the toString method, then the JVM uses a default implementation of toString method that is present in the java.lang.Object class. This implementation returns a string composed of the classname and some other value (hashcode, but that is not important right now). That is why you see weird values such as name@323423423 being printed.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 71 guests