About Question enthuware.ocpjp.v7.2.1112 :
Moderator: admin
-
- Posts: 17
- Joined: Tue Dec 18, 2012 7:54 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1112 :
This is a bit of a nit-pick, but it did confuse me and as it was a question near the end of the test, I was mentally tired:
I wasn't sure what was meant by "first line of code" as there were lines of code in the class description and in the invocation of that class. It seems obvious now as to what you meant, but if you could add //1 and //2 after the lines of code invoking the class, then there would be no chance of confusion.
I wasn't sure what was meant by "first line of code" as there were lines of code in the class description and in the invocation of that class. It seems obvious now as to what you meant, but if you could add //1 and //2 after the lines of code invoking the class, then there would be no chance of confusion.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
Sure, thank you for your suggestion. It has now been updated to make it clear.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Thu Dec 11, 2014 12:32 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
what is actual return value of hashCode() function of Object class.
class SpecialPicker<K> {
public K pickOne(K k1, K k2) {
return k1.hashCode() > k2.hashCode() ? k1 : k2;
}
}
public class MapEQ{
public static void main(String[] args) {
SpecialPicker<Integer> sp = new SpecialPicker<>(); //1
System.out.println(sp.pickOne(1, 2).intValue()+1); //2
}
}
From this program output it seems 1.
Am I right?
class SpecialPicker<K> {
public K pickOne(K k1, K k2) {
return k1.hashCode() > k2.hashCode() ? k1 : k2;
}
}
public class MapEQ{
public static void main(String[] args) {
SpecialPicker<Integer> sp = new SpecialPicker<>(); //1
System.out.println(sp.pickOne(1, 2).intValue()+1); //2
}
}
From this program output it seems 1.
Am I right?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
You can print out the actual value of the hash code for any object by just one line of code:
Code: Select all
System.out.println(objRef.hashCode());
//Here, objRef is the reference to any object.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
Clearly, as the answer of 2 is not provided, we know that hashcode of 1 must be less than hashcode of 2, but is this always the case with ints ie the higher value gives a higher hashcode?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
Yes, hashcode of an Integer object is same as its int value: http://docs.oracle.com/javase/7/docs/ap ... l#hashCode()
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
How does this work suppose if we do this..
SpecialPicker<String> sp = new SpecialPicker<>();
then
System.out.println(sp.pickOne(1, 2).intValue()+1);
would cause a problem since we are calling String.intValue()
At compile time compiler does not know about SpecialPicker<Integer> then how can it allow intValue() to be called ?
SpecialPicker<String> sp = new SpecialPicker<>();
then
System.out.println(sp.pickOne(1, 2).intValue()+1);
would cause a problem since we are calling String.intValue()
At compile time compiler does not know about SpecialPicker<Integer> then how can it allow intValue() to be called ?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
I am not sure if I understand your question correctly. If sp is declared as SpecialPicker<String>, then you can't call sp.pickOne(1, 2).intValue().
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
I mean does the compiler know that we declared sp as SpecialPicker<Integer> ?
Does it know about <Integer> at compile time ?
Does it know about <Integer> at compile time ?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
Yes, of course. Generics information given in the code is meant for the compiler.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
The Integer hashcode threw me off. I thought it returned a random number, like the hashcode for Double, or Boolean. I need to find better books.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
A valid hashCode() method would never return a random number for a simple reason that it has to return the same value if called multiple time.
Why do you think Double's or Boolean's hashCode() returns a random number? Check this out to understand what it returns:
https://docs.oracle.com/javase/7/docs/a ... l#hashCode()
Why do you think Double's or Boolean's hashCode() returns a random number? Check this out to understand what it returns:
https://docs.oracle.com/javase/7/docs/a ... l#hashCode()
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
Okay, so the hashCode for a Double (or any object) is not a random number (it just looks like it). According to the API, then, "The result is the exclusive OR of the two halves of the long integer bit representation, exactly as produced by the method doubleToLongBits(double), of the primitive double value represented by this Double object. That is, the hash code is the value of the expression:
(int)(v^(v>>>32))
where v is defined by:
long v = Double.doubleToLongBits(this.doubleValue()); "
I have no idea what this means and I don't want to know. My mind is cluttered enough as it is. I just hope on the exam I'm not supposed to know which Double hashcode has a greater value. As far as Object hashcodes, the API provides, "As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)"
So while the Object hashcode might also look random, it's not because it's tied to the internal address. So it's not random, unless assigning internal addresses is somehow random from a mere human's perspective. I think it's beyond the exam. Let's hope so. Thanks.
(int)(v^(v>>>32))
where v is defined by:
long v = Double.doubleToLongBits(this.doubleValue()); "
I have no idea what this means and I don't want to know. My mind is cluttered enough as it is. I just hope on the exam I'm not supposed to know which Double hashcode has a greater value. As far as Object hashcodes, the API provides, "As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)"
So while the Object hashcode might also look random, it's not because it's tied to the internal address. So it's not random, unless assigning internal addresses is somehow random from a mere human's perspective. I think it's beyond the exam. Let's hope so. Thanks.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
You don't need to know the hashcode value returned by Double or even Object. But you definitely need to understand why any hashCode implementation that returns a random number would be invalid. It must return the same value every time it is called on the same object.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1112 :
Yes, I got that latter part. It must return the same value.
Who is online
Users browsing this forum: No registered users and 42 guests