Jesse Custer

Ranch Hand
+ Follow
since Feb 07, 2007
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 Jesse Custer

The way an object is stored in memory differs from the way it is stored on a hard drive, or send across a network.
We need to format the data structure of the object as a sequence of bytes. We can then send this series of bytes across a network cable and reconstruct the Object on another computer.
You are mistaken,

The error will be thrown at first cast, it will never get to the second.
Howdy,

The class TestGC has a "Has A"-relationship with itself. So when you reach line 1, three objects have been created.

At line 1, we make the aref variable of a refer to b.
At line 2, we make the aref variable of b refer to c.
At line 3, we make the aref variable of c refer to a.

After line 3, there are still just 3 objects.

Then we refer c to a new instance of TestGC wich means there are now 4 objects created. None of these objects are eligible for Garbage Collection because the original c is still referenced by b.

At line 4 we make a and b refer to the same object as c. At this point 3 objects become eligible for Garbage Collection. Object a, b and the original c (wich was still referenced by b).
[ May 09, 2007: Message edited by: Jesse Custer ]
Hi Ganesha, If I have understood correct this is what you are trying to do:



You will get a NullpointerException when you run this code, but not because of line 2. The Exception is thrown at line 1 because you are trying to call the equals() method on a null object.


Hi Barry, why didn't i think of doing that . But it helped, thanks.

To Anil and Chandra. Thank you for your replies.
But I was well aware that my compareTo() method had a bad implementation. What I wanted to point out was that the objects would be handled as being equal because of the compareTo() method while they are not according to the equals() method.

It was just something I didn't expect. Imagine a class Person with an id and name. When I have 2 different persons with the same name, i'm unable to put them both in a TreeSet ordered on name, cause they will be considered as not unique.
[ May 07, 2007: Message edited by: Jesse Custer ]
According to K&B book page 542-543, sets determine if objects are equal by using the equals methods. And a TreeSet orders it's objects according to the compareTo() method.
Now I found out that if two objects are not equal according to equals method() but do have same ordering according to compareTo() they are NOT both inserted in the TreeSet. Not exactly what I was expecting. Can anyone elaborate about this.

The code you provided doesn't compile, please check your syntax before posting. This is the correct code with an added line so it's better to see what happens here. The regex you use is the same as "aaa".

I always think of StringBuilder and StringBuffer of holding data internally in a Collection of chars/bytes. That leads me to conclude it's answer A & B

A. Because we are using none synchronized methods and StringBuilder, multiple threads can simultaneously access the StringBuilders append() and toString () methods. This is not safe like Swarna Dasa pointed out.

B. Since the addMessage() and dumpMessage() methods contain only 1 line of code, replacing StringBuilder with StringBuffer will solve our thread-safety issue.

C. After synchronizing the addMessage() method, the dumpMessage() method will still be unsafe.

D. Bogus

E. Since we need to select 2 answers this could never be correct =)
Howdy Ansar,

thanks for this great example. It was a real eye opener for me. The call to show() in the super class A will always point to the implementation in the Subclass Trick. At wich point the variable a of class Trick still has default value 0.
I would never have expected that. This is even the case when class A is not abstract and has it's own implementation of show().
In other words, the next piece of code gives exactly the same result.
Indeed, using the FileOutputStream you can only write to the file. However, a file opened by a FileOutputStream, can still be used simultaneously by a FileInputStream to write data to the file. So it is possible to read and write to the file.

The reason why answer 4 is probably not correct is that it doesn't state it will create the file if it doesn't exist.
I don't follow anymore where you have problem understanding. I think the code example I provided has a logical flow similar to the java.util.regex classes. So if you understand that, then I don't see the problem.

The find() method will search the String until it finds a match. Then it stops searching and returns true. From then on we can call the start() method to get the starting index of the match.
If we call the find() method again, it will continue searching from where it stopped until it finds another match or until it reaches the end of the String.
If it reaches the end of the String without finding a match, it will return false and hence you can not call the start() method anymore because there is no match and thus no starting index.
[ April 17, 2007: Message edited by: Jesse Custer ]
Did you correctly paste the code?
The question states that both classes are in the same directory, so it makes no sense that they are in different packages.
If you remove the package declarations from the code then you'll see that answer 4 is indeed correct.
Decision about wich overridden method will be called is made at runtime. The compiler doesn't know that the variable of type Utils actually contains a Ping. So we are forced to handle the exception or declare it like done here.
If you are still unclear about this then check out the next piece of code. It's a simplified version of the Matcher and Pattern class.


[ April 17, 2007: Message edited by: Jesse Custer ]