This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Sunny Mattas

Ranch Hand
+ Follow
since Apr 22, 2008
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 Sunny Mattas

Hello All,
In our spring web application we are getting AJAX JSON request and I have to do the request mapping in controller.
Following is the structure of my request form data sent to the server.


Following is the request mapping I have done in the controller.






But I am getting the following error in the browser

415 Unsupported Media Type

Please let me know what is wrong with my request mapping. Thanks.

Regards,
7 years ago


Some thing more to add.
This is because anything which is non static belongs to a object.Static methods like main doesn't belong to any object, so if we call non static method or variables directly from static context then compiler will not be able to know which object we are using and will give error.


Hi,

You can override the protected finalize() method present in Object class. decrement the static count in this method.
It is called whenever any object is garbage collected by JVM.



Hope this works...
hi
For me also both examples are not compiling.
And i think it has nothing to do with Integer class because following code is also not compiling.



But this is compiling with a warning

Hi Sergio
whenever I see these IS-A questions I divide it into two parts compile time and runtime.

1.Compile Time
At compile time we should remember compiler does not have any information about real object.Thing it cares about is references.

Now if we write


if we call a method using reference variable a then compiler will look for it in class A only because compiler simply sees reference variable a as type of Class A it has no knowledge about real object B.

2.Runtime
Now at runtime real object is created means JVM knows that B is real object.If any method is called useing reference variable a then JVM will first check the class B. If method is not found here then JVM will look for it in class A.

For casting check here

I hope this helps you.



Hi,

Java Serialization takes care of saving that object's entire "object graph"



This means the object we are trying to serialize can have reference variables pointing to more objects.And all these objects will be serialized.Like if we serialize a Dog object, the Collar will be serialized automatically.
But this will only happen if all the objects have Serializable interface implemented to their classes.If this is not the case then NotSerializableException will be thrown.
NotSerializableException is occurring in the example where Collar class is not implementing the Serializable interface.



See in above code class B implements Serializable interface but its parent class A is not , so non-serializable class A constructor will run during deserialization.

I hope it helps


Hi,

why C is invalid


if we change the code and put option c there then we have



Now at 1 sheep implements Herbivore interface with type Plant.This is correct.But see Herbivore interface is also extending Hungry interface and method munch in Hungry becomes


Now this is the problem,Munch method in class Sheep has Grass as its argument,so method munch of interface Hungry is not correctly implemented here.

why D is wrong



Now if we put option D in the code then we have


Class Wolf is implementing Carnivore<Sheep>,problem here is with generic type sheep.Carnivore interface has generic type <E extends Animal> i.e. E should be sub type of Animal.But in above code it is clear that Sheep is extending Plant not Animal.So this code will give compiler error.

I hope this helps you.

Sunny Mattas
SCJP5


Here two objects will be created at line 1 and 2. Object at 1 will be created at compile time an the string memory pool.And objects present in String memory pool doesnot get garbage collected.

At 2 we are using new keyword so, object will be created only at heap by JVM and "someString" is already there in pool because of line 1 and compiler will not create it again.This will save the memory and is the main use of string memory pool.
So at 3 three object referred by reference variable two is only available for garbage collection.

If I am right garbage collection for strings is not in scope of SCJP.

Sunny Mattas
SCJP5

but in K&S answer is c


hi
There is a mistake with this question in K&S.
Book says second object garbage collected is


But this will not garbage collected because value of short is under 127 which means it is in cache memory.
If value is changed to


then two objects will be garbage collected.

Regards
Sunny Mattas
SCJP5


When i print values of class A's instance variables those are same when object is created.



Object you want to serialize should belong to class which implements Serializable interface.Class A in your code is not implementing the marker interface Serializable .
In fact I think your code should throw runtime exception because you are trying to serialize class A.

Regards




This line will compile but will throw exception at run time.Cast in this code tells compiler let me assign parent class object dog to its child class reference Beagle b2 and every thing will be OK at runtime but then at runtime we have dog object being referenced to child class Beagle.We get a exception.
Yes but it will compile thanks to the cast used.


This code will compile and there will be no runtime exception as actual object is new Beagle() created at line 6.So this time cast has kept its promise to the compiler.


This will not compile, as compiler has no knowledge about real object. It sees reference variable dog2 of type Dog and tells us parent class object cannot be referenced by child class reference variable b4.Note here is no promise to compiler using any cast.

I hope not missed anything.....

Regards
Sunny Mattas
SCJP5




No x is the instance variable declared at line 2. Above its value is getting changed for object refrenced by z,x,f1 and later by f3.

Regards
Sunny Mattas
SCJP5

Only two objects are created



No, I think three objects are created.



At compile time compiler finds string literal "abc", it puts this object in to the string memory pool.This is the first object created.Then Compiler again finds "abc" in line 3 but there is already an "abc" object in string memory pool so it is not put there.This is the use of String memory pool to save the memory. Then at run time two StringBuffer objects are created at line 1 and 3.
Thus there are total of three objects created.

Regards
Sunny mattas
SCJP5