Showing posts with label Java Fundamentals. Show all posts
Showing posts with label Java Fundamentals. Show all posts

Sunday, August 9, 2009

Imortant Revision Notes for Classes and Constructors in Java

Class Declaration--> Syntax of class declaration is as follows:

(class modifiers) class (class name)
               (extends clause) (implements clause)  // Class header

{ // Class body
    -- field declarations
     --method declarations
     --nested class declarations
     --nested interface declarations
     --constructor declarations
     --initializer blocks
}

Methods of a Class--> They are the class members and are also called operations. They define behaviour of the class. 

Syntax of their declaration is as follows:

(method modifiers) (return type) (method name) ((formal parameter list))
         (throws clause) // Method prototype


{ // Method body
    --local variable declarations
    --nested local class declarations
    --statements
}

Method Overloading--> This means several methods share same name but have different argument list.

Constructor--> It is a special method which is used to initialize state of an object when it is created using new operator. 

It has following syntax:

(accessibility modifier) (class name) ((formal parameter list))
            (throws clause) // Constructor header
  { // Constructor body
    --local variable declarations
    --nested local class declarations
    --statements
  }

However there are few constraints for a method to be constructor:
a) Their modifiers can be only accessibility modifiers.
b) They cannot have a return type
c) Their should be same as the class name.

Default Constructor--> It is the constructor with no argument. Its syntax is: classname()

Implicit Default Constructor-->If no constructor is defined in the class, then implicit default constructor is provided by java.

Its syntax is:

classname(){super();}

Sunday, July 12, 2009

Important Points to Remember in Java

Method Parameters:
Types of Method Parameters in Java:

a) Formal Parameters-->They are the parameters defined in the method definition.


b) Actual Parameters--> They are the parameters defined in the method call.


c) Final parameter--> If a formal parameter is declared to be final, then its value cannot be changed during its lifetime in the method body.



Note: Widening, narrowing, type promotion rule applies to the parameter passing as well. They are equivalent to assignment conversions.


***************************************
2. Arrays: 


a) Array--> Its a collection of homogeneous data elements.

b) Array Declaration-->Syntax for array declaration is as follows:


    (datatype)[](arrayname) or (datatype) (arrayname)[]

c) Array Construction--> This means allocating space to the array in computer memory. Its syntax is:


  (arrayname)=new (datatype)[(arraysize)]

d) Value assignment--> This means assigning values to the array elements.

e) Array Initialization--> Java provides the means of declaring, constructing and explicitly initializing an array in one declaration. Its syntax is:


(datatype)[](arrayname)={(array initialization list)};








*******************************


3. Anonymous Array--> They are used to initialize the array objects which are already declared. like:
   int a[];
 a=new int[]{1,5,5,3,9,7,3}; we cannot use a={1,5,5,3,9,7,3}; to initialize an array already declared.





********************************

Thursday, December 13, 2007

Notes on Java Fundamentals

Basic Lexical elements----
--->keywords & other reserved words

*******************************************************************
1. Access modifiers - private,public,protected
2.other modifiers---abstract,final,static,synchronized,transient, volatile
3 Name space keywords -- import,package

*******************************************************************
byte,short,int,long-- signed two's complement integers
boolean -- 1 bit
char-- 16 bit unicode characters rather than ASCII set used in C

********************************************************************
Formula ------2^(n-1) to 2^(n-1)-1`

********************************************************************

Exceptional Floating point constants---

Float.NAN
(NAN Stands for Not A Number)
Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Double.NAN
Double.NEGATIVE_INFINITY
Double.POSITIVE_INFINITY

************************************************************************

char represented in single quotes -
'\u0000'--- followed by four hexadecimal

************************************************************************

Integral can be decimal, hexadecimal or octal
Hexadecimal starts with 0x
Octals start with 0

************************************************************************
Floating point e or E stands for exponential value
eg 7e3 =7000

*************************************************************************

note--> double x=77; is valid declaration as doublex =77D;

*************************************************************************

To convert boolean, characters,integers,floating point numbers to String use valueOf() method
String is not null terminated.

**************************************************************************

Once initialized string objects are inmutable i.e cannot change their contents. so use stringBuffer if the contents have to be changed.

****************************************************************************

String bounds checked at run time rather than compile time.

string abc ="sdefg";
is not same as
string abc =new("string");

if we use first one compiler scans current cache to check for another string object with same literal value.
If it is there it reuses same object.

********************************************************************************

Search Amazon for Best Books on Java J2EE

Blogarama

blogarama - the blog directory

Search your favourite topics

Google