Monday, November 2, 2015

Constructors in Java.

Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor. 

Constructor name must be same as its class name and must have no explicit return type.

There are two types of constructors:
1. Default constructor (Non Parameterized constructor)
Default constructor provides the default values to the object like 0, null etc. depending on the type.

2. Parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects.  



Constructor overloading used in Java in which a class can have no. of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type. 

class Car
{
    int id;
    String carname;
    String brand;
    int qty;
    Car(int i,String n){
    id = i;
    carname = n;
    }
    Car(int i,String n,Str a){
    id = i;
    carname = n;
    brand =a;
    }
    Car(int i,String n,Str a,int t){
    id = i;
    carname = n;
    brand =a;
    qty = t;
    }
    void display()
    {
    System.out.println(id+" "+carname+" "+brand+" "+qty);
    }
    public static void main(String args[]){
    Car s1 = new Student5(101,"Alto");
    Car s2 = new Student5(102,"Swift","Maruti",25);
    Car s2 = new Student5(102,"Swift","Maruti");    
    s1.display();
    s2.display();
    s3.display();
   }
}

Output:

101 Alto 0
102 Swift Maruti 25
102 Swift Maruti 0

Wednesday, October 14, 2015

Operator Precedence in Java.

Java has well-defined rules for execution order in which the operators in an expression are evaluated when the expression has several operators.
Below are the execution orders i.e. precedence in Java Coding.

LevelOperatorDescriptionAssociativity
1=
+=
-=
*=
/=
%=
Assignment
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
Right
2? :Ternary conditionalRight
3||Logical ORLeft
4&&Logical ANDLeft
5|Bitwise inclusive ORLeft
6^Bitwise exclusive ORLeft
7&Bitwise ANDLeft
8==
!=
Relational is equal to
Relational is not equal to
Left
9<
<=
>
>=
instanceof
Relational less than
Relational less than or equal
Relational greater than
Relational greater than or equal
Type comparison (objects only)
Left
10<<
>>
>>>
Bitwise left shift
Bitwise right shift with sign extension
Bitwise right shift with zero extension
Left
11+
-
Addition
Subtraction
Left
12*
/
%
Multiplication
Division
Modulus
Left
13++
--
+
-
!
~
type )
Unary pre-increment
Unary pre-decrement
Unary plus
Unary minus
Unary logical negation
Unary bitwise complement
Unary type cast
Right
14++
--
Unary post-increment
Unary post-decrement
Right
15()
[]
·
Parentheses
Array subscript
Member selection
Left