java - New objects: new operator vs serialization -


as understood i've read there 2 ways create new objects in java: 1) using new operator , 2) means of serialization (for deep copying, example). other object manipulations (like assigning, instance) deal references existing objects. difference between 2 above mentioned ways in terms of inner logic? seems 1 difference serialization somehow doesn't use constructor methods. right? there other differences?

by 'inner logic' mean how compiler (or whoever deals it) creates object step-by-step, algorithm uses, methods used , on. more margaret bloom writing in answer in more detail.

further confusion clarification:

so right during deserialization copy of object:

class class1 {     static array_length = 10;     public class1() {         int[] anarray = new int[array_length];         anarray[0] = 5;         ...         anarray[9] = -2;     } } 

will include copy of array created elsehow (how? since no constructor has been called)? , furthermore, though original array (before serialization) has been created using static field (which lost during serialization) deserialized copy nevetheless identical original array?

serialization , new operator different things, though both result in reference newly allocated object.

you can find detailed information new operator in chapter 15.9.4 run-time evaluation of class instance creation expressions of java language specification.

at run time, evaluation of class instance creation expression follows.
[...]
next, space allocated new class instance.
[...] new object contains new instances of fields declared in specified class type , superclasses.
[...] next, actual arguments constructor evaluated, left-to-right. [...]
next, selected constructor of specified class type invoked. results in invoking @ least 1 constructor each superclass of class type.
value of class instance creation expression reference newly created object of specified class. every time expression evaluated, a fresh object created.

editing mine
long story short, new allocates space new object (specifically space fields), initialize fields default values , invokes chosen constructor.


java serialization matter entirely.

the ability store , retrieve javatm objects essential building transient applications. the key storing , retrieving objects in serialized form representing state of objects sufficient reconstruct object(s). emphasis mine

which means serialization designed allow programmer save objects states persistent medium (abstracted stream within java) , read them back.

as such, deserializiation not invoke constructors since object state restored automatically reading out stream. can override default behavior though.

reading object objectinputstream analogous creating new object. new object's constructors invoked in order superclass subclass, object being read stream deserialized superclass subclass. the readobject or readobjectnodata method called instead of constructor each serializable subclass during deserialization.

emphasis mine


said that, stress out how using new , serialization totally unrelated things conceptual point of view.
in first case creating own new object, in latter reading saved object (possibly else).

even though can thought similar final result, should have, in mind, clear distinction between twos.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -