Friday, 20 November 2015

Learning the Concept of Arrays In Java

Arrays in Java

An Array is a group of related data items that share a common name. The extend of an array in Java is established when the array is formed. Once the array is created, the length is fixed.

Array Index and Elements

               A loop with the subscript as the control variable can be used to read the entire array.

One Dimensional Arrays

               A list of items can be given one variable name using only one subscript is called one dimentional array. 

          - Creating an one dimentional array in Java

                   Syntax:   type arrayname[];
                                    type[] arrayname;

Both the above statements do the same operation. Creation of an array in Java is a three step process. 

          i) Array Declaration
         ii) Creation of Memory Location
        iii) Assign Values

i) Array Declaration

          Arrays in Java can be declared in two forms as shown in the syntax above. 

Example for Array Declaration:

          int [] serialnumber;
          int serialnumber[];

ii) Creation of Memory Location

          Create memory location for the declared array. 

Example for creation of memory location:

          serialnumber = new int[5];

The above statement create 5 int type locations for the array serialnumber. We can also declare and create memory location using a single statement in Java.

Example: int serialnumber[] = new int[5];

iii) Assign Values

Assign values means nothing otherthan initialization of arrays. 

Syntax for initializing an Array:

          arrayname[subscript] = value;
          type arrayname[] = {list of values};

First line assign values to particular element in the array and the second statement shows the syntax for assigning a list of values in to an array. 

Example for initializing an array:


Above five statements can be replaced with the single statement below.

int[] serialnumber = {1200, 1201, 1202, 1203, 1204};

Sunday, 1 November 2015

Java Method Overloading

Method Overloading in Java

In java method overloading means we are using overloading methods.That means if a class have multiple methods by same name but different parameters, it is known as Method Overloading.

If we need to perform one operation and as explained above the method name increases the readability of the program.

Method overloading is a type of polymorphism, called static polymorphism.

Different ways to overload the method
There are two ways to overload the method in java
  1. By changing number of arguments
  2. By changing the data type

Example 1: Overloading – Different Number of parameters in argument list

class MethodOverloading
    public void display(int a)
    public void display(int a, char ch)  
         System.out.println(ch+ " "+a);
class Sample
   public static void main(String args[])
       MethodOverloading abc= new MethodOverloading();
A 5

2)Example of Method Overloading by changing data type of argument

class MethodOverloading {     void sum(int a,int b){System.out.println(a+b);}     void sum(double a,double b){System.out.println(a+b);}        public static void main(String args[]){     MethodOverloading abc=new MethodOverloading ();     abc.sum(5.5,5.5);     abc.sum(10,10);        }   }  

Tuesday, 11 August 2015

Difference between Methods and Constructors in Java

Methods Vs Constructors in Java

What is the difference between Methods and Constructors in Java?

               It is obvious that Constructor and Method in Java make confusion for the beginners. Here are few points which illustrate the difference between a constructor and method in Java.





It is not necessary to use the same name of the class to create a Method.

The name of the Constructor should be same as the class name it resides.


Method is an ordinary member function which is used to expose  the behavior of an object

Constructor is a member function of a class used to initialize the state of an object


Methods must have return type unless it is specified as void.

Constructor does not have a return type.


Compiler does not create a method in any case if one is not available.

Java compiler creates default constructor if the program doesn’t have one.


Methods invoked explicitly. 
Invoked using the dot operator.

Constructor invoked implicitly. i. e. Invoked using the keyword ‘new’.

               Hoping that the above table gives an idea about how Constructors are different from methods in Java.

Constructor Method Difference

Tuesday, 4 August 2015

Static keyword in java

Java Static keyword

For memory management in java used the static keyword.
We can use this keyword with variable,method,blocks and nested class.

In Java Variables can be declared with the “static” keyword.

Example: static int x = 0;  
  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variable.
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object
Variables which are declared with a static keyword inside a class (outside any method) are known as Class variable / Static variable. They are known as Class level variable because values of these variables are not specific to any instance but are common to all instances of a class. Such variables will be shared by all instances of an object.

1. Java Static Variable

Sample Program:

// static variable 
class Office{ 
   int id; 
   String name; 
   static String department ="Payroll"; 
   Office(int a,String b){ 
   id = a; 
   name = b; 
 void display (){
 System.out.println(id" "+name+" "+department);
 public static void main(String args[]){ 
 Office ab1 = new Office(101,"Gokul"); 
 Office ab2 = new Office(102,"Ram"); 


101 Gokul Payroll
102 Ram Payroll

2. Java Static method

// Static method

class Office{ 
     int id; 
     String name; 
     static String department = "
     static void change(){ 
     department = "HR"; 
     Office(int a, String b){ 
     id = a; 
     name = b; 
     void display (){System.out.println(id" "+name+" "department);} 
    public static void main(String args[]){ 
    Office ab1 = new Office (101,"Gokul"); 
    Office ab2 = new Office (102,"Ram"); 
    Office ab3 = new Office (103,"Krishna"); 


101 Gokul HR
102 Ram HR
103 Krishna HR