Translate

Sunday 21 December 2014

Variable Length Arguments in java

Java supports variable length argument of one data type. That is we don't need to define number of arguments when its variable for one type of data-type.

For example:-
class Calculator
{
    public int sum (int... a)
    {
        int total=0;
        for(int i=0; i < a.length; i++)
                    total+= a[i];
        return total;
    }
}

A variable length argument allows us to pass any number of arguments while calling the method.
There can be only one variable length argument in a method and if  present it must be the last parameter.

for example
public void method(String S, float f, int... a)
{
   .......
}

Variable length argument reduces number of method if method overloading is being done on the basis of number of parameters.

Saturday 20 December 2014

Constructors in Java

Constructors are special methods that are called automatically when an object of the class is created. Constructor have same name as the class and they dont have a return type not even void.
Basically there are 3 types of constructors

  1. Default
  2. Parameterized
  3. Copy-constructor
Default Constructor:- This constructor takes no argument. It is called when an object is created without any explicit initialization.
              Money m1 = new Money();
If we have not defined any constructor then default constructor is provided by java.

Parameterized Constructor:- This constructor is called when object is created  and initialised with some value at the time of creation.
               Money m1 = new Money(1000,20);

Copy Constructor:- This constructors is called when an object is created. It's initialised with some other object of class at the time of creation.
               Money m1 = new Money(1000,20);             
               Money m2 = new Money(m1);

Definition of constructor looks like this
class Money
{
    private int rs, paisa;
    public Money()
    {
         rs = paisa = 0;
    }
    public Money (int r, int p)
    {
         rs = r;
         paisa = p;
    }

    public Money (Money m)
    {
         rs = m.rs;
         paisa = m.paisa;
    }
    public void set (int r, intp)
}

Always remember that we can call a method from an another method of the same class, simply by using it's name. 

this keyword in java

'this' is a reference variable that refers to the object that has called the member function. 'this' is available in all methods of a class except static methods. It is created implicitly so we don't need to declare it.

class Money
{
     private int rs, paisa;
     public void set (int rs, int paisa)
    {
       this.rs = rs;
       this.paisa = paisa;
    }
}

Now in above example rs and paisa to the left of equal op belongs to class and to the right are arguments passed.

Method Overloading in Java

While defining a method it is compulsory to specify return type, otherwise it would give a compile time error. In java following syntax is followed to define a method.

access_specifier return_type method_name(argument 1, ..., argument n)
{
    .....//main body
}

method_name(argument 1, ..., argument n) is known as signature of method.
We can have as many methods in our class as we want as long as their signature is different. We can have multiple methods with the same name as long as their argument list are different. This is called Method Overloading. Either no of argument must be different or type of argument must be different or order of argument must be different.

For example:-

class Calculator
{
    public int sum(int a, int b)
    {
        return a+b;
    }
    public int sum(int a, int b, int c)
    {
        return a+b+c;
    }
    public float sum(int a, int b)
    {
        return a+b;
    }
}

class Java
{
    public static void main(String Args[])
    {
         Calculator calc = new Calculator();
         int x,y;
         float f1;
         x = calc.sum(3,4);
         System.Out.Println("3+4=" + x);
         y = calc.sum(3,4,5);
         System.Out.Println("3+4+5=" + y);
         f1 = calc.sum(1.2f, 1.4f);
         System.Out.Println("1.2+1.4=" + f1);
    }
}

Method overloading is an example of compile time Polymorphism.

Wednesday 3 September 2014

Object Oriented Programming (Class)

If a problem is solved in terms of classes and objects. It is called OOP.

Class:-
A class provides definition for an object. Normally variables of a class are declared as private members which are directly not accessible to the user and methods form public part of class which is accessible to user.
Object is an instance of a class.

Lets do an example:-
class Money
{
    private int rs;
    private int paisa;
    public void set(int r, int p)
    {
        rs = r;
        paisa = p;
    }
    public void show()
    {
        System.out.print(rs + " " + paisa);
    }
}

Save it as Money.java

class UseMoney
{
    public static void main(String args[ ])
    {
        Money m;
        m1 = new Money();
        m1.set(100,20);
        System.out.print("First amount is");
        m1.show();
        Money m2 = new Money();
        m2.set(200,30);
        System.out.print("Second amount is");
        m2.show();
    }
}

Save it as UseMoney.java
Then compile
c:> javac Money.java
c:> javac UseMoney.java
Now you have compiled these two java files. Now you have to run the file which contains main function.
c:> java UseMoney

This will execute your java program.

Sunday 31 August 2014

String Buffer Class

String buffer objects are used to store character sequences but they are mutable objects which means that they can be altered directly.

Creating a String Buffer Object:-
StringBuffer s1 = null;
StringBuffer s2 = new StringBuffer();
StringBuffer s3 = new StringBuffer("JAVA");
StringBuffer s4 = new StringBuffer(1000);     // Size of string buffer is 1000

By default initial memory allocated to s2 is 16 characters.
For s3 it is 4+16 characters.
So when we create a StringBuffer object, by default memory is allocated for 16 characters. When we create a StringBuffer object and initialize it with string. Then memory allocated will be length of string and 16 characters more. We can also create object with specified memory.

StringBuffer s3 = new StringBuffer("JAVA");
System.out.println(s3.capacity);                // 4 + 16
System.out.println(s3.length);                    // 4

The capacity of StringBuffer object is no of characters it can hold at any instant and length is the number of character it is holding.

Garbage Collector

In Java garbage collector is a low priority thread that runs automatically and destroys the objects that are not being referred to by any reference variable. We can request to run garbage collector by System.gc( ). It is not guaranteed that this request would be fulfilled.

e.g
1.)
String s1="xy";
String s2=s1;
s1=s1+z;
s2=s1;
s1=null;      //"xy" is now eligible for garbage collection
s2=null;


2.)
void Method1( )
{
    String s1 = "xy";
    String s2;
    s2 = method2(s1);
    s2 += "cd";
    s2 = null;
}      //s1 is local variable so after the end of method Method1 "xy" is eligible for garbage collection

String Method2(String s3)
{
    s3 = "Java";
    return s3;
}

Total Pageviews