Help coderanch get a
new server
by contributing to the fundraiser

Greenhorn
since Mar 02, 2008
For More
Cows and Likes
Cows
In last 30 days
0

## Recent posts by Ravi Yadav

I guess I missed the meaningfully equivalent part...
What does meaningfully equivalent means? who decides what's meaningfully equivalent? I thought the purpose of overridding equals() method was to take control on the factors that make two objects equal. In the case of A,B one might consider the two instances equal if the product of their fields are equal and not the individual fields. In such a case I cannot use the given hashcode right?.

15 years ago
Manju/Ankit,

Doesnt D break the contract? I though the contract says if two objects are equal, their hashcodes should be equal too.

In this case the two objects A, B are equal because the equal methods return true. As per the contract, the hashcodes should be true as well...

what am I missing here...
15 years ago
So the answere must be wrong...
15 years ago
This Q is from K&B masterExam CD:

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 fullfill the equals() and hashCode() contracts for
this class? (Choose all that apply)

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;

Can someone please explain how d is correct?

if we have two instances of the class SortOf A and B with the following parameters

A
------
code.length() = 5
bal = 4
rate = 2

B
--------
code.length() = 5
bal = 2
rate = 4

using option D would make the two instances A and B equal i.e 5*4*2 = 5*2*4
Where as their hashcode would be different (5*4) != (5*2)

THIS BREAKS THE HASCODE, EQUALS CONTRACT that if two objects are equal, their hashcodes have to be equal.

In case a case how can D be correct?
15 years ago
This is from CertSCJP study material...

11. public String makinStrings() {
12. String s = �Fred�;
13. s = s + �47�;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

Can someone tell me why the # of objects is 3?
15 years ago
Hi All,

I am sure we are closing in. I have a feeling that in the very next post my doubt will be cleared.

Taking the example above...
----------------------

int x = list.size(); >>>>>>>>>>>>>>>> gets lock on list object and calls size
int x = list.size(); >>>>>HOW CAN THREAD 2
IS CALLING THE SAME
INSTANCE OF LIST THAT THREAD 1
HOLDS THE LOCK TO???
list.remove(x-1);
list.remove(x-1);
...........................

15 years ago
Srilatha,

Interesting to note.. that's what exactly my first post says

.........
Please refer to K&B SCJP 5.0 page 714....
........

What I dont understand is if two methods are synchronized on the same lock

such as add() and remove() method of LIST as above.

Why do we have to synchronize the method that calls the LIST api's?
15 years ago
Ulf and Others,

That's my doubt - Arent list.size() and list.remove() both synchronized individually?
The threads T1 and T2 call the same instance of list so they lock on the same object of list as well.
Then why do we need to put size() and remove() inside another synchronized block like the one above?

synchronized(list){
list.size();
list.remove();
}

regards,
Ravi
15 years ago
oh so silly!!

Object obj = i.next();
System.out.println(obj.toString());

Ofcourse this would return the desired string since toString()is overwritten.
Probably I was thinking too much over it.

Thanks Marc.
15 years ago
Hi all,

This question is from SCJP K&B. cH-7

import java.util.TreeSet;
import java.util.Iterator;

1.class TreeSortTest{

2.public static void main(String arg[]){
3.new TreeSortTest().fun();
4.}
5.public void fun(){
6.TreeSet map = new TreeSet();
10.if(map instanceof TreeSet);

11.Iterator it = map.iterator();
12.while(it.hasNext()){
13.System.out.println(it.next()+" ");
}
}
}

Output is one three two
My question is in ln#13 since the return value of it.next() is not casted to String, the toString() function of Object should have been called.

If that is so, how does the toString() method of Object print the Strings 'one, three, two. Shouldnt it print 'objectname@<address>' ?

What am I missing here?

Regards,
Nilendu
15 years ago
SORRY GUYS....SOMEHOW THE ADD-REPLY BUTTON GETS CLICKED EVEN BEFORE I AM DONE WITH THE POST..

so, my question is both T1 and T2 call the same instance of List then
why wouldnt one of the two threads get blocked at names.size() call? How does both thread call names.size() at a time?

Regards,
Ravi
15 years ago
1. public class NameList{
2. private List names = Collection.synchronizedList(new LinkedList());
5. }
6. public String removeFirst(){
7. names.remove(0);
8. }
9.}

Calling class..
main{
NameList nl = new NameList();

}

T1.start();
T2.start();

K&B says - " The thing to realise here is that in a threadsafe class like the one returned by synchronizedlist each individual method is synchronized. So, names.size() and names.remove() is synchronized. But nothing prevents antoher thread from doing something else to the list n between those two calls.

I didnt get this concept here
two threads T1 and T2 both call
15 years ago
Please refer to K&B SCJP 5.0 page 714.

import java.util.*;
public class NameList{

}
15 years ago
Thanks for the clarification guys!
15 years ago
Hi,

I was playing around this these super keyword - I am unable to understand why the following piece of code doesnt compile.

import java.util.*;
class species{
}
class Animal extends species{
}
class Dog extends Animal{
}

public class testgeneric{

public static void main(String arg[]){
new testgeneric().fun();
}
public void fun(){

List<? super Animal> an = new ArrayList<species>();
}

}

I get an error -
testgeneric.java:24: cannot find symbol