IO Streams

The basic idea of  IOStreams is
  • Storing and reading data from files
  • reading data from keyword
First let us understand some basic terminology.

Persistent media:

  • The environment that allows us to store data permanently is called persistent media.
  • We can store data permanently in three places
  1. File
  2. Database
  3. Remote Computer 

Persistence:

  •  The process of storing data permanently in a persistence media.

Persistence Logic:

  • The logic that persist data in a persistence media is called persistence logic.
  • Ex: IOStreams based logic, JDBC logic, Networking based logic.

Persistence Technologies:

  •  The technology that provides API to develop persistence logic is called persistence technology.
  • Well known persistence technologies are 
  1. IOStreams : to persist data in files
  2. JDBC, EJB, Hibernate: to persist data in db
  3. Networking: to persist data in remote computer

Where can we store data permanently? 

  • In persistence medias either files or in databases.
  • Storing data in variables and arrays  is temporary. Data will be lost when a local variable goes out of scope or when the program terminates.
  • programmers use files or databases for long term storage of large amount of data. it is available even after termination of the program. We refer to data maintained on files as persistent data, because the data exists beyond the duration of the program execution.
  • To store data in files and databases  Oracle has given in  built API. We all need to do is creating the particular class object calling methods for storing and reading data from that persistence media.
  • IOStreams API is given to store and read data from files
  • JDBC API is given to store and read data from Databases. 

How java application can store or read data from a file?

  • Using stream object.

Introduction to Streams:

  • Stream is logical connection between java program and a file.
  • To store the data in the persistence media there should be a way to connect to persistence media from java application either physically or logically. Stream provides logical connection.
  • "Its a continuous flow of data between java program and persistence media"

Direction of stream flow:

  • Stream has a direction and its direction depends on the viewer's view. In java the viewer is java Application. If you look from Java Application it is sending out from Java Application.


Type of Streams:

  • Generally Streams are divided in to two types based on data flow direction.
  1. InputStream.
  2. OutPutStream.

InputStream:

  • The stream that allows data to come into the java application from the persistent media is called input Stream.

OutPutStream:

  • The stream that allows data to send out from the java application to be stored into the persistence media is called OutPutStream.
  • Basically InputStreams are used to read data from a persistence media , and Oputstreams are used to write or store  data in a persistence media from a java application

Types of Java Streams:

  • In java we are allowed to send data through streams only either in the format of bytes or characters. So based on the type of the data passed through streams .
  • In java streams are divided in to two types.
  1. Binary Streams.
  2. Character Streams.

 1.Binary Streams: 

  • The streams which read and write data in the format of  bytes is called Character streams.

 2.Character Streams:

  • The streams which read and write data in the format of  characters is called Character streams.
Java Streams

Programing Inrerview Questions on try catch

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("instance of java");    
  10.  
  11.     }
  12.  
  13. }
  14. }





2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("try block");   
  10.  
  11.     }
  12.    finally{
  13.  
  14.     System.out.println("finally block");    
  15.  
  16.   }
  17.  
  18. }
  19. }







3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.             
  8.         try {
  9.  
  10.             int a = 0;
  11.             int b = 10;
  12.             int c = b / a;
  13.  
  14.             System.out.print("try block");
  15.  
  16.         }
  17.         catch(Exception e) {
  18.  
  19.               System.out.print("catch block");
  20.  
  21.         }    
  22.  
  23. }
  24. }




String programing interview questions

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.charAt(3));
  9. }
  10. }



2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.length());
  9. }
  10. }








3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str1="abc";
  8.       String str2="abc";
  9.  
  10.        System.out.println(str1.equals(str2));
  11. }
  12. }






4. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.         String str1 = "abc";
  8.         String str2 = "abc";
  9.         String str3= new String("abc");
  10.  
  11.         System.out.println("str1 == str2 ? "+(str1==str2));
  12.         System.out.println("str1 == str3 ? "+(str1==str3));
  13.         System.out.println("str1 equals str3 ? "+(str1.equals(str3)));
  14.  
  15.  
  16. }
  17. }






Inheritance programming interview questions

1. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("super class method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.   System.out.println("sub class method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  SubDemo subobj=new SubDemo();
  10.  subobj.show();
  11. }







2. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.  super.x=y+2;
  9.   System.out.println("x="+super.x+"y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }




3. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo {

  4.  int x;

  5. }

  1. package com.instanceofjava;
  2.  
  3. public class SubDemo extends SuperDemo{
  4.  
  5.  int y;
  6.  
  7. public void show(){
  8.  
  9.  super.x=y+2;
  10.   System.out.println("x="+super.x+"y="+y);
  11.  
  12. }
  13.  
  14.  public static void main(String args[]){
  15.  
  16.  SubDemo subobj=new SubDemo();
  17.  subobj.x=2;
  18.  subobj.y=2;
  19.  subobj.show();
  20.  
  21. }




4. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4.  int x;
  5. SuperDemo(){
  6.  
  7.  x=24;

  8. }
  9. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{
  3.  
  4.  int y;
  5.  
  6. public void show(){
  7.  
  8.   System.out.println("x="+super.x);
  9.   System.out.println("y="+y);
  10.  
  11. }
  12.  
  13.  public static void main(String args[]){
  14.  
  15.  SubDemo subobj=new SubDemo();
  16.  subobj.show();
  17.  
  18. }





Arrays in java

Array:

  • Array is referenced data type used to store multiple values.
  • we can not change size of array at runtime.
  • memory can not be altered Once the memory has been allocated for an array at the run time.
  • Arrays are objects


Need of array:

  • In  real time projects array is used to collect same type of objects to send all values with single method call.

Problem of primitive data types:

  • We can not store values in continuous memory locations using primitive data types .
  • We have two problems due to this limitation

1.we cant store multiple values:

  • If we want to store multiple values , say 1 to 10 , we must create 10 variables .
  • All those 10 variables are created at different locations.

2.In single method call we can not pass multiple values :

  • we can not pass all values to the remote computer with single network call or method call.Using primitive variables  , which increases burden on network and also increase number of lines of code in program.

Solution:

  • Values must be stored in continuous memory locations with single variable name. To solve above two problems ,
  • This can be possible using array.
  • In java array is reference data type . it is used to store fixed number of multiple values of same type in continuous memory locations.
  • Like other data types array is not a keyword it is a concept. it creates continuous memory locations using other primitive or reference types.

 Array Limitation: 

  •  Array size is fixed , means we can not increase or decrease its size after its creation.

Array Declaration:

  • <Accessibility modifier><Modifier><datatype>[] <array variable name>;

For example:

  • public static int[] i;
  • public static Example[] e;
  • Like in C or C++ , in java we can not mention array size in declaration part. It leads Compile time error.
  • int[5] i; Compile time Error: illegal start of expression
  • int [] i;

Possible declaration:

  1. After data type : int[] i;
  2. Before variable name : int []i;
  3. After variable name : int i[];

Type of arrays:

  1.  Single dimensional : int [] i;
  2. Two dimensional : int[][] i;
  3. Three dimensional : int [][][] i;

Array object creation:

  • <Accessibility Modifier><Modifier><data type>[]<array name>={<list of values with , separator>};
  • int [] ia={10,20,30,40};

Super keyword

super:

  • The functionality of super keyword is only to point the immediate super class object of the current object.
  • super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
  • super keyword used to access the members of the super class object.
  • super.member;
  • It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.

Uses:

  • Using super keyword we can explicitly point the immediate super class object and access all the super class members which are present with the same name of the subclass members from the sub class methods.

  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.instanceofjavaforus;
  2. public Class Subdemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7.  
  8. super.a=10;
  9. super.b=20;
  10.  
  11.   super.show();
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  Subdemo obj= new Subdemo();
  17.  obj.a=1;
  18. obj.b=2;
  19. obj.disply();

  20.  
  21. }
  22. }
  23. Output: 
  24. 1
  25. 2
  26. 10
  27. 20

  • Using super keyword we can explicitly call the super class constructor.
  • The statement which explicitly calling super class constructor should be always the first statement of the sub class constructor.
  • The statement which explicitly calls the super class constructor should be present inside a constructor only.


  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10.  SuperDemo(int x, int y){
  11.  a=x;
  12. b=y
  13. }
  14. }


  1. package com.instanceofjavaforus;
  2. public Class Subdemo{ 
  3. int a,b;
  4.  
  5. SubDemo(int x, int y){
  6. super(10,20);
  7.  a=x;
  8. b=y
  9. }
  10.  
  11. void disply(){

  12. System.out.println(a);
  13. System.out.println(b);
  14.   super.show();
  15. }
  16.  
  17. public static void main (String args[]) {
  18.  Subdemo obj= new Subdemo(1,2);
  19.  
  20. obj.disply();

  21.  
  22. }
  23. }
  24. Output: 
  25. 1
  26. 2
  27. 10
  28. 20

 

Key points to remember:

  • It is a keyword used to store super class non static members reference in subclass object.
  • Pre defined instance variable used to hold super class object reference through sub class object.
  • Used to separate super class and sub class members if both have same name.
  • It must be used explicitly if super class and sub class members have the same name.
  • Used to call super class constructor from sub class constructor.


Select Menu