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.


Wednesday, June 10, 2009

Java Important Terms, Definations and Concepts

Features of Java


1) Encapsulation--> This means grouping of data members and member functions into single unit called class. This also means grouping of related entities into single unit. For ex grouping of classes, interfaces and subpackages into single package.


2)Polymorphism-->This means one interface many forms.We can also so existing in many forms. This can be of two type:

a) Static Polymorphism--> This means specifying things at compile type. For example method overloading. We specify at compile time by passing arguments that which method to be called.

b)Dynamic Polymorphism--> This means specifying things at runtime. For example method overriding. Say for ex: a super class variable can have object of super class or any of its sub classes. If we call an overriden method, then its the method of the class whose object is referred by super class variable at runtime.


3)Inheritance--> It is the fundamental mechanism of creating a new class from an existing one. This way new class has properties of base class as well as new class. This is called is-a relation. This should be used where is-a relation is maintained throughout the lifecycle of the objects involved.


4)Aggregation-->This means creating a new class using existing class as datamember of the new class. This is called has-a relation.


5)Class--> A class is a blueprint or category of objects. A class specifies properties and behaviour of a an object. The properties of a class are called attributes and defined by fields in Java. The behaviour of a class is operations and defined by methods in Java. They both collectively are called members.


6)Object--> It is an instance of a class. It is constructed using blueprint of the class.


7)Object References--> Its same as object variable.


8)Static Members--> Static members belong to the class and not to a particular object.


9)Instance Members--> They belong to a particular object.Each object has its own copy of instance  member.


10) Static block--> This is the block contained in a class and is executed each time class is loaded. That is its called even if a class contains no main method and we execute the class.It is generally used to initialize static members.


11)Non Static Block--> It is called each time an object of a class is created. If static, non-static and constructor are there in the class, then they are executed in following order: static blok-->non-static block--> constructor.


There are two ways of defining a new class from an existing class, inheritance and aggregation.

Inheritance-->This means defining a new class from an existing one, which contains its own properties, apart of properties from a base class. Its an "is a relation" in uml notation.

Aggregation-->In this approach a new class is constructed using objects from other classes as constituent or members of this class.


12)Lexical tokens--> The low-level language elements are called lexical tokens and are used to construct more complex language constructs. identifiers, numbers, operators and special characters are called lexical tokens.

13)Keywords--> Keywords are reserved identifiers in a language and cannot be used to denote other entities.


14) Literal--> It denotes a constant value.

String Literal example>"Hello","world"


Primitive Datatypes and Literal Examples:

Integer Literal example-> 2,3

Floating Point Literal example-> 2.45,3.15

Boolean Literal example--> true, false

Character literal example-->'a','2','\u002'(four digit hexadecimal number from \u0000 to \uffff), '\121'(octal number from \0 to \377)

Long Literal example-->10l,20l

double literal example-->3.21d,5.54d

octal literal example-->012,07

Hexadecimal literal example-->0x1b,0x10

Thats all for the day !!!!


Search Amazon for Best Books on Java J2EE


blogarama - the blog directory

Search your favourite topics