Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!

Julio Eneriz

Greenhorn
+ Follow
since Nov 28, 2007
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 Julio Eneriz

In the book it's said that you could do that but it's not a good design... so don't waste too much effort trying to learn bad designs (but I understand curiosity).

I guess you could do it like in the posted example, or you can put a void code in each class that doesn't really use the method, or you could make it final so children could never implemented it...
The second statement is false because you are comparing references, not the actual objects.

I think what happens in the first one is auto-unboxing, because if it was auto-boxing, you would have the same problem, so in order to compare, you unbox.

But I would have said both were false, so thanks for your example.
This is also my situation (only a week left!) and I have double check the confirmation email... it says:

Exam Name: Sun Certified Programmer for the Java 2 Platform, SE 5.0
Appointment Duration: 03:15 [hh:mm]

I thought that the extra time was for registration and printing of the score, but now I'm a bit worried...
Because it's sure that 3 and 4 will not happen ever in that try, but 1 and 2 may happen (you never know when, maybe in another thread).
I have this two classes:

class B extends A {
String doStuff(Long l) {
return "B, Long";
}
}

public class A {

String doStuff(long l) {
return "A, long";
}

public static void main(String[] args) {
A a = new B();
B b = new B();

System.out.println(a.doStuff(0l));
System.out.println(a.doStuff(new Long(0l)));
System.out.println(b.doStuff(0l));
System.out.println(b.doStuff(new Long(0l)));
}
}

And I was expecting this output:
A, long
B, Long
A, long
B, Long

but I get

A, long
A, long
A, long
B, Long

I know it's not inheritance, but the behavior of the autoboxing seems a bit weird... Is it just "look the reference type and if you can autobox, do it, if not, go to the object type and try to see if something matches"

Weirder is that if I add this method to B:
String doStuff(long l) {
return "B, long";
}

The output is:
B, long
B, long (why not B, Long!)
B, long
B, Long

Is it something to memorize or there is a more general rule?

Thanks
GC
This is the kind of exercise that is much easier to solve with a drawing.

The key is to remember that finalize may be call or not. So we are looking for a beginning of 4 printings with several possible endings (all combinations of 0 or 1 of each A1, A2 and A3).

The beginning of the output is well defined. a0 = a0.other() makes a0 point A1 and returns A1, which toString() method is invoked. Then the same A2, A3 and A1 again.

So every answer with a beginning A1A2A3A1 is valid if the end contains no more than one of A1, A2 or A3 in any order.

Does it make sense?

Originally posted by Deepak Jain:
So my question is since t1.finalize() invokes finalize() , How come JVM again invokes finalize()?



I think is the same trick thing that calling run() method in a thread. It's different behavior if you use it instead of the JVM.
If you had a non static class, the syntax would be

g.new NonS(); // ok

so the I think it should be
g.new S();// error here

but still it does not compile. I don't know why. It seems that you need to use the name of the class, not a variable.

Originally posted by Karen Marie:
Starting to get clearer. So is there ever a case where you could see

<E extends Anything>

as part of the return type?



<E extends Anything> is not a valid return type, is the definition of what is E. Once defined, you can use it, something like that

<E extends Anything> E getThat() {...}

Or



I cannot find any useful use for that. I think the use of generics in return type happens when you define E in the class (for example class Draw<E extends Clothes> and something like <E> getFirstItem ).

Maybe someone with more experiencie will do it.
From the Api (http://java.sun.com/j2se/1.5.0/docs/api/) Enum page


ordinal()
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).


So the behaviour depends on the ordinal I = 0, V = 1, X = 2 and so on, so L,C and M > 2.

What I'm not sure is the purpose of the code.
The problem now is that you are trying to use T without declaring it. Or, if Collection<T extends CharSequence> was a declaration (which is not), then the returning type is missing.

Besides, you cannot make a new Collection, because Collection is an interface.

I think what you are trying would be this:

public static <E extends CharSequence, T extends CharSequence> Collection<T> test(Collection<T> col){
return new ArrayList<T>();
}
[ November 30, 2007: Message edited by: Julio Eneriz ]
I think the problem is that you are defining twice E. This compiles:

public static <E extends CharSequence> Collection<E>
getLongWordsC(Collection<E> col){
return new ArrayList<E>();
}

The one with ? compiles because it's something different.

Originally posted by zartab ameen:
Yes you guessed it correct the question is how many reference variable are eligible for garbage collection.

I guessed it was 3 but the correct option is 0.

From K&B book page 204 when you are passing reference variable you are actually passing the bit pattern that means when you do some thing in a method the value will be also reflected outside the method,similarly when you assing null the other reference also becomes null.



Why 3? I should have gone for 2...

By the way, is there a way to test it? I'm trying to code as much as possible, but as the GC invocation is not guaranteed, I don't see a way to do it for this example.
GC
I think the only way to ensure it is safe from the gc is to make the "a" field static and public, or to make it private but accesible with a public static method.

That's what I understood about the singleton pattern
(http://en.wikipedia.org/wiki/Singleton_pattern)