Tuesday 2 June 2015

Introduction to Java

Analysis can be done only in structured form of data.
Java:
char takes 2 bytes using the unicode encoding standard.
boolean data type has two values: True or False.
Pillars of OOPs: Encapsulation, polymorphism and Inheritance.
Inheritance is needed for overriding and runtime polymorphism.

class name
{
instance variable (Normal variable)
static variable (shared by all the objects)
constructors (needed most for inheritance)
methods
intialization block
Unnamed block/anonymous block
}

initialization block:
class A
{
int a;
{
a=10;   //will execute on creation of an object before the call goes automatically to constructor.
}
int b;
{
a=50;
}
.
.
.
{
a=60;
}
}

the various blocks execute in the exact order as written in the class.

static block:
static int a;
static { a=10; } // this will execute whenever a static variable is used.

There is no destructor in java, instead there is a garbage collector.

floating point:
float f=4.96f
or f=4.96F
or f=496e-2

String is an inbuilt class in Java.

Lifetime and scope of a variable:

Type conversion and casting:
implicit:
float b;
int a;
b=a;
explicit:
float b;
int a;
a=(int)b;

in java, all expressions are automatically promoted to int
eg: byte a=2,b=4,c;
c=a*b //not possible
this is because a*b is promoted to int and c is still in byte format.
instead do this:
c=(byte)a*b;
or int c=a*b;

Arrays:
Arrays are treated as an object in java.
only runtime allocation is allowed.
int a[];
int []a;
and then this is followed by,
a=new int[10];
similarly,
int a[][];
or int [][]a;
or int []a[];

int a[][]={{1,2},{1},{3,2,2}}

No comments:

Post a Comment