wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object I/O" Watch "object I/O" New topic
Author

object I/O

hai zhang
Greenhorn

Joined: Oct 30, 2001
Posts: 11
What is the result of compiling and executing the following code fragment:
Integer a=new Integer(5);
Integer[] numbers=new Integer[]{a,a};
java.io.ByteArrayOutputStream bOut=new java.io.ByteArrayOutputStream();
try{
java.io.ObjectOutputStream out=new java.io.ObjectOutputStream(bOut);
out.writeObject(numbers);
out.close();
}
catch(java.io.IOException ioe){}
java.io.ByteArrayInputStream bIn=new java.io.ByteArrayInputStream(bOut.toByteArray());
try{
java.io.ObjectInputStream in=new java.io.ObjectInputStream(bIn);
Integer[] num=(Integer[])in.readObject();
System.out.println(num[0]==num[1]);
}
catch(java.io.IOException ioe){}
catch(ClassNotFoundException cnfe){}
The answer is that true is print out.
Can anybody explain?
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by hai zhang:
What is the result of compiling and executing the following code fragment:
Integer a=new Integer(5);
Integer[] numbers=new Integer[]{a,a};
java.io.ByteArrayOutputStream bOut=new java.io.ByteArrayOutputStream();
try{
java.io.ObjectOutputStream out=new java.io.ObjectOutputStream(bOut);
out.writeObject(numbers);
out.close();
}
catch(java.io.IOException ioe){}
java.io.ByteArrayInputStream bIn=new java.io.ByteArrayInputStream(bOut.toByteArray());
try{
java.io.ObjectInputStream in=new java.io.ObjectInputStream(bIn);
Integer[] num=(Integer[])in.readObject();
System.out.println(num[0]==num[1]);
}
catch(java.io.IOException ioe){}
catch(ClassNotFoundException cnfe){}
The answer is that true is print out.
Can anybody explain?

Conversion goes betn Integer and Byte (U can use Byte ..why Integer?)
When arrays are initialized they get the initial values as 0
so when they are compared they remain same .. so the answer is true
Thankx
Ragu
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi all, my computer was down for a while...
IMHO this question is not about the value of the Integers, because the == compares if the objects are the same not for content.
The question is would be the Java Serialization so clever to only serialize and deserialize once an object if we ask it to do it twice for the same object in the same "serialization session"?
The answer to the question is printed by the program.
I think a definition for a "serialization session" is the time for wich a set of objects are written to the same stream and no changes occurred to the state of them while doing it.


SCJP2. Please Indent your code using UBB Code
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i guess it's not a question of "magic" that output is "true". it's logical. 'coz v r writing "numbers" array object to the stream. not that means the values of numbers array are written. so if v have same values as array members it write same values. when v read it again it and try to see number[0] == number[1] it will give obviously true as the same values were written. it doesn't depend upon type of values (here references). 'coz references are also one type of value only (this may be argued in relation to "if java has passing by ref or not?" 'coz ref are nothing but values:-))
so i think output is natural. any further ideas???
regards,
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
sorry forgot to mention a thing,
if u try to see num[0] and numbers[0] they are different!!! same for num[1] and numbers[1] but num[0] = num[1] and numbers[0] = numbers[1]
-maulin.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

when v read it again it and try to see number[0] == number[1] it will give obviously true as the same values were written

the values of the Integers are not compared with ==
try:
Integer[] ia = { new Integer(5), new Integer(5) };
System.out.println(ia[0]==ia[1]);
It will print false because == compares if the two integers are the same object, that is if they occupy the same address in memory.
The example prints true because Java Serialization only serilazes and desarializes once a, though it was required to do it twice(because the two references to a).
Please read the following by Bruce Eckel about serialization: http://codeguru.earthweb.com/java/tij/tij0116.shtml
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Originally posted by Jose Botella:
the values of the Integers are not compared with ==
try:
Integer[] ia = { new Integer(5), new Integer(5) };
System.out.println(ia[0]==ia[1]);
It will print false because == compares if the two integers are the same object, that is if they occupy the same address in memory.
The example prints true because Java Serialization only serilazes and desarializes once a, though it was required to do it twice(because the two references to a).
Please read the following by Bruce Eckel about serialization: http://codeguru.earthweb.com/java/tij/tij0116.shtml

you didn't get my point. say variable a's reference is at memory location 2000. so numbers[0] = numbers[1] = 2000 when we write the numbers object. so when we read it back we will get 2000 as each array element. i stil don't think it's a question of serialization. it's logical. it has to be that way. if u try to think in terms of C pointers it's clear.
regards,
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
sorry. i'm sorry. i got my mistake. Jose is right. i guess that in serialization once it serializes any object and if the same reference is to be serialize again it doesn't do it but keep some kind of information pointing to the previously serialized object and so we get same object references in the example in the discussion. if we 've an object having reference count 2 it will need to be serialize twice and Java will do it only once keep extra information when the second reference to the same object is to be serialized.
regards,
maulin
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Just in case the following can be thought to be true by anyone:

say variable a's reference is at memory location 2000. so numbers[0] = numbers[1] = 2000 when we write the numbers object. so when we read it back we will get 2000 as each array element.

The serialization process writes fields of an object (state) to a stream. The deserialization process reads these information and creates a new object that has the same state. But it does so in a position of memory that is not related to the one that the object occupied before serialization.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object I/O
 
Similar Threads
decompressing byte[]
Serialization
Deseriaziling parameterised/generic collections
what are the classes that supports a class when it is serialized?
ObjectStreams...