Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1423 :
Posted: Sun Aug 20, 2017 1:55 pm
by Javier
Hi Admin!
Why the System.out.println(papers[1].id); is printing 2?
Why is not printing hashcode aswell?
What I see is that is accessing the id variable through the array.
Thank you so much for your help!!
Re: About Question enthuware.ocajp.i.v8.2.1423 :
Posted: Sun Aug 20, 2017 2:51 pm
by admin
why do expect hashcode to be printed ?
Re: About Question enthuware.ocajp.i.v8.2.1423 :
Posted: Mon Aug 21, 2017 10:40 am
by Javier
Because is printing one member variable of one element of the ARRAY?
Re: About Question enthuware.ocajp.i.v8.2.1423 :
Posted: Mon Aug 21, 2017 10:55 am
by admin
There are three print statements:
System.out.println(papers);
System.out.println(papers[1]);
System.out.println(papers[1].id);
The first line causes toString on the array object referred to by papers variable to be called. This method, which is implemented by java.lang.Object class creates a string as defined here:
https://docs.oracle.com/javase/8/docs/a ... toString--
The second line causes toString on the Paper object referred to by papers[1] reference to be called. Since, class Paper does not override toString, the one implemented by Object class is used. It generates a string as per the details given in link I mentioned earlier.
The third line tries to print papers[1].id. Since id is an int, there is no need to call toString on an int. The println method simple prints the int value contained in id. This is explained in the javadoc description of the println method :
https://docs.oracle.com/javase/8/docs/a ... intln-int-
So now, can you explain where else are you expecting hashcode to be printed and why?
Re: About Question enthuware.ocajp.i.v8.2.1423 :
Posted: Sat Jul 07, 2018 7:39 am
by __JJ__
It might be worth noting that there's an exception to this b/c SOP is overloaded to take a char[]; if so it prints it "normally". If you prepend or append a string to the char reference though, it resorts to the ugly representation of all the other arrays.
Code: Select all
int[] ia = {0,1,2};
System.out.println(ia);
char[] ca = {'0','1','2'};
System.out.println(ca);
System.out.println(">" + ca);
System.out.println(ca + "<" );
String[] sa = {"a","b","c"};
System.out.println(sa);
Integer[] Ia = {0,1,2};
System.out.println(Ia);
Character[] Ca = {'0','1','2'};
System.out.println(Ca);
OUTPUT:
[I@15db9742
012
>[C@6d06d69c
[C@6d06d69c<
[Ljava.lang.String;@7852e922
[Ljava.lang.Integer;@4e25154f
[Ljava.lang.Character;@70dea4e