Sunday, 24 March 2019

Example of Java Deserialization .


Code:
?
1
2
3
4
5
6
7

import java.io.*; class Depersist{ 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:: 211 govind

Example of Java Serialization.

Description:
In this example, we are going to serialize the object of Student class.
The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object.
We are saving the state of the object in the file named f.txt.

Code:
?
1
2
3
4
5
6
7

import java.io.*; class Persist{ public static void main(String args[])throws Exception{ Student s1 =new Student(211,"govind"); FileOutputStream fout=new FileOutputStream("f.txt"); ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(s1); out.flush(); System.out.println("success"); } }
Output:: success

Tuesday, 5 February 2019

12. Swapping of two Numbers in java without third variable.


Description:
Write a sample code to Swapping of two Numbers in java without third variable.


Code:
?
1
2
3
4
5
6
7
8
9
10
import java.util.Scanner; public class Swapping_Number { public static void main(String[] args) { int x,y; System.out.println("Enter x and y no: "); Scanner s=new Scanner(System.in); x=s.nextInt(); y=s.nextInt(); System.out.println("Before Swaping \n x="+x+"\n y="+y); x=x+y; y=x-y; x=x-y; System.out.println("After Swaping\n x="+x+"\n y="+y); } }


Output:
Enter x and y no: 
10
20
Before Swaping 
 x=10
 y=20
After Swaping
 x=20
 y=10

11. Palindrome program in java.


Description:
Write a sample code to Palindrome in java.


Code:
?
1
2
3
4
5
6
7
8
9
import java.util.Scanner; class Palindrome { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check :"); original = in.nextLine(); int length = original.length(); for (int i = length - 1; i >= 0; i--) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("The string is a palindrome."); else System.out.println("The string isn't a palindrome."); } }

Output:
Enter a string to check :
madam
The string is a palindrome.

Enter a string to check :
godblessgovind
The string isn't a palindrome.