About Question enthuware.ocajp.i.v7.2.1252 :
Moderator: admin
-
- Posts: 5
- Joined: Wed Sep 02, 2015 7:21 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1252 :
Hello
My question is, I thought once you try to change the value of the local variable itself, e.g s++ or s = a, the reference starts pointing to a new object. So how come in this question, cA[1] in m1() and cA[1] in m2() are still pointing to the same object when the following occurred in m2(): cA[1] = cA[0] = 'm';
My question is, I thought once you try to change the value of the local variable itself, e.g s++ or s = a, the reference starts pointing to a new object. So how come in this question, cA[1] in m1() and cA[1] in m2() are still pointing to the same object when the following occurred in m2(): cA[1] = cA[0] = 'm';
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
What you say is true but there is an important point that you are missing.Tariee wrote:Hello
My question is, I thought once you try to change the value of the local variable itself, e.g s++ or s = a, the reference starts pointing to a new object. So how come in this question, cA[1] in m1() and cA[1] in m2() are still pointing to the same object when the following occurred in m2(): cA[1] = cA[0] = 'm';
cA is a reference to an array object. But the elements of that array are not references to other objects because this is an array of chars, which is a primitive. Therefore, cA[1] and cA[0] are not references. They are merely values. That is why, when you change cA[0], you are changing its value right there.
You should try running the same code after changing char arrays to Object arrays and then see what happens.
You may want to go through this article that explains the basic concept: http://www.javaranch.com/campfire/StoryPassBy.jsp
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed Sep 02, 2015 7:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
Thank you so much, its very clear now
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
It is resolved.
Sorry.
Sorry.
-
- Posts: 4
- Joined: Thu Mar 02, 2017 12:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
The explanation to this answer states that "So instance member 'c' keeps its default (i.e. 0) value." But it is not true - the default char value is not 0 but '/0000', this is, null. Here you get zero, because the char is cast to int.
The following snippet prints nothing:
But the following prints zero:
The following snippet prints nothing:
Code: Select all
public class Demo {
char ch;
public static void main(String[] args) {
System.out.print(new Demo().ch);
}
}
Code: Select all
public class Demo {
char ch;
public static void main(String[] args) {
System.out.print((int)new Demo().ch);
}
}
Last edited by mcberenguer on Sat Mar 04, 2017 4:58 pm, edited 1 time in total.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
char is an integral type just like byte, short, and int. Its default value is 0. '/0000' is just another way to write it Java code in character format. Internally, it stores 0. There is no difference between the two.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Thu Mar 02, 2017 12:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
But the point is that the code is printing "0,m" because the char is cast to int, not because c keeps its default value. Without the cast, the code prints ",m". There may not be difference between the two internally, but when printed out the output is different.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
That is what the explanation to option 3 says as well, "Because of the explicit cast to int in the println() call, c will be printed as 0."
So yes, the cast to int is important and the fact it keeps its default value 0 is equally important here.
-Paul.
So yes, the cast to int is important and the fact it keeps its default value 0 is equally important here.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
Code: Select all
System.out.println( ( (int)c) + ", " + cA[1] );
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
When there is no explicit object reference, an implicit reference "this" is supplied by the compiler for instance variable. So "c" is actually "this.c". You should read about this reference here:
http://stackoverflow.com/questions/3728 ... is-in-java
http://stackoverflow.com/questions/3728 ... is-in-java
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Wed May 10, 2017 2:49 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
Hi,
I just want to know is arrays the only one will be pass by reference?
Regards,
Shamran.
I just want to know is arrays the only one will be pass by reference?
Regards,
Shamran.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
No, in Java, everything is passed by value. But in case of objects (any object, including arrays) it is the value of the reference that is passed by value. It is a very important topic so I will suggest you to go through a good book to understand it. Here are a few good links -
http://javadude.com/articles/passbyvalue.htm
and
http://stackoverflow.com/questions/4048 ... s-by-value
http://javadude.com/articles/passbyvalue.htm
and
http://stackoverflow.com/questions/4048 ... s-by-value
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
And why does the compiler not put an implicit this before the c in m2()
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1252 :
Because a variable named c is already in scope. The compiler tries using "this" (or the classname to use the static variable) only if it is not able to resolve the given variable. Here, it is already able to resolve c as a method parameter. So there is no need to use "this".horst1a wrote:And why does the compiler not put an implicit this before the c in m2()
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 50 guests