Win a copy of Terraform in Action this week in the Cloud forum!

Ankit Garg

Sheriff
+ Follow
since Aug 03, 2008
Ankit likes ...
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
Bangalore, India
Cows and Likes
Cows
Total received
42
In last 30 days
0
Total given
20
Likes
Total received
309
Received in last 30 days
0
Total given
245
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ankit Garg

Hi Priyanka,

So in both cases, the problem is that the Comparator you are using is causing this confusion. So the max method takes a Comparator and uses that to compare each object with the other. The comparator is supposed to compare the two numbers, return a positive value is the first one is bigger or a negative value if the second one is bigger. So something like this:


But your comparator always returns the biggest value, so whenever it compares two numbers, it will always thinks the first number is bigger. So it will do the following:

Arrays.asList(3,4,6,9,2,5,7);
1. Compare 3 to 4, comparator will return 4, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
2. Compare 3 to 6, comparator will return 6, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
3. Compare 3 to 9, comparator will return 9, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
4. Compare 3 to 2, comparator will return 3, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
5. Compare 3 to 5, comparator will return 5, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
6. Compare 3 to 7, comparator will return 7, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.

So due to the comparator you are passing, the stream thinks the first number is the biggest.
What was the output when you ran the code, and do you have any doubts about why the output was what it was?
Strange, for me even if I remove the body of SUMMER, the code doesn't compile, I get a different error:

Season is abstract; cannot be instantiated

use this block for both SUMMER and FALL because I want the same implementation for both of them.


Is this some new syntax rule that I'm not aware of?
Hi Moe,

Are you sure the code compiles without the printHours in any of the enum constants? If I try to remove one of the methods I get this error

<br /> Season> is not abstract and does not override abstract method printHours() in Season.WINTER
My personal experience with SCJP was that I got like 4-5 questions wrong initially. Fortunately I had time to review all answers, and I corrected all of them (at least I think I did, I still got 1 question wrong in the end ).

Kertes Gray wrote:The answer is the moment you put a collection with a reference bound(upper-bound), it is now immutable.


Hi Kertes, this is not entirely true though. The collection is mutable, you can sort it, clear it, delete elements at certain indexes from it. You just cannot call add method on it as the parameter <? extends Bird> won't take any type of object as a valid argument. The only way to call add on a List<? extends Bird> is to do something like this add(new <? extends Bird>()) which obviously is not a valid syntax
To add to Jesse's excellent explanation, the purpose of such a list is to get objects from it and perform operations on them which all birds can do. So for example


So as you can see, you can loop over the List<? extends Bird> and feed all the birds.

It is not that the List<? extends Bird> is immutable, but no parameter you pass to the add method will be allowed by compiler. The main difference lies in the declaration of the add and get methods.

For a List<? extends Bird> the get method returns <? extends Bird> you don't know exactly what it returns but it will be either Bird or Nightingale or some other sub-class of Bird so you can always be sure it is assignable to Bird. <br /> But for the add method, any parameter you pass won't be allowed as <? extends Bird> doesn't tell the compiler exactly what the type <T> is. So whether you try to pass a Bird object or a Nightingale to the add method, nothing is <? extends Bird> so the compiler cannot allow you to pass any of them to the add method. <br /> Thus although it might look like a List<? extends Bird> is immutable, but actually the problem is the there is no valid parameter to the add method. You can for example call List.clear method to clear a List<? extends Bird> so it is not immutable.
Hi Priyanka,

One of the key is to understand here is that once a variable is assigned, the compiler doesn't keep track of what value was assigned to it. All it cares about is the type of the variable.


On the throw statement the compiler doesn't know whether ex was assigned null or something else. All it knows is that ex is of type Exception and that you need to handle it.

There are many ways to confirm this behavior, for example the following code:


In the above code when you try to assign sup which is of type Super to sub which is of type Sub (which inherits from Super), the compiler won't allow it. Even though you know the assignment will work (as sup contains an object of type Sub), but when compiler sees the statement Sub sub = sup; all it knows is sup is of type Super and Super is-not-a Sub.
I had a similar delimma when I did SCJP, I started preparation with SCJP for Java 1.4 book (not realizing there was SCJP for Java 1.5), then bought and went through the 1.5 book, but eventually gave SCJP 1.6 exam (the 1.5 and 1.6 exams were supposed to be almost the same). Between Java 1.4 and 1.5 there were a lot of changes, so I was glad I took the latest exam.
I do see this in the Exam objectives/topics, so I would assume it is covered:

Create worker threads using Runnable and Callable, and manage concurrency using an ExecutorService and java.util.concurrent API

Hi Jeremy, welcome to the ranch.

It really depends on how much time you can give to study for the exam, and how much practice you want to do. Since you mentioned you can spend a couple of hours study daily, I guess you'll be able to complete the book in a couple of months. Then you can do lots of mock exams. I studied for about 5-6 months for my own SCJP certification (1-2 hours study a day), including doing mocks for almost 2 months.
I can't find the Table 1.1 you are referring to, but if you think about it, although anonymous inner classes are implicitly final, there is no valid syntax to mark it final. If you do this


You are declaring the variable final not the anonymous inner class.
I can probably annotate each line if that helps:

The question says that "For simplicity, let's give each module the same name as the package". The question doesn't say that the module name is magic.helper. I guess instead of making it simpler, the question made it more confusing
Hi Shoeb, welcome to the Ranch.

Did you try to contact your credit card provider?