Francisco Gonzalez

Greenhorn
+ Follow
since Apr 11, 2005
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 Francisco Gonzalez

Today I cleared the SCJP 1.4 - 91%

Study materials:

- K&B book (must have)
- javaranch info/forum
- Took a lot of mock exams.
- I also had the Sybex's Java Study Guide by Roberts & Heller, but K&B has everything needed, so I didn't even used this one, only the mock it came with.

I took the exam once I started getting an average of 80%+ on every mock exam and of course 100% on the Rules round-up

SCBCD here I come !

Thanks,

Francisco
SCJP 1.4
17 years ago
super or this is the first line in every constructor, either implicitly or explicitly.

What that statement in K&B means is that no instance members is available until the super call to superclass has completed. In other words, until all the constructors in the inheritance tree have completed. Until then you will be able to use safely instance members in the constructors.

Look at this example:



If i is declared static, then it will be fine.

Regards,
Francisco
To add some more.

when you're implementing an interface, what you're basically doing is overriding the methods defined in that interface. So all overriding rules apply, which does not restrict you from use modifiers such as: synchronized and native even abstract if it's an abstract class that implements the interface.

You need to keep the same signature, the access modifier can't be more restrictive, do not throw any new or broader checked exception, etc,etc

Regards,
Francisco.
Actually you can try it and see

-Francisco
You could try this code that gives you one scenario:

Finally clause won't execute whenever there is a JVM shutdown. This can be when for example there is an OutOfMemoryError or when in the try block there is a System.exit(int) call.

Regards,
Francisco
This question is from danchisholm:



it's asked for the output of that program which is 31,0

Explanation:


The expression (-1 & 0x1f) is equal to (0xffffffff & 0x1f), and both are equal to the hex value 0x1f or decimal 31. The expression (8 << -1) is equivalent to (8 << 0xffffffff). If the left hand operand of a shift expression is of type int, then the right hand operand is implicitly masked with the value 0x1f. In other words, the expression (8 << -1) is equivalent to (8 << (-1 & 0x1f)). By replacing -1 with the hexadecimal representation we have (8 << (0xffffffff & 0x1f)). By evaluating the right hand operand we have (8 << 31). When 8 is shifted 31 bits to the left, the result is zero since the only non-zero bit is lost as it is shifted beyond the most significant bit of the int data type.



My doubt:

What does it mean that when the left-hand operand is of type int the right-hand operand is implicity masked with 0x1f ? Is it because the right-hand operand is a negative number ? What would happend if the left-hand operand is of type long, then what ?

Please, somebody explain when this implicit mask is performed.

Thanks,
Francisco
The answer for the first question cause confusion. All objects are created in method m1(), so after m1() returns all objects are ellegible for GC. But because you don't have such answer, the correct will be "None of the above".

Amit, island of isolation is ellegible for GC, and the first question is an example of it, they created one to make you think that after m1() finishes, each object would have a reference to the other and none would be ellegible for GC.

As far as the second question, the only object reference that you're preserving is i3 (C), because for the other two (i1 & i2) you changed the reference before calling m2() method to the same object of i3. So the original objects referenced by i1 & i2 were lost. And that would happend before m2 begins or after it finishes, does not make any different because it didn't do anything else with the object reference. However, all will be ellegible for GC after m1() completes.

Hope this explanation is clear enough.

Regards,
Francisco
Amit,

Singleton it is a design pattern that allows you restrict a class to have only 1 instance.

So in order for you to accomplish this you need to declare the constructor with a private access modifier, so nobody will be able to do a new on the class. But at the same time you will need to provide an static method that can be called and that return that only instance. Why an static method ? Well think a about it, if you cannot instantiate the class in the first place, how are you going to call a instance member ?

Look at this example:



With a class following that pattern nobody outside the class will be able to do a new Singleton() but instead needs to use the static method in order to get an instance.

Regards,
Francisco
Arun,

There is an implicit call to super() in Extension's constructor. Remember unless you explicitly insert a call to super or this as the first line of a constructor, the Compiler will insert a super() call automatically/implicitly.

So whenever you create an instance of Extension, Base's no-arg constructor will be call as the first thing. Also, there is another implicit call to Object no-arg constructor in Base's constructor.

There is a rule for inheritance that says that a Constructor's class will not complete until all of the constructor above of its inheritance tree has executed. It all starts at Object constructor down to the class been instantiated. e.g Object->Base->Extension

Regards,
Francisco
As far as I can tell, A and E are the correct answers.

B can't be right since b==c is the basic equals implementation in Object class, which Wrapper classes override for a more meaningful comparison.

C also wrong, because it breaks the API contract of equals being symmetric, where both b.equals(c) and c.equals(b) should return true.

D is also wrong because Wrapper classes return false for Objects different from its type, they don't throw a exception for that.

Regards,
Francisco
Thanks Steven for your response.

But there is something I don't understand, ~10 is really:

1111 1111 1111 1111 1111 1111 1111 1010

So in your calculation you are discarding all the 1's at the begining, I don't think you can't do that, or you can ? I know you ended up with the right answer, but is that always going to work ?

Regards,
Francisco
The following question is from K&B mock exam.



I know how all those bitwise operators work. But I'm having a hard time to manually perform the calculations, and for the exam, time is an enemy.

What's the best approach to attack a question like that ?

Thanks,
Francisco