About Question enthuware.ocajp.i.v8.2.1423 :

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

Moderator: admin

Post Reply
Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1423 :

Post 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!!

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

Re: About Question enthuware.ocajp.i.v8.2.1423 :

Post by admin »

why do expect hashcode to be printed ?
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1423 :

Post by Javier »

Because is printing one member variable of one element of the ARRAY?

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

Re: About Question enthuware.ocajp.i.v8.2.1423 :

Post 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?
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1423 :

Post 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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 45 guests