Tuesday, June 16, 2009

Java More Definations and Concepts

Types of variables

1) Instance variables--> They are non static members of a class. Every object of a class has its own copy of these variables. Their values exist as long as object containing them exists. Initialized to default valued by default.

2)Static Variables-->They belong to the class and are created when class is loaded first time at runtime.They exists as long as class exists. Initialized to default value by default.

3)Local variables-->Created in method or block and executed for method or block. After the execution of method or block, they are no more accessible. They should be explicitly initialized in a non conditional statement, before being used.


Main method--> This is the method where from execution of a program can start. It must be public, static and void. It should be public so that it can be accessed by Java Interpreter. It should be static so that It can be accessed without object of a class being created. public and static keywords can appear in any order.


Typecasting and converstions:

1) Narrowing--->Conversion of broader datatype to narrower datatype is called narrowing. It results in loss of magnitued information. This means bits from the left of the binary respresentation are truncated to fit in the destination variable. Like conversion from float to int. In this explicit cast is required.

2)Widening--->Conversion from narrower type to the broader one is called widening. In this no explicit cast is required.


Unary Numeric Promotion--> In a unary operator if type of operand is narrower than int, it is converted to int. It is applicable to +,-,>>,<<,>>>,~ and expressions in array initialization and array indexes. Note: not applicable to ++, -- operators.


Binary Numeric Promotion--> In binary numeric promotion type of the expression is promoted to the type of broadest operand which is atleast an int.


String Concatenation-->When an operand is added to a string object using '+' sign, then any of the following cases take place:

a) If other operand is primitive datatype(int, long etc) its value is converted to the string object with string representation of its value.

b) Values like true, false and null are also converted to String objects their corresponding string respresentation. A reference variable with null value is also converted to String with string representation as "null"

c) For all other references, String is constructed by calling toString method of the referred object.


Conditional Operators:

They are used to check the condition of an expression.

&& --> Shortcircuit AND

& --> Bitwise AND

|| --> Shortcircuit OR

| --> Bitwise OR

Difference between && and & is as given below:

In case of && if first expression is false then next condition is not checked. However in case of & if first condition is false, still next condition is checked.
! --> NOT

It  is used to invert the value of a boolean expression. true is changed to false and vice versa.


Integer Bitwise operators:

~ Bitwise Compliment--> Inverts all bits of an operand

& Bitwise AND--> Returns 1 if both corresponding bits of two operands are 1, else returns 0.

! Bitwise OR--> Returns 0 if both corresponding bits of two operands are 0, else returns 1.

^ Bitwise Exclusivley OR--> Returns 1 if one bit of the two operands is 0 and another one is 1, else returns 0.


Shift Operators

a>>n--> Right shift carry sign bit

a<--> Left shift zero fill.

a>>>n--> Right shift zero fill.


Search Amazon for Best Books on Java J2EE


blogarama - the blog directory

Search your favourite topics