preload
Feb 24

Dear Open Peta Users

I have prepared Java basics syntax sheet which would be very useful for beginners and students . The following topics has been covered in style sheet

  1. Java Comments
  2. Data Types
  3. Variables
  4. Constants
  5. Operators
  6. Mathematical Functions
  7. Reading Input
  8. String Class
  9. Conditional Statements
  10. Branching and Looping
  11. Classes and Objects
  12. Inner Class
  13. Interface

The Remaining topics will be released in next version .

Click Here To Download the Syntax Sheet

You can modify , use , distribute to anyone . No restrictions apply to this Syntax Sheet.

Jul 08

Since Java is pure object oriented programming , all things must be done using the help of the objects. Some times if  developer wants to transfer the object from one JVM to another JVM or one network to another network , its very difficult to pack or unpack the object contents, because the object can have any type of datatypes or methods like images ,videos etc .. , serialization helps to do this task.

Object Serialization

Object Serialization is the process of converting object into stream of bytes which would be transferred via network. In other end the stream of bytes would be converted into original object , this process is called object de-serialization. The stream of bytes will be stored into a file in local disk after serialization.Now lets take small review about classes and methods used for Serialization and De-serialization process.

Package Name : java.io

Classes :

  1. ObjectOutputStream
  2. ObjectInputStream
  3. FileOutputStream
  4. FileInputStream

Methods :

  1. Object readObject()
  2. void writeObject(Object obj)

Example Program

Now lets create one Employee Object with some fields then we can apply the serialization concept.

1. Employee.java

package com.rmi.serial;
 
/*
 * Author :Anthoniraj.A
 * Date  : 30/06/09
 * Description : Program For Object Creation
 */
import java.io.Serializable;
 
public class Employee implements Serializable
{
    String name;
    String designation;
    double salary;
    int exp;
}

2.Employee Serialization

package com.rmi.serial;
/*
 * Author :Anthoniraj.A
 * Date  : 30/06/09
 * Description : Program For Object Serialization
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class EmpSerializable
{
  Employee emp = null;;
  ObjectOutputStream oos = null;
 
  private void setValues()
  {
      emp = new Employee();
      emp.name = "Antony";
      emp.designation="AssistantProfessor";
      emp.salary =32000;
      emp.exp = 3;
  }
  private void serializeValues()
  {
        try
        {
          oos = new ObjectOutputStream(new FileOutputStream(new File("sample.ser")));
          oos.writeObject(emp);
          System.out.println("An Employee Object is serialized into sample.ser file");
        }
        catch (IOException ex)
        {
             ex.printStackTrace();
        }
        finally
        {
            try
            {
               oos.flush();
               oos.close();
            }
            catch (IOException ex)
            {
                Logger.getLogger(EmpSerializable.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
  }
  public static void main(String a[])
  {
       EmpSerializable es = new EmpSerializable();
       es.setValues();
       es.serializeValues();
  }
 
}
 
OutPut
An Employee Object is serialized into sample.ser file

3.Employee DeSerialization

package com.rmi.serial;
/*
 * Author :Anthoniraj.A
 * Date  : 30/06/09
 * Description : Program For Object Deserialization
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
 
public class EmpDeserializable
{
  ObjectInputStream ois = null;
  private void getValues()
  {
        try
        {
           ois = new ObjectInputStream(new FileInputStream(new File("sample.ser")));
 
           Employee em = (Employee) ois.readObject();
           System.out.println("Employee Name :" + em.name);
           System.out.println("Employee Designation :" + em.designation);
           System.out.println("Employee Salary :"+em.salary);
           System.out.println("Employee Experience :" + em.exp);
 
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
        }
        finally
        {
            try
            {
                ois.close();
            }
            catch (IOException ex)
            {
               ex.printStackTrace();
            }
        }
     }
  public static void main(String a[])
  {
      new EmpDeserializable().getValues();
  }
}
 
output
Employee Name :Antony
Employee Designation :AssistantProfessor
Employee Salary :32000.0
Employee Experience :3
Feb 16

1.Wrapper Classes

Primitive Types – Wrapper Class
byte – Byte
short – Short
char – Character
boolean – Boolean
int – Integer
long – Long
float – Float
double – Double

package org.vit.java.collections;
public class WrapperTest {
 
public static void main(String[] args) {
double d1 = 1.1234;
Double d = new Double(d1);
System.out.println(d.intValue());
System.out.println(d.doubleValue());
System.out.println(d.floatValue());
System.out.println(d.isNaN());
System.out.println(d.byteValue());
System.out.println(d.compareTo(0.0));
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
 
}
 
}

Output :
1
1.1234
1.1234
false
1
1
4.9E-324
1.7976931348623157E308

2.Type Casting

package org.vit.java.collections;
 
import javax.swing.JOptionPane;
public class TypeCasting
{
 
public static void main(String[] args)
{
int x =10;
String str = String.valueOf(x);
float f = Float.parseFloat(str);
JOptionPane.showMessageDialog(null,x);
}
 
}

Output:
The result is 10.0

3. Vector Example

package org.vit.java.collections;
 
import java.util.Vector;
 
public class VectorTest {
 
public static void main(String[] args) {
Vector v = new Vector();
System.out.println("The Default Capacity is "+v.capacity());
v.add("antony");
v.add("raj");
v.add("10");
v.add("23.4");
v.add("kamal");
v.add("234");
v.add("214.40");
v.add("java");
v.add("2.0");
v.add("2.4");
v.add("11");
v.add("end");
System.out.println("The Vector is growing its size doubled s"+v.capacity());
System.out.println("The first element of vector is "+v.firstElement());
System.out.println("The last element of vector is "+v.lastElement());
System.out.println("The 10th element of vector is "+v.get(9));
System.out.println("The size of the vector is "+v.size());
v.set(0, "start");
System.out.println("The first element of vector is "+v.firstElement());
v.remove(9);
System.out.println("The Vector Values are");
for(int i=0;i<v.size();i++)>
{
System.out.print(v.get(i)+" ");
}
v.clear();
}
}

Output :
The Default Capacity is 10
The Vector is growing its size doubled s20
The first element of vector is antony
The last element of vector is end
The 10th element of vector is 2.4
The size of the vector is 12
The first element of vector is start
The Vector Values are
start raj 10 23.4 kamal 234 214.40 java 2.0 11 end