Wednesday, 26 February 2014

System.out.println


System is a class in the java.lang package.
out is a static member of the System class, and is an instance of java.io.PrintStream.
println is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.


1)System: It is the name of standard class that contains objects that encapsulates the standard I/O devices of your system.
It is contained in the package java.lang.Since java.lag package is imported in every java program by default,therefore java.lang packageis the only package in Java API which doesnot require an import declaration.
2)out:The object out represents output stream(i.e Command window)and is the static data member of the class system.
So note here System.out (System -Class & out- static object i.e why its simply refered by classname and we need not to create any object).
3).println:The println() is method of out object that takes the text string as an argument and displays it to the standard output i.e on monitor screen.

 
Note:
 System -Class
out -static Object
println() -method

Friday, 21 February 2014

Sorted Array





public class ArraySort {

public static void main(String[] args) {
int a[] = {6,500,700,200,1000,1};   
int temp=0;
/*** Ascending Order ***/
for(int i=0;i<a.length;i++)
{   
for(int j=a.length-1;j>i;j--)
{   
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;   
}
}
}
for(int i=0;i<a.length;i++)
System.out.println("a["+i+"] = "+a[i]);
}
}



                                                   (OR)

public class Test {
    public static void main(String[] args)
    {
        int a[] = {6,500,700,200,1000,1};   
        int temp=0;
        /*** Descending Order***/
        for(int i=0;i<a.length;i++)
        {   
        for(int j=0;j<a.length;j++)
        {   
        if(a[i]>a[j]){
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;   
        }
        }
        }
        for(int i=0;i<a.length;i++)
            System.out.println("a["+i+"] = "+a[i]);
    }
                                                       (OR)
public class SortedArray
{
    public static void main(String[] args) {

        int a[]={30,7,9,20};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
}
}

Tuesday, 18 February 2014

String character counting using for loop

public class CountCharacters {

    public static void main(String args[])
    {
        
        String input = "Today is tuesday"; //count number of "a" on this String.
        int charCount = 0;
        for(int i =0 ; i<input.length(); i++){
            if(input.charAt(i) == 'd'){
                charCount++;
            }
        }
        System.out.println("count of character 'd' on String: 'Today is Monday' using for loop  " + charCount);
}
}

Monday, 3 February 2014

Parsing

Parsing : -
              -- Parsing is to read the value of one object to convert it to another type.
              -- For example you may have a string with a value of "10". Internally that string contains the Unicode characters '1' and '0' not the actual number 10.
              -- The method Integer.parseInt takes that string value and returns a real number.

Example :

        public class Parsing {
        public static void main(String[] args) {
        String somestring ="10";
        int i = Integer.parseInt(somestring);
        System.out.println(i);
}
}
               

Polymorphism

-- Concept of exhibiting different behaviours in different situations by a method is known as polymorphism.

There are 2 types of polymorphism.
1.Method or static polymorphism
2.Object or dynamic polymorphism

Static polymorphism :

              --  Here the same method will give different result based on the arguments that we are passing so this is known as method polymorphism.
              -- which method has to be executed will be decided by the compiler during the compilation is known as static polymorphism.
              --  We can achieve the method polymorphism using method overloading.
             -- compiler it self will decide which method has to be executed first is known as“StaticPolymarphism”

 Method overloading or method overwriting:-
                                       --Concept of defining a method with same name and with different signatures is known as method overloading.


Dynamic Polymorphism :

              -- Same method will give different result based on the object acting upon it.(object polymorphism)
              -- In this case which method has to be executed will be decided by the jvm during runtime(dynamic)
              -- We can achieve dynamic polymorphism by using method overriddenig.

Method overriding :
              -- Concept of defining the same method with same signature inside the subclass.

Sunday, 2 February 2014

Encapsulation

 --  Binding the data along with their corresponding functionalities.
 -- we achieve the concept of encapsulation only by declaring all the variables as private and providing public setter,getter methods
 -- javaBeans are used to transfer the data. so, java beans are also known as "data transfer objects" (DTO).
this is used to set the value and get the values.so,it is known as "value object"(VO)

example :

public class Employee {
    private int id;
    private String name;
    private String addr;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
   

}

package com.pointred.encaptest;

public class Test {
    public static void main(String[] args) {
        Employee em = new Employee();
        em.setId(4);
        em.setName("bala");
        em.setAddr("bangalore");
        System.out.println("values are "+em.getName()+" " +em.getAddr() +" "+em.getId());
       
    }

}

Deserialization

--Process of reading the state of an object from the byte stream is known as Deserialization

serialization

1.Serialization ?

          -- process of writing the state of an object into a byte stream which is known as serialization.
          --   State of an object” means data present in the object at any instance of time.
          -- If any class is implementing serializable interface JVM will support to convert the state of object of those classes into a byte stream
          --  Transferring the data contained in the object from RAM to hard disk is known as Serialization.
         --Transfering data through network that Object most be a serializable object that means it helps in decoding data before tranfering and encode the data after revecing.


Example :

public class Student implements Serializable {
    int id;
    String name;
    public Student(int id,String name)
    {
        this.id = id;
        this.name = name;
    }
}

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class Persist {
public static void main(String[] args) throws Exception {
    Student s = new Student(3, "bala");
    try {
        FileOutputStream fout = new FileOutputStream("E:/f.txt");
        ObjectOutputStream out = new ObjectOutputStream(fout);
        out.writeObject(s);
        out.flush();
        System.out.println("Serialized data saved");
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
   
}
}