Sunday, August 26, 2012

ARRAYLIST in java


ArrayList implements all List operations using List interface and is also accept null values.The ArrayList is re sizable i.e shrinks and grows automatically and it has a capacity .

The class hierarchy is as follow:
---->Java.Lang.Object
      ----->Java.Util.AbstactCollection
                ----->Java.Util.AbstractList
                        ------>Java.Util.ArrayList

The array list contains following methods for implementing different operations



Return typeMethod name
voidadd (int index, Object o)
booleanadd (Object o)
booleanaddAll (Collection c)
booleanaddAll (int index,Collection c)
voidclear ( )
Objectclone ( )
booleancontains (Object o )
Objectget (int index)
intindexOf(Object o)
booleanisEmpty( )
int lastIndexOf( Object o)
Objectremove( int index)
Protected voidremoveRange( int fromIndex, int toIndex)
Objectset (int index, Object o)
intsize ( )
Object [ ]toArray( )
Object [ ] toArray (Object [ ] a)
voidtrimToSize( )




Wednesday, August 15, 2012

OPERATOR PRECEDENCE IN JAVA


Operators in Java are special symbols that executes specific operations on one, two, or three operand/operands, and then return a result.They moves to left to right or right to left.In the table bellow all the Java operators has been described with examples




Saturday, August 11, 2012

ACCESS MODIFIER IN JAVA



There are 4 access modifier in core Java. They are public, private, protected and default.
The table bellow shows the access of class with different modifier








Access Modifier table
     Visibility                                

PUBLIC  
    PROTECTED  DEFAULT PRIVATE
Access from the same class            YES                 YES YES YES
Acces from any class in same package YES YES YES NO
From subclass in same package YES YES YES NO
Access from subclass outside package YES YES through inheritance NO NO
Access from any non subclass outside package YES NO NO NO





JAVA KEYWORDS WITH EXPLANATION



There are near about 50 keywords ( reserved words) in core Java.The tables bellow demonstrate JAVA keywords with description and example and assigned alphabetically.Hope that you will find useful for learning or refreshing your memory......
Please inform me by posting comments if I am wrong...




keyword description example
Abstact used in class and method declaration.
Abstract class:
1) class must be declared with abstract keyword
2) methods in abstract class may or may not be abstract
3)If a class contains at-least one abstract method, the class must be declared as abstract.
4)Abstract methods must not be implemented in abstract class. Methods must be implemented in sub classes
5) An abstract class can not be instantiated, its solo mission is to be subclassed.

abstact class Myclass // abstract class
{
public abstact void do();// is a abstract method
public String getName()
{ }
}
Assert followed by a boolean value
(true or false), or an expression,
 to be evaluated at runtime, that
returns a  boolean value. If the boolean expression evaluates to false, then an AssertionError is thrown.

assert true;
assert 50 == 50;


// this will throw an Assertion Error:
int test = 50;
assert test == 100;

boolean is a data type which stores boolean
value, i.e True or False. It is also used
to declare methods that returns a value
which is also a boolean.
boolean myFlag = true;
breakOne of the control statement to
control the program flow. used to
break the loop of switch statement.
1) Unlabeled Break statement
2) Labeled Break Statement

1) Unlabeled Break statement

for(int num =0; num < 5 ; var++)
{      System.out.println(“Num is : “ + num);

  if(num == 3)
     break;
}


2)Labeled Break Statement

Back:

for(int num1=0; var1 < 10 ; num1++)
{
 for(int num2 = 1; num2 < 10; num2++)
 {             System.out.println(“num1:” + num1 + “, num2:” + num2);
     if(num1 == 5)
break Back;
 }
}
byte8 bit signed 2's complement integerbyte b = -1;
casecase is used in Switch statement
switch ( value/variable to test ) {
case value:    
your code_here;
break;
case value:
your code_here;
break;
default:
value/variable_not_above;
}
catchuse with try block to catching exceptiiontry
{ code here}
catch( exception )
{}


charcharacter type in unicode   char test ="y"
classimplementation of a particular kind
of object which defines instance,
class fields, methods,innner classes,interfaces as well as 
superclasses of class 

class Person {
private String name;
public Person(String myName) {
this.name = myName;
}
public void show() {
System.out.println(name);
}
}

classs Strudent extends Person
{
Pesron p = new Person();
p.show();
}
constis keyword in Java, but till now not used 
continueto skip the rest of the statement
in body of the loop and continue
with the next iteration of the loop
1)Unlabeled Continue
2)labeled Continue 

1)Unlabeled Continue 
for(int num1 =0; num1 < 15 ; num1++)
{
   for(int num2=0 ; num2 <1 5 ; num2++)
 {      if(num2 == 2)
           continue;
  System.out.println( num1 + num2);
 }
}


2) Labeled Continue
Back:
for(int num1 =0; num1 < 5 ; num1++)
{

 for(int num2=0 ; num2 < 5 ; num2++)
  {
       if(var2 == 2)
        continue Back;
 System.out.println( num1 + num2);

   }
}


defaultcan optionally be used in
"Switch case"  labelling a block
of statement which is to be
executed
 see case
douse with while to form "do-while"
loop uses boolean  expression to
execute block of statements

do {
     statement(s)
} while (expression);
double 64-bit double precision IEEE 754  floating-point number double mydouble = 0.05;
elseuse with "if " to form "if else" or "else if" statement tests boolean expression
int score = 79;
        char status;
        if (score >= 90) {
            status= 'A';
        } else if (score >= 80) {
            status = 'B';
        } else if (score >= 70) {
            status = 'C';
        } else if (score >= 60) {
            status = 'D';
        } else {
            status = 'F';
        }
        System.out.println("status = " + grade);
   
enumenumeration is a type whose  field consist of a fixed value of constant. It extends base class called Enum 
public enum Day {
 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}