Skip to main content

Basic Java

Introduction

Hey guys are you excited to learn Java,then you are at the right place!!! When anyone started learning Java they tried "Hello World" and if it run successfully they are happy and leave it.But the fact is they didn't understand why they are using all the stuff???
So let's get started with what is exactly the meaning of printing "Hello World" on the console

System:

System is the final class from java.lang package contains several useful class fields and methods and cannot be instantiated.
System class encapsulates several aspects of the run-time environment.
Prototype -
             public final class System extends Object

out:

out is the Stream variable. It opens and ready to accept output data.

Prototype-
            public static final PrintStrean out.

println():

println() is the non-static method of PrintStream class from java.io package.
It terminates the current line by writing the line separator string.
The line separator string is defined by the System property line.separator

Prototype-
             public void println();

 NOTE-

There are total 10 println() methods and 9 print() methods in  PrintStream class which we can use for printing data of different datatype.

Transient Keyword

What is transient keyword:

transient is a special keyword who skip your variable while serializing the object

serialization means process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) .

When you declare any variable as transient , JVM will take this as indication for the variable is not part of the persistent state of an object.
Let's create a class with transient variable okay so that it will be more understandable....!
 package com.app; 
 import java.io.Serializable; 
  
   public class Student implements Serializable{ 
     int id; 
     String name; 
     transient int age;    //Now it will not be serialized   
     public Student(int id, String name,int age) {   
         this.id = id; 
         this.name = name;   
         this.age=age; 
      }   
  } 
  
Now we will code to serialize the object....!
 package com.app; 
 import java.io.FileOutputStream;
 import java.io.ObjectOutputStream;
 
 class PersistExample {
  public static void main(String args[]) throws Exception {
   Student student = new Student(101, "Shashank", 22);// creating object
   // writing object into file
   FileOutputStream fileOutputStream = new FileOutputStream("f.txt");
   ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
   out.writeObject(student);
   out.flush();
   out.close();
   fileOutputStream.close();
   System.out.println("success");
  }
 }
 
output:success
Now we will deserialize to object....!
 package com.app; 
 import java.io.FileInputStream;
 import java.io.ObjectInputStream;
 
 class DePersist {
  public static void main(String args[]) throws Exception {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream("f.txt"));
   Student student = (Student) in.readObject();
   System.out.println(student.id + " " + student.name + " " + student.age);
   in.close();
  }
 }
 
output:101 Shashank 0

Comments