Monday, 13 July 2015

DETAIL JAVA ,VARIABLES,CONSTANTS,LOOPING STATEMENTS,CONDITIONAL STATEMENTS,ARRAYS



VARIABLE: It is a name given to a memory location, which can hold a value and that can be changed any number of times in future if required.
Adv:  Reusability & easy to maintain (future updations are easy) .
 NOTE: always use meaningful names.
Syntax: Datatypevariablename;
CONSTANT: It is a name given to a memory location, which can hold a value that can’t be changed in future.
Syntax:  final Datatypeconstantname=value;             if…. else
CONDITIONAL STATEMENTS :( decision making statements)
In java we have two types of conditional statements.
If …else
Switch… case
IF--- ELSE: whenever we want to execute one block of statements
among two blocks then we can use If..Else.
 Syntax: if(condition) {    block of stmts1    } else { block of stmts2 }
Note : condition is true then block of stmts1 will be executed
Switch…case: Whenever we want to execute a particular block of statements among many blocks then we will choose switch case statement.
 Syntax: switch(choice) {
case “c1”:
 block1
                   break;        
case “c2”:
                   block 2
                   break;
          -----------
          Default: ------ // not mandatory
}
LOOPING STATEMENTS: Looping statements are used for executing a certain block of statements repeatedly and continuously for some number of times.        
in java we have 3 types of looping statements
for loop
while loop
do…while loop
for loop: whenever we clearly know how many no of times the block should be repeated then use for loop.
syntax : for(initiation ;condition ;improvement)
          { 
---------------- block of stmts
 }
While loop: It is used for executing a block of statements repeatedly as long as the condition is being satisfied.
Syntax: while( condition) 
          { ------- block of stmts
          }
do---- while loop: It is used for executing a block of statements repeatedly as long as the condition is being satisfied, but first time execution will be done without checking the condition.
Syntax:  do {
 -------------block of stmts
} while(condition) ;
ARRAYS: Array is a special type of variable which can hold multiple values.
in java we have the following types of arrays
single Dimensional array
multi Dimensional array
For storing and retrieving values to and from array we will use index. Index starts with 0 and continues like 1.2.3……
Single Dimensional array
          Syntax: datatype[] arrayname= new datatype[size];   (OR)
          datatypearrayname[]= new datatype[size];
Multi Dimensionalarray : to store more no of multiple values very easily we should use  this array
Syntax: datatype[][] arrayname= new datatype[size][size];   (OR)
          datatypearrayname[][]= new datatype[size][size];
Object array:  it is used for storing multiple values of different datatypes
Syntax: Object[] arrayname= new Object[size];   (OR)
                      Object arrayname[]= new Object[size];

object array:
package pack1;

publicclass array {

         
          publicstaticvoid main(String[] args) {
                   Object a[]=new Object[6];
         a[0]="akhil";
         a[1]="sindu";
    a[4]=3.14;
         a[2]=100;   
         a[3]="haritha";
         a[5]='a';
         System.out.println(a[4]);   }
}
ArrayList class
Hashtable class
Note: ArrayListclass ,Hashtable class are predefined class of collection API in java.
ArrayListclass : it is used for simulating dynamically growing array.
Syntax: ArrayList<datatype>arrayname= new ArrayList<datatype>();
For retrieving values from this we will use index. Index starts with 0 and continues like 1.2.3……
To add data to this array we need to use the below syntax
Syntax: arrayname.add(data);
package array;

importjava.util.ArrayList;

publicclassarraylistclass {

         
          publicstaticvoid main(String[] args) {
                   ArrayList<String> a=newArrayList<String>();
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         System.out.println(a.get(2));
         }
}


ArrayList<String> a=newArrayList<String>();
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         System.out.println(a.get(2));
         System.out.println(a.size());
        

publicclass array {

         
          publicstaticvoid main(String[] args) {
                   ArrayList<String> a=newArrayList<String>();
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
        
         System.out.println(a.size());
         System.out.println(a.get(0));
         System.out.println(a.get(1));
         System.out.println(a.get(2));
         System.out.println(a.get(3));
         System.out.println(a.get(4));
         System.out.println(a.get(5));
         System.out.println(a.get(6));
         System.out.println(a.get(7));
        
        
        

}
}


For………………………………………………..


packagesetlist;

importjava.util.ArrayList;
importjava.util.List;

publicclass list {

         
          publicstaticvoid main(String[] args) {
                   ArrayList<String> a=newArrayList<String>();
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
         a.add("akhil");
         a.add("swapna");
         a.add("sindhu");
         a.add("haritha");
        
         System.out.println(a.size());
         for(int i=0;i<40;i++)
         {
         System.out.println(a.get(i));
        

                   }

}

SET: Set is an interface belongs to the collection API which is implemented by a class
HashSet: It is similar to ArrayList it will be dynamically growing but will hold only unique values (even though you add duplicate values but it will take only one value in to the set).
Syntax:Set<datatype>setname=newHashSet<datatype>();
          To add values in to a set we have the same add() method which is also available in the ArrayList.
Syntax: setname.add(data);
Note: here above data‘s data type must be set declaration’s data type
 To retrieve values form a set here we don’t have get() method as like in ArrayList, so for this concept we have an interface Iterator in java.
Syntax: Iterator<datatype>iteratorreference=setname.iterator();
Note: here Iterator’s data type &setname’s data type must be same.
          Iterator: Iterator is an interface belongs to the collection API which is used to handle sets, list and so on. Simply Iterator reference acts like a pointer.
Useful methods in Iterator interface for doing testing as follows
hasNext(): it checks whether Iterator reference has next element  available or not ,returns true if it has next element otherwise returns false.
next(): it gets element’s data from the location which is pointed by Iterator reference.



Set<String> l=newHashSet<>();
         l.add("a1");
         l.add("a2");
         l.add("a2");
         l.add("a3");
         l.add("a4");
         l.add("a");
         l.add("a1");
         l.add("a2");
         l.add("a2");
         l.add("a3");
         l.add("a4");
         System.out.println(l.size());
         Iterator<String>itr=l.iterator();
         for(int i=0;i<l.size();i++)
         {  
         System.out.println(itr.next());
        
         }
         l.add("a6");
         itr=l.iterator();
        
         System.out.println(l.size());
         for(int i=0;i<l.size();i++)
         {
         System.out.println(itr.next());
         }
         l.add("a7");
         itr=l.iterator();
        
         System.out.println(l.size());
         for(int i=0;i<l.size();i++)
         {
         System.out.println(itr.next());
         }
         l.add("a8");
         itr=l.iterator();
        
         System.out.println(l.size());
         for(int i=0;i<l.size();i++)
         {
         System.out.println(itr.next());
         }

}
}


Note:  here data is of same data type’s data by the time of declaration
To access(retrieve) data from this array we need to use the below syntax
Syntax: arrayname.get(indexno);
Hashtableclass :is used for simulating dynamically growing array.
For storing and retrieving values to and from this we will use keys instead of Index
Syntax: Hashtable<datatype1, datatype2>arrayname= new Hashtable<datatype1, datatype2>();
datatype1 is for keys & datatype2 is for values and datatype1 ,datatype2 are datatype’s class name
To add data to this array we need to use the below syntax
Syntax: arrayname.put(data, data);
Note:  here data is of same data type’s data by the time of declaration
To access(retrieve) data from this array we need to use the below syntax
Syntax: arrayname.get(key);
publicclasshashtable {

         
          publicstaticvoid main(String[] args) {
                   Hashtable<String,String> a=newHashtable<String,String>();
         a.put("akhil","hyd");
         a.put("swapna","hyd2");
         a.put("sindhu","tmnd");
         a.put("haritha","usa");
         a.put("swapna1","hhhhhh");
         a.put("sindhu1","kkkkk");
         a.put("haritha1","jjjjjj");
        
         System.out.println(a.size());
   
         System.out.println(a.get("haritha"));
         System.out.println(a.get("kkkkk"));
                            }
          }

No comments:

Post a Comment