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

Fabio Nascimento

+ Follow
since Nov 16, 2008
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Fabio Nascimento

The rules to remember are:

If x.equals(y)==true then x.hashCode()==y.hashCode() MUST be true;
If x.equals(y)==false then x.hashCode()==u.hashCode() MIGHT be true;

And these rules are applied on Collections that uses Hash Codes for fast searching, because they first search the bucket (hashCode) before search the object (equals), otherwise equals() have nothing to do with hashCode().

For instance:

They will be equals even with differente hashCodes, but they will not be found in a collection that uses hash codes for fast searching using its own implemented methods.
[ December 05, 2008: Message edited by: Fabio Nascimento ]
I think Sun wanted to give people the power to write non-serializable classes, what if someone doesnt want you to serialize its class? You have the power to mark it as final and doesnt mark as serializable.

Ok I dont explain you an advantage but its certainly a why
Local things happens between { } not between ( ).

I knew that you could use ( ) everywhere in Java, but that is ugly O_O
I didnt know about that ambiguous error, what a tricky for the exam heh
NavigableSet, NavigableMap and Console are the only difference between 5 and 6 and it will took you a few more hours to study them, so do the 6 man
The equals() method is the way to determine when two objects are considered equals based on real caracteristics that makes sense to the class writer.

When you compare objects, if they dont override equals, how can Java know if they are equal? Java can't guess it.

If you don't provide a way for Java to know if they are equal, Java will compare its references, and references are barely "memory address", which usually doesnt make sense in a real world. Thats what the Object equals() method do, compare the reference of two objects with "==".

For instance, the String equals() method was overriden, and for the String's Class writer, it makes sense to say that two String objects are equals if they have the same character sequence.

If you write a Client class, and for your business logic two Client's are equals when they have the same account number, you can override the equals() method in your class and return true when the objects account number are the same, which means "For the Client class, it makes sense to say that two instance of Clients objects are equals when they have the same account number".

Hope it helps.

After line 10

After line 10 you have one Aegis Object eligible for GC, after all you have 4 objects eligible for GC, three that you removed the references manually and one that become unreachable after that.
Yeah but, theres no overriden variables so you always access variables of your reference type and not of your object.

A a = new B(); // B extends A

Any access to "a.anyVariable" will reffer to the A class. Any access to "a.anyMethod" at compile time will reffer to the A class. But with methods the polymorphism get in action at runtime and if you have an overriden method in B, then in the runtime it will execute the B's method.

Got it?

To see that in action, declare an y variable in B class and try to access it with "a.y", you will get a "cannot find symbol" compile error, since the compiler will look for "y" in the "A" class (which is the reference type)
[ November 18, 2008: Message edited by: Fabio Nascimento ]
Just complementing the answers above, if you want to see the error compile your program with java -source 1.4 (or 1.3 or 1.2) ClassName and you will see the compile error.

Java 5 and above have autoboxing feature and no error will be shown.
It's up to you, if you went through a chapter that you are familiar with the subject and had no problems solving the exercices just skip it, but if you face problems solving the exercices or feel that you missed something, do the drill, its just two minutes.
You can access static members of a given class by its reference, this is kinda bizarre but the compiler just check the reference's type of the variable and switch it for you on the bytecode, for example:

What the compiler do is to check what type "a" is and then switch it for you. All the lower a's becomes upper A's in the bytecode. But be carefull, the compiler does it based on the reference type, not the objec. For instance:

Even your object is of B type, you are accessing the A members!


So the answer is 5 objects created and 2 eligible for GC. Now you ask "why do you said 2 objects eligile for GC and spoken only about one Dozen object?

Have you ever heard about the "Island of Isolation"? Its an unreachable place for people outside the island, but inside the island thing happens. What do I mean? Look:


da[1] = null


When you lose the reference to the Dozen object the int array (another object) is unreachable! It lives now in the Island of Isolation, the object itself still have a reference (the Dozen dz attribute) but since Dozen is unreachable you cant ever reach!

Thats why you have 2 eligible objects for GC, not only the objects that have no reference become eligible, but all the objects that cant be reachable (living in the island of isolation).

So be carefull, when you find an object that has no reference, check if the object attributes doesnt refer to another objects that become unreachable too!

Sorry for the long post, hope it helps you understand what happen!

[ November 16, 2008: Message edited by: Fabio Nascimento ]
[ November 16, 2008: Message edited by: Fabio Nascimento ]
You can pass a byte when you are expecting an int because bytes can be promoted to ints, but you cant pass an array of bytes when you expect an array of ints.
System.out.print(ga.i+" "+ga.getI());

First it execute the ga.getI() method overriden by the Arabik class which prints "Sub" and returns 2, then it prints 1 (ga.i) and 2 (returned by ga.getI()), so the final output is:

Sub1 2