• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.Object  RSS feed

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

iam having a doubt ,is that an instance of

java.lang.Object is serializable .i think it should be

serializable .But why in the api's it is not mentioned

that java.lang.Object implements serializable interface


regards
amir
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16044
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, java.lang.Object is not serializable. If it would have been, then the API documentation would have said so.

Why do you think that java.lang.Object is or should be serializable?
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Young

i thought java.lang.Object should be serializable. let me

explain you with an example. in servlets request.getParameter()

return an object of type java.lang.Object.only serializable

objects can be stored in session.if java.lang.Object is not serializable

then how can we get that from session or-else how can we set that into

session

regards
amir
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything in Java is an Object. If Object were serializable, then every object would be serializable, including all those that don't need to be (or indeed, can't be). Your example is far too narrow - Servlet only constitute a fraction of the programming done in Java.

The method you highlight returns Object, so whatever is returned must be an Object. However, it doesn't need to be an instance of the java.lang.Object class. It could be anything, including (for example) a String, which is serilizable.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes paul java.lang.Object is serializable

i have the proof for you i have done this test after

reading your reply
Object ob= new Object();
HttpSession session= req.getSession();
session.setAttribute("ob",ob);
System.out.println(" session "+session.getAttribute("ob"));

in the console i got session java.lang.Object@70beac00
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only "proof" that would be acceptable would be if you went to the Javadoc for java.lang.Object and looked at the class declaration, and saw that it said "implements Serializable." I'll wait here while you go check.

OK, you're back. Does it? No? OK, then. Object is not serializable. End of discussion.

As for your example, I can't begin to imagine what you think that proves. "setAttribute" is going to be implemented as a call to "put" on a Hashtable, nothing more, nothing less. Showing that you can store an Object in a Hashtable, and then retrieve and print the object, has nothing whatsoever to do with serializability.

Here's a test for you: open an ObjectOutputStream, and write an instance of java.lang.Object to it. I'll wait here while you go try it.

OK, you're back. Did it work? No? OK, then.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
Everything in Java is an Object. If Object were serializable, then every object would be serializable, including all those that don't need to be (or indeed, can't be). Your example is far too narrow - Servlet only constitute a fraction of the programming done in Java.

The method you highlight returns Object, so whatever is returned must be an Object. However, it doesn't need to be an instance of the java.lang.Object class. It could be anything, including (for example) a String, which is serilizable.


While I am a little amused with the OP's relentless pursuit in his proof of Object being serializable, methinks the OP has a small point -- That the methods he has difficulty with, have poor and (if this thread goes to show anything) grossly misleading signatures.

- Anand
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes hill

i tried what you said

i got an exception like

Exception in thread "main" java.io.NotSerializableException: java.lang.Object
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
at test.main(test.java:29)

but how an java.lang.Object can be added to a session

only serializable objects can be added to the session
[ January 27, 2007: Message edited by: Amirtharaj Chinnaraj ]
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the issue of confusion here is how can Object *not* be serializable, when a String is serializable? After all, isn't a string an object?

The fact is... an Object *would* be serializable, if it wasn't for the detail that it isn't declared as Serializable. There is nothing in the internal makeup of the base Object class that would cause it to serialize incorrectly. So... When a subclass, like the String class, is declared as Serializable and it serialized -- it's base Object class is also serialized correctly.

Henry
[ January 27, 2007: Message edited by: Henry Wong ]
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how an java.lang.Object can be added to a session

only serializable objects can be added to the session


There is a difference between adding an object to the session, and actually serializing that session. In your example, your new session will fail to work, if it is attempted to be serialized.

Henry
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amirtharaj Chinnaraj:
only serializable objects can be added to the session


You simply proved that that statement is false.

A true statement would be "only serializable objects *should* be added to a session".
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We persist sessions to the database for clustering and non-serializable objects in the session show that exception. Every time. We use an EAR produced by another team and they put something "bad" on the session so our logs are full of these stupid stack traces.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!