Tuesday 24 June 2014

valueOf (Object obj)

public static String valueOf(Object obj)
Returns the string representation of the Object argument.

class Employee{
 int id;
 String name;
 
 Employee(int id, String name){
  this.id = id;
  this.name = name;
 }
 
 public String toString(){
  return id +"," + name;
 }
}

class StringValueOfObject{
 public static void main(String args[]){
  Employee e1 = new Employee(1, "Krishna");
  Employee e2 = new Employee(2, "Kishore");
  
  String str1 = String.valueOf(e1);
  String str2 = String.valueOf(e2);
  
  System.out.println("str1 = " + str1);
  System.out.println("str2 = " + str2);
 }
}

Output
str1 = 1,Krishna
str2 = 2,Kishore






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment