Monday, 31 March 2014

Swapping numbers

                                         Normal swapping

public class Swap {
   
 public static void main(String[] args) {
  int a =90;
  int b =50;
  a = a+b;
  b = a-b;
  a =a-b;
  System.out.println("A value is "+a);
  System.out.println("B value is "+b);
}
}


                                                       Swapping through method


public class Swap
{
    public static void m1(int a,int b)
    {
        int x= a;
        a=b;
        System.out.println("The nassigned value of a is--->"+a);
        System.out.println("The nassigned value of b is--->"+b);
        a=x;
        System.out.println("Swaped value of a to b is-->"+a);
        System.out.println("Swaped value of b to a is-->"+b);
     }

 public static void main(String[] args) {
 
  m1(10, 20);
}
}

Tuesday, 25 March 2014

ConcurrentModificationException

http://way2java.com/exceptions/concurrentmodificationexception/

As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on).

public class IteratorDemo {
    public static void main(String args[]) {
        ArrayList al1 = new ArrayList();
        al1.add("Raju");
        al1.add("Reddy");
        al1.add("Rao");
        al1.add("Ratnakar");
        ListIterator it1 = al1.listIterator();
        while (it1.hasNext()) {
            System.out.print(it1.next()+" ");
            it1.add("Setty");
            //al1.add("Goud");
        }
    }
}
As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on). - See more at: http://way2java.com/exceptions/concurrentmodificationexception/#sthash.DD6Jznku.dpuf
As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on). - See more at: http://way2java.com/exceptions/concurrentmodificationexception/#sthash.DD6Jznku.dpuf
As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on). - See more at: http://way2java.com/exceptions/concurrentmodificationexception/#sthash.DD6Jznku.dpuf
As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on). - See more at: http://way2java.com/exceptions/concurrentmodificationexception/#sthash.DD6Jznku.dpuf
As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on). - See more at: http://way2java.com/exceptions/concurrentmodificationexception/#sthash.DD6Jznku.dpuf

Wednesday, 5 March 2014

SNMP Protocol

http://www.manageengine.com/network-monitoring/what-is-snmp.html

What is SNMP ?

           --  Simple Network Management Protocol (SNMP) is an application–layer protocol defined by the Internet Architecture Board (IAB) in RFC1157 for exchanging management information between network devices.
          -- SNMP is one of the widely accepted protocols to manage and monitor network elements.
          -- It is used for collecting information from, and configuring, network devices, such as servers, printers, hubs, switches, and routers on an Internet Protocol (IP) network.
         -- You can monitor network devices such as servers, workstations, printers, routers, bridges, and hubs, as well as services such as Dynamic Host Configuration Protocol (DHCP) or Windows Internet Name Service (WINS).
         -- Use SNMP management software to monitor any network device on which you install SNMP agent software
         -- Using SNMP, you can monitor network performance, audit network usage, detect network faults or inappropriate access, and in some cases configure remote devices.
         --  SNMP commands to read and write data in each device MIB. 'Get' commands typically retrieve data values, while 'Set' commands typically initiate some action on the device.
         -- For example, a system reboot script is often implemented in management software by defining a particular MIB attribute and issuing an SNMP Set from the manager software that writes a "reboot" value into that attribute.

      
SNMP consists of


SNMP Manager --
                -- 

toString() method

-- If you want to represent any object as a string, toString() method comes into existence.

If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Advantage of the toString() method

By overriding the toString() method of the Object class, we can return values of the object, so we don't need to write much code.

Understanding problem without toString() method

Let's see the simple code that prints reference.
  1. class Student{  
  2.  int rollno;  
  3.  String name;  
  4.  String city;  
  5.   
  6.  Student(int rollno, String name, String city){  
  7.  this.rollno=rollno;  
  8.  this.name=name;  
  9.  this.city=city;  
  10.  }  
  11.   
  12.  public static void main(String args[]){  
  13.    Student s1=new Student(101,"Raj","lucknow");  
  14.    Student s2=new Student(102,"Vijay","ghaziabad");  
  15.      
  16.    System.out.println(s1);//compiler writes here s1.toString()  
  17.    System.out.println(s2);//compiler writes here s2.toString()  
  18.  }  
  19. }  
Output:Student@1fee6fc
       Student@1eed786
As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let's understand it with the example given below:

Example of toString() method

Now let's see the real example of toString() method.
  1. class Student{  
  2.  int rollno;  
  3.  String name;  
  4.  String city;  
  5.   
  6.  Student(int rollno, String name, String city){  
  7.  this.rollno=rollno;  
  8.  this.name=name;  
  9.  this.city=city;  
  10.  }  
  11.    
  12.  public String toString(){//overriding the toString() method  
  13.   return rollno+" "+name+" "+city;  
  14.  }  
  15.  public static void main(String args[]){  
  16.    Student s1=new Student(101,"Raj","lucknow");  
  17.    Student s2=new Student(102,"Vijay","ghaziabad");  
  18.      
  19.    System.out.println(s1);//compiler writes here s1.toString()  
  20.    System.out.println(s2);//compiler writes here s2.toString()  
  21.  }  
  22. }  
Output:101 Raj lucknow
       102 Vijay ghaziabad

Equals() method

Note -- ‘equals()’ method check for the content equality
              Operator will check object.. whether object is same or not


public class EqualsMethod {
    public static void main(String[] args) {
        String s1 = new String("bala");
        String s2 = new String("bala");
        if (s1.equals(s2)) // using equals method
        {
            System.out.println("Content are same -- TRUE");
        } else {
            System.out.println("Content are different -- FALSE");
        }
        if (s1 == s2) // using operator
        {
            System.out.println("Object is same -- TRUE");
        } else {
           System.out.println("Object is different -- FALSE");
        }
    }
}