David Samer

Ranch Hand
+ Follow
since Feb 08, 2012
David likes ...
Eclipse IDE Spring Opera
Merit badge: grant badges
For More
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 David Samer


I am confuse about one test quest from K&B book SCJP6

I don't get it after trying to draw it several times in order to see what are the objects I have, how many I have, which ones still keep references, and such.

Would it be b2 since b1 is static and keeps references in a1 and a2?

If anyone can do a little drawing it would be of great help to understand it.

I would appreciate any help , thank you!
Is it because no matter what kind of constructor class has, it will always have a call to super() implicitly?
Hello! .

A example in code:

Compiler says I need default constructor in parent class cause of the implicit call to super in the child's constructor. I thought , this would apply only when I am using the default constructor in the child but not with any defined constructor you define , when not defining the default one.
So, it doesn't matter what constructor I define in the child class because it always will have implicitly a call to super() ?

I simply don't understand the implicit call to super() in child constructor (being a non default one).

Thanks for the answers

Hello there Himai

Himai Minh wrote:

Is it possible to have two Chilis instance with different unique hashcodes, but they are the same objects? No. I don't see this from the code.

Is it possible? Wouldn't be the same instance due equals and inherited hasCode()?

Then, mainly because hashCode() is inherited from Object class, it means it fulfills its contract?

Thanks in advance
Hello, everyone, going to step in in order to ask for a doubt

Something I don't understand yet, as solution says :

E. As it stands, the Chilis class legally supports the equals() and hashCode() contracts.

Is this correct besides it has not been overriden hashCode()? (Just equals). Being a Map doesn't need for override both, equals() and hashCode()? . What am I missing? :S

Thanks in advance .
Greetings there Himai Minh ;) and Mala Gupta.

Both have given me the key to understand it, specially your example Mala . Thank you so much , crystal clear finally! ^_^

I am still not understanding it, besides I ran the code several times

Isn't supposed when comes to static methods , polymorphism doesn't apply? Isn't exactly the first line Joanne Neal says ? => a method is static then the type of the reference variable will decide what method is called.

Then why the output looks like if polymorphism is applied :S

(Edited due grammar )

Alexandre Leveille wrote:Hi!

1) 2L is a long and java won't automatically cast down a long to an int because there's a possible loss of precision (even though in this case, the number 2 can very well fit inside an int). What Java can do however is auto-box your long literal to a Long object. This Long object is holding the value 2, and match the arguments list for doNumber (Object dn).

Quoting Katty Sierra and Bert Bates, Chapter 3 Using Wrapper Classes and Boxing, page 249:

In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter.

2) int[]{1, 2 , 3 ,4} could match doNumber (Object dn): just comment doNumber (int... dn) and it will do so!

But the method doNumber (int... dn) is considered a more direct match. That is because var-args were created to address the problem that passing an arbitrary number of values required arrays.

With the arrival of var-args, many API methods that accepted a number of arguments via an array have been changed to use var-args.
Eg.: MessageFormat.format(String pattern, Object[] arguments) is now format(String pattern, Object... arguments).

Therefore, an Array can be used where a var-args is expected, but an arbitrary number of arguments cannot be used where an Array is expected, preserving the compatibility with legacy code and APIs.

3) new Integer[3] is instantiating an Array of Integers (new Integer[]{0,0,0}). It then matchs doNumber (Integer... dn) for the same reason int[]{1, 2 , 3 ,4} matches doNumber (int... dn).

oh I see, so in the case of 1) with "2L" argument being long, cannot be casted to int due lost of precision . Widening not happening then JVM goes for the autoboxing using wrapper class instead. That makes sense

Yet I am not having clearly enough the varg args arguments, looks like wildcard over the rest of choices.
In the particular case of doNumber (int... dn) , anything that is int , then how a new Integer[] {0,0,0,0} is an it? An array like that looks like Object type rather than "int" primitive type, having (holding inside) int primitives. I yet do not see why the array is an "int" rather than "Object" instead. The same goes for Integer[3] :/

Thank you for your answer Alexandre Leveille

Greetings there people.

I am hell confuse regarding one exam question from a well known simulator.

This is how the code looks like:

It prints the output:


I am having hard time understanding the calls to:

1) doNumber(2L); which calls last one doNumber(Object) , why? Im lost with "2L"

2) doNumber(new int[]{1,2,3,4}); which calls doNumber (int... dn) ( why not the doNumber(Object) ? )

3) doNumber(new Integer[3]); which calls doNumber (Integer... dn) . As far as I know Integer constructor admits String or an int as parameter so I get lost with the tricky square brackets [].

Thanks in advance.

Hello everyone.

I hope this link : https://coderanch.com/t/596424/java-programmer-SCJP/certification/Threads-Resolved-test , can answer the doubts (in which I also asked the same question )

Thank makes sense now. Thank you to the 3 of you , help much appreciated.
Hello everyone.

I am not having clear at all one of the answer regarding to a practice test from Kathy's and Bert's practice exams book, first question, page 57.

Here the question:

Which are true?
A) Compilation Fails.
B) An exception is thrown at runtime.
C) Bang will execute for a second or two.
D) Bang will execute for at least 10 minutes.
E) Thread t1 will almost certainly be the last thread to finish.
F) Thread t1 will almost certainly be the first thread to finish.
G) It's difficult to predict which thread will be the last to finish.

The correct answers are C and G. I have clear G one but not C. Which is the way to know such as assumption? . Thanks in advance.

Praveen Kumar M K wrote:The thread "id" AFAIK is just some autogenerated number uniquely identifying a thread. I think if you run your program again maybe you'll get a different output.

As I thought then. Thanks Praveen for all your help, it has been good and appreciated.