Showing posts with label Revision. Show all posts
Showing posts with label Revision. 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.





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

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 !!!!

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

Monday, October 13, 2008

Revision Quiz

1. A ____ object can store and retrieve “value” objects indexed by “key” object.
Ans: Hashtable

2.Wrapper class objects contain mutable values.
True/False?
Ans: False

3.A Vector can hold object references or primitives types.
Ans: False

4.Hashtable class implements which of the following interfaces?Ans: Map

5.String s1=new String(“MyString”);String s2=new String(“MyString”);s1==s2 will return ___.

Ans: False

6.Float.NaN or Double.NaN refers to ___
Ans: Not a number

7.The java.lang.System is a ___ class.

Ans: final

8.The ___ class is a wrapper around int data type in java.

Ans: Integer

9.java.lang.Double is a concrete subclass of abstract class java.lang.Number.
True/False
Ans:True

10.What happens when you try to compile and run the following code snippet?
public class Wrapper{
public static void main(String []a)
{
String s=”15.25”;
try {
int number=Integer.parseInt(s); //line 1
System.out.println(number); //line 2
}
catch(NumberFormatException nfe)
{
System.out.println(“sorry”);
}
catch(Exception e){ }
}
}
A) It will compile fine and display 15 when run.
B) It will compile fine and display Sorry when run.
C) It will not compile.
D) It will compile fine nothing will be displayed when run
Ans: B

11.The java.lang.Math class has ___ constructor.

A) public
B) private
C) protected
Ans: B

12.A Vector maintains object references in the order they were added.
Ans: True

13.The String class represents a mutable string

Ans: False

14.The parent class of both Integer and Character class is Number class
Ans: False

15.What happens when you try to compile and run the following code snippet?

public class WrapChar
{
public static void main(String []a)
{
Character wrapChar=new Character(“c”);//line1
System.out.println(wrapChar);//line 2
}
}
A) Compile time error at line 1
B) Compile time error at line 2
C) Compile fine and display c when run
D) Compile fine but throws RuntimeException
Ans: A

Search Amazon for Best Books on Java J2EE

Blogarama

blogarama - the blog directory

Search your favourite topics

Google