Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Mahesh Murugaiyan

+ Follow
since Jun 25, 2009
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 Mahesh Murugaiyan

Thanks Ninad. Thanks Paul, yes, thats makes total sense. Other than enum, interface(constants) are there any other entitiy that is implicitly static ? I will add this to my study notes.

This is Question #3 in practice exam 2 in Bates/Sierra's Practice Exam book.
Numinor is a stand alone class with the "Members" enum. How can this enum(which is a non-static member of Numinor) accessed without creating an object for Numinor class?


Question(s) on generic typing with polymorphism. The ceritification book says

public void foo(List<?> list) { }
public void foo(List<Object> list) { }

If there IS a difference (and we're not yet saying there is), what is it?

There IS a huge difference. List<?>, which is the wildcard <?> without the keywords extends or super, simply means "any type." So that means any type of List can be assigned to the argument. That could be a List of <Dog>, <Integer>, <JButton>, <Socket>, whatever. And using the wildcard alone, without the keyword super (followed by a type), means that you cannot ADD anything to the list referred to as List<?>.

Q1: If i cant add anything to List<?> what is the point of allowing this?

Consider this example:

Q2: Why im not able to add a Animal object to List<? extends Animal> animals2? doesnt it mean "allow all object that are of type Animal or extends Animal"? it doesnt allow me to add both Animal and Dog.

I have used Generics in Collections since 1.5 for the past 5 yrs or so. Still, this behaviour is puzzling me. Ref materials are not helping too much either. hence the long post!

Thanks in advance.
Got it. I did a map.get(new ..) and the response was null. Did the hashCode, equals and now everything is cool. Thanks!!!

In the code given below, i did not write a overridden hashCode(). Then how is it finding the object back properly?

Thank you Dennis and Fred!!!
I came across this question while taking the certpal test.

I get a error on the "pant = new int[5]"; which is, "Type mismatch: cannot convert from int[] to int[][]" I dont understand why i am getting this error. pant is being initialized as a single dimensional array. Why the compiler sees it otherways?

hello. im preparing for SCJP 1.6 and came across these two questions which confuses me a bit. i tried these out and they work as mentioned in the book, still i need some clarifications:

Question 1:

2. Question 2:

- I read that all numerical literals are int by default. so in the first example, the int 200 is narrowed down to short and then boxed into Short. (is it also because 200 can fit in short easily?)

- then, why cant the 7 which can fit in the short is being accepted as a parameter? while short story = 200; works, why cant an int be passed as a parameter?

I agree to all the points of Ritchie. You have to think about re-structuring the logic.

- The reason why your tester class did not ask to continue is, say if i did not pick the guess first time, the guess returns a message say, "Too Low". the while loop in #22 checks if the gameOne.isGameOver is true. because i had the wrong guess, it isn't. so the flow doesn't continue into the loop and the code exits.

- Why do you need a loop in the guess?
- dont you think having just one variable currentDiff would take the guesser closer to the number? Comparing the currentDiff with the previous differential may miss lead some time.

- To start with, I have re-structured just the tester class in a way to make it readable and modular. See the tester class below: I have moved the logic in main to a playGame method. To facilitate this you have to change the GuessingGame.guess to return a boolean instead of String. you can simply print the messages in the guess method itself. and work on the Guess logic.

The code above is not a perfect solution yet. Atleast it will get you into the second and further attempts which wasn't working for you before.
8 years ago
When a class containing the main method should be public to run along with the (public)class name should be the name of the .java file, why it isnt the same case for enum? Below code runs just fine?! Should the compiler complain about the name of the enum(CoffeeSize) not being same as name of the .java file ( ?

file name

8 years ago
Hi Kumar,

Its interesting to see why removeRange is marked as protected. I will watch this thread to see if some one explains.

For the UnsupportedOperationException, getNextSet() should work properly.
How are you passing the list into this method? is that list obtained using Collections.unmodifiablelist ?

- Mahesh
8 years ago
Hi Brian,

You can add a explicit type cast which will covert the string into a "double" or just do a scanDouble().

- Mahesh
8 years ago

That's not completely true and what actually happens is a good demonstration of why the Calendar class is implemented as a factory. If you look at the source code for the Calendar.getInstance() method you will see it calls the createCalendar method and that decides what type of Calendar subclass to return based on the locale. In other words you can use the same code wherever your program is run and an instance of the appropriate class will be returned

Hello, The calendar.getInstance, if no locale is passed, and if the locale is not Japan, by default returns a Gregorian Calendar. Have a look at the code snippet and its output below:

The above code outputs:


The Calendar.getInstance() indeed works as you said. Gets the Locale, and returns a specific subclass implementation based on Locale.

The getInstance() method abstracts all this locale checking and subclass creation from the end user which is the objective of a factory method!

Hi Glen,

Calendar, DateFormat, NumberFormat are all Abstract classes.

In Java terms, we cannot instantiate any abstract class. Though they serve specific purpose, they are better when refined further by their sub classes. The subclasses of these classes add more functionality. For example, the Calendar.getInstance would give you a Gregorian Calendar, DateFormat.getInstance would give you a SimpleDateFormat.

Both GregorianCalendar, SimpleDateFormat extends their abstract class, and provide more value add.

About the factory method:

1. A Factory Method is a design pattern. Simply put, a factory method will abstract all the complexities involved in instantiating and initializing a object. In this case, while using Calendar.getInstance, we have no idea about how the object was created and presented back to you! you don't know what are the arguments passed, what default value to set etc.

2. Apart from just hiding the complexities, another cool aspect is, if the same object needs to be created in multiple places, then you can avoid having the initialization code every where. Its more clearer than having a private method constructing the object. By moving this piece of code into its own class, your class can worry about the actual business than about creating/initializing bits and pieces.