Legato Interview Questions| Java interview | SpringBoot | MicroServices | MongoDB | Docker | AWS
ฝัง
- เผยแพร่เมื่อ 5 พ.ย. 2021
- Java8, Spring, SpringBoot, Microservices, MongoDB, AWS and Docker questions were asked as part of Legato interview.
Spring Boot Custom JPA repository link : • Spring data JPA custom... - วิทยาศาสตร์และเทคโนโลยี
It’s superb. Waiting for more videos
Great Inputs .. Thanks a lot
Very nicely explained
static and transient variable is never take part in serialization process,Can you share code or can bit explain more in details.
Static Variables:
Static variables are class-level variables, shared among all instances of a class. During serialization, static variables are not serialized because they belong to the class, not to individual objects. When deserialized, static variables will contain the values present in the class definition at the time of deserialization.
Example:
import java.io.*;
class MyClass implements Serializable {
static int staticVariable = 10;
int nonStaticVariable = 20;
}
public class SerializationExample {
public static void main(String[] args) {
try {
MyClass obj = new MyClass();
obj.staticVariable = 30; // Modify the static variable
// Serialize the object
FileOutputStream fileOut = new FileOutputStream("object.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
out.close();
fileOut.close();
// Deserialize the object
FileInputStream fileIn = new FileInputStream("object.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
MyClass newObj = (MyClass) in.readObject();
in.close();
fileIn.close();
// Print static variable value after deserialization
System.out.println("Static variable after deserialization: " + newObj.staticVariable);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In this example, the staticVariable is modified before serialization, but after deserialization, its value reverts to the original value defined in the class.
Transient Variables:
Transient variables are not serialized during the serialization process. When an object is serialized, transient variables are skipped and not included in the serialized representation. When deserialized, transient variables are initialized to their default values.
Example:
import java.io.*;
class MyClass implements Serializable {
transient int transientVariable = 100;
int nonTransientVariable = 200;
}
public class SerializationExample {
public static void main(String[] args) {
try {
MyClass obj = new MyClass();
obj.transientVariable = 300; // Modify the transient variable
// Serialize the object
FileOutputStream fileOut = new FileOutputStream("object.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
out.close();
fileOut.close();
// Deserialize the object
FileInputStream fileIn = new FileInputStream("object.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
MyClass newObj = (MyClass) in.readObject();
in.close();
fileIn.close();
// Print transient variable value after deserialization
System.out.println("Transient variable after deserialization: " + newObj.transientVariable);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In this example, the transientVariable is modified before serialization, but after deserialization, its value reverts to the default value (0 for an integer) because it is marked as transient and is not serialized.
Could you please tell this is for how many yrs experienced profile?
5 years