Vinayagar Karpagam

Ranch Hand
+ Follow
since Apr 09, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Vinayagar Karpagam

Prateek,

The compiler won't widen and box simultaneously whether there is var-args or not. I believe the issue we were discussing here is not simultaneous widening and boxing. It is about having 2 var-args, one with boxing and the other with widening. And it is supported. But we need to be more specific in the caller to avoid the ambiguity.
Prateek,

If one cannot be used, there is no use in the compiler and run-time allowing it. If they both allow it, there must be a way to use it. Isn't it?
And we can use var-args with boxing and widening and both simultaneously, just that we got to be more specific in the caller so as to avoid ambiguity.

It would be great for yourself if you can find an appropriate way to call the methods. That way, you can conclude by yourself how it works.
Prateek,

There will be no impact of 1st run in the free memory during second run, simply because they are two different processes.

and is it happening that every time we run the program a fixed number of objects are created(thus a fixed memory is used) thus the free memory comes out to be same,is it the case?



Probably, yes.

Henry,

Can we consider the thread priority as a reason for gc() call to be ignored? In other words, since the garbage collection is supposed to run under a lower priority, can a gc() request get ignored due to a higher priority thread being active? Is there a documentation that covers the priority of the GC threads? Because typically in most of the heavy applications, the application runs out of free memory and has actively running higher priority threads. Won't that impact the GC from freeing the memory which is counter-productive?

Yes, even though it is not related to the current thread I've been staying as a watcher for quite some time. Just started posting replies.
In theory, the free memory can go down after a GC; a very common example being the presence of another thread that creates Objects in parallel with the thread that invoked the GC. In my opinion there is no final answer to the question. Only thing that can be said is that you cant conclude that GC ran and freed memory by just checking that the free memory goes up after GC.

1.why equals is been called and name=clovr compared wih clover on line 36 ?

>> Because all objects of Dog has the same hashCode(). Whenever an object is put into a hash map, we need to make sure that the object doesnt already exist. If it already exists, we need to replace the value with the new one. So, once the hashCode() matches, equals() will be called.

2.why equals is been called and name=clover compared wih clovr.i think it should return true as clover exsits in the bucket(created at line 34 an put in the hashmap on line 34) on the line 39 ?

>> The clovr exists first in the bucket. So the comparison is printed. Even though clover exists in the bucket, the equals() is never called due to an optimization in the HashMap.put() which doesnt call equals() if the references are same. Looking at HashMap.java source code will give further details. The equals() call was not required to confirm the equality.



3. why the object created new Dog("clover") is being compared to clovr on line 54.i think,complier should compare d1 with the being passed in the get function on line 54 and should return true.

>> true has been returned as part of line 54's get() call. Before that the clovr is compared due to the equality in hashCode().

4.why equals is been called and name=clover compared to clovr.As d1 exists in the bucket,it should straightaway ,display the value at key d1.The equals returns false then ,why is it displaying it's content as the output of line 50..The code is at line 50.

>> The compiler is never as intelligent as human. It will need to search all the objects in the bucket in linear order. As said for qn 2, "clovr" is at the start of the bucket. A key with a different value like "mohit" may probably sit later in the bucket and hence may avoid "equals false" comparisons. Sorry though if "mohit" also sits earlier.

5.why equals is been called clovr been compared with name=arhtur ?.The equals method returns false,then why is it output of line 61 giving contents at d1,it should give null as output ?

>> Please refer to answers for questions 2 and 4.

Regards,
Vinayagar
Sorry, its my eclipse settings that made the warnings displayed as error :-)
It compiles fine with a javac.
Yes, I tried. The following code doesn't compile.

Nitish Bangera wrote
This code compiles properly n outputs Dog. Now i am asking, what did the compiler see to compile it without errors?



Are you sure the code compiles? As far as I believe the compiler wouldn't allow the call when reference type is not parent type.
But B() internally calls A(). It is not the case with a overridden method.
May be I can agree with "sort of overridden". But it is not "exactly" overriding.

The run-time polymorphism (overriding) happens when we call a method on an object
and the method to be called is decided based on the run-time type.
But the constructor creates an object, a step before the run-time polymorphism.

Regards,
Vinayagar. K
We always claim that if a class extends another class,
all the inherited methods of the parent class will be accessible on a child class object.

But in this case, we have two methods which are not allowed to be kept as overloads in a class.
Then the sub-class method will override the parent class method.
This means that our sub-class Dog can have only one method, the overriding method (method(String[])).

And the compiler allows calls to both the overridden method and the overriding method. But at run-time,
it is always the overriding method that is being called.

Ankit Garg wrote:
But you won't try to serialize an instance of FileWriter or FileReader. So a no-arg constructor is pointless for such a class...



Absolutely.. by its name itself, a bean represents a value holder and FileReader/Writer clearly wouldn't be classified as a bean.
So, I think the "best" practice of adding an empty constructor wouldn't apply for such classes.
I'm also not well aware of why putting a default constructor for a bean is a good practice. And I dont know if one such standard exists.
But in some real-time scenarios, a default constructor is absolutely necessary.

For e.g., in case there is a Serializable class which extends a non-serializable class,
the non-serializable parent class must have an empty constructor without which a run-time exception will be thrown during the de-serialization.

But if there is one such practice defined as a standard, it could well relate to the above scenario
since as a bean, it is better to be serializable than not.
Hi Karthick,

Thanks for the interesting question..

As you would be already aware, the methods method(String...) and method(String[]) are treated internally as same (even overloading is not allowed).
But since the compiler can allow method(String...) to be called as method(), it compiles.
And the call is considered as a over-ride during the run-time as well.

Swapping the methods in the super and sub-classes would give us a fair idea on what's happening. The code doesn't even compile.

The program in fact takes advantage of both the compile-time and run-time polymorphisms.



Regards,
Vinayagar. K
I hope adding -ea as VM arg in the launch configuration and setting the source code level to >= 1.4 should be enough to enable assertions.