0% found this document useful (0 votes)
65 views7 pages

Implementation Serialization To Student Object

The document discusses several Java concepts including serialization, deserialization, generics, wrapper classes, and autoboxing/unboxing. It includes code examples for serializing a Student object to a file, deserializing and printing the object, using generic methods to print arrays of different types, defining a generic class, demonstrating autoboxing of primitive types to wrapper classes and vice versa. The code examples compile and run successfully, printing the expected output.

Uploaded by

Surya Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views7 pages

Implementation Serialization To Student Object

The document discusses several Java concepts including serialization, deserialization, generics, wrapper classes, and autoboxing/unboxing. It includes code examples for serializing a Student object to a file, deserializing and printing the object, using generic methods to print arrays of different types, defining a generic class, demonstrating autoboxing of primitive types to wrapper classes and vice versa. The code examples compile and run successfully, printing the expected output.

Uploaded by

Surya Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Implementation Serialization to student object

Student class
package serialization;

import java.io.Serializable;

public class student implements Serializable {

int id;

String name;

public student (int id,String name)

this.id=id;

this.name=name;

Serializable
package serialization;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class serdemo {
public static void main(String args[])throws Exception{
student s1=new student(211,"hari");
FileOutputStream fout= new FileOutputStream("f.txt");
ObjectOutputStream out= new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
System.out.println("success");
}
}
Output: ​run:

success
BUILD SUCCESSFUL (total time: 0 seconds)

Implementation deSerialization to student object


Student class
package serialization;

import java.io.Serializable;

public class student implements Serializable {

int id;

String name;

public student (int id,String name)

this.id=id;

this.name=name;

Desearializable
package serialization;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class deserdemo {
public static void main(String args[])throws Exception{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));
student s=(student) in.readObject();
System.out.println(s.id+""+s.name);
in.close();
}
}
Output:
run:

211hari

BUILD SUCCESSFUL (total time: 0 seconds)


Implement Generic Methods
package generic;

public class genericme {

public static<E> void printArray(E[]elements){

for(E element:elements)

System.out.println(element);

System.out.println();

public static void main(String args[])

Integer[] intArray={1,2,3,4,5};

Character[] charArray={'J','A','V','A'};

System.out.println("Int Array");

printArray(intArray);

System.out.println("char Array");

printArray(charArray);

Output:

run:

Int Array

5
char Array

BUILD SUCCESSFUL (total time: 0 seconds)


Implement Generic class

package generic;

public class genclas<T,U> {

private static genclas<Integer, String> g;

T a;

U b;

genclas(T x,U y)

a=x;

b=y;

System.out.println(a+""+b);

public static void main(String[] args){

genclas<Integer,String> g=new genclas<Integer,String>(11, "43");

Output:

run:

1143

BUILD SUCCESSFUL (total time: 0 seconds)


Implementation of wrapper class autobox
package wrapper;

public class autobox {

public static void main(String[] args) {

int a=20;

Integer i=Integer.valueOf(a);

Integer j=a;

System.out.println(a+" "+i+" "+j);

System.out.println(i.toBinaryString(a));

System.out.println(i.toHexString(a));

System.out.println(i.toOctalString(a));

Output:

run:

20 20 20

10100

14

24

BUILD SUCCESSFUL (total time: 0 seconds)


Implementation of wrapper class unbox
package wrapper;

public class unbox{

public static void main(String args[]){

Integer a=new Integer(3);

int i=a.intValue();

int j=a;

System.out.println(a+" "+i+" "+j);

Output:

run:

333

BUILD SUCCESSFUL (total time: 2 seconds)

You might also like