Paul Lachance

Greenhorn
+ Follow
since Nov 27, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Paul Lachance

Hi Michael, does the book discuss the differences in the behaviors of different browsers? Different browsers seem to have very different css default values and even when the values are identical there are differences in behavior. IMHO this is the most frustrating aspects of html/css design...

regards
Paul
Also finalize will never run again so if the object is gc'd again finalize will not run.
There is a question on the K&B CD that I suspect has an incorrect solution...

The question is:

Given:
class SortOf {
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length() * bal);
}
public boolean equals(Object o){
// insert code here
}


Which of the following will fill the equals() and hashCode() contracts for this class?

A. return((SortOf)o).bal == this.bal;
B. return((SortOf)o).code.length() == this.code.length();
C. return((SortOf)o).code.length()*((SortOf)o).bal == this.code.length()*this.bal;
D. return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate == this.code.length()*this.bal*this.rate;

I selected C (which is correct) but the solution mentions that D is also correct. As far as I can tell D should NOT be correct since there are values of code.length(), bal and rate such that equal SortOf's could have different hashCodes!

for example, if one SortOf has code="blah" (length=4), bal=2 and rate=3, and another SortOf has code="da" (length=2), bal=3 and rate= 4, then the SortOf's are equal() but they have different hashCodes which violates the contract.
It is worthy to note that while a non void value will resolve to true when using the conversion char b in printf/format, the Boolean wrapper will set its value to false unless either a true (boolean value) or "true" (String) are used in the constructor argument...
for the purposes of the exam i would consider them keywords (they shouldn't be used as identifiers!)

if the add(Object o) section is uncommented the following error occurs:

TestGen.java:3: name clash: add(java.lang.Object) in TestGen and add(E) in java.util.HashSet<Dog> have the same erasure, yet neither overrides the other
public class TestGen extends HashSet<Dog> {
^
1 error


It seems like it is not permitted to create an overloaded instance of add() in this case... why not??? This seems to be related to the type errasure mechanism which I guess is not covered in the 1.5 exam?

( tags added)
[ January 15, 2007: Message edited by: Barry Gaunt ]
In K&B self test question 12 on p.617, the class declaration is as follows:

public class Group extends HashSet<Person> {...}

in the API for HashSet<E> there is an add(E o) method... doesn't that mean Group should inherit an add(Person o) method. As such, the add(Object o) method added to Group should be an overloaded add method. Based on the solution this appears not to be the case and the code does not compile on account of the add(Object o) method... any thoughts?
I'm not sure what purpose #1 could serve but it seems you do get a warning if you try to add anything to sampleList:

public static void main(String [] args) {
ArrayList sampleList = new ArrayList<Integer>(); // #1 - No warning
int x = 5;
sampleList.add(5);
}

GenTest3.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
sampleList.add(5);
^
1 warning
Isn't it also true that any exception thrown at runtime will also extend RuntimeException (whether progammer or JVM initiated)? I would think both A and B are implied...
Hi all,

The following is a test script for == and equals() behavior. It's based on an earlier post a found, I just added some cases and a note for each explaining the behavior. I would appreciate feedback on the comments. In particular I'm not sure about b.equals(e)...

Since equals() uses instanceof, I assume that only object type matters and reference variable type is not relevant?



public class TrickyBox {

public static void main(String [] args) {

Integer a = new Integer(12);
Integer b = 12;
int c = 12;
Object d = new Long(12);
Object e = 12;
Integer f = 128;
Object g = 128;

if (a == b) System.out.println("a == b"); // false (compares Integer objects only one of which was created by autoboxing so the object was not pooled (2 different objects))
if (a == c) System.out.println("a == c"); // true (so 'a' being unboxed)
if (a == d) System.out.println("a == d"); // false (Integer being compared with an Object (2 different objects))
if (a == e) System.out.println("a == e"); // false (")
if (b == c) System.out.println("b == c"); // true (so 'b' being unboxed)
if (b == d) System.out.println("b == d"); // false (object comparison with == (not same object so not == ))
if (b == e) System.out.println("b == e"); // true (so 'b' and 'e' refer to the same object... pooling occurs for values -128 to 127 based on object type and nor reference variable type))
if (c == d) System.out.println("c == d"); // false
if (c == e) System.out.println("c == e"); // true (so 'e' being unboxed)
if (d == e) System.out.println("d == e"); // false (different objects)
if (f == g) System.out.println("f == g"); // false (objects not pooled since above 127)
if (a.equals(b)) System.out.println("a and b are equals"); // true (equal values and both are Integers))
if (a.equals(c)) System.out.println("a and c are equals"); // true (c boxed)
if (a.equals(d)) System.out.println("a and d are equals"); // false (can't widen)
if (a.equals(e)) System.out.println("a and e are equals"); // true (e boxed)
if (b.equals(c)) System.out.println("b and c are equals"); // true (c boxed to Integer)
if (b.equals(d)) System.out.println("b and d are equals"); // false (can't widen)
if (b.equals(e)) System.out.println("b and e are equals"); // true (b and e boxed??)
// compile error since c is an int and has no equals method !
// if (c.equals(d)) System.out.println("c and d are equals");
// if (c.equals(e)) System.out.println("c and e are equals");
if (d.equals(e)) System.out.println("d and e are equals"); // false (can box e to Integer but can't widen to Long))
// compile error because you can't cast an Integer as a Long (not in the same hierarchy)
// if (a.intValue() == ((Long)b).longValue()) System.out.println("EQ ab");
if (a.intValue() == ((Long)d).longValue()) System.out.println("EQ ac"); // true
if (a.intValue() == ((Long)d).longValue()) System.out.println("EQ ac"); // true
if (new Integer(2).equals(new Integer(2))) System.out.println("equals new"); // true

}
}
[ January 07, 2007: Message edited by: Paul Lachance ]