java.lang.String valueOf(Object obj)

Description :

This java tutorial shows how to use the valueOf(Object obj) method of String class of java.lang package. This method returns a string representation of the Object obj specified as method argument. To make it simple it is only simple call to the Object.toString() method.

Method Syntax :

public static String valueOf(Object obj)

Parameter Input :

DataType Parameter Description
Object obj The object parameter we are interested to get the String equivalent

Method Returns :

The method valueOf(Object obj) returns a String object which the equivalent of the toString() of Object. If the Object is null, the return value will be null.

Compatibility Version :

Since the beginning

Exception :

None

Discussion :

The java method, String valueOf(Object obj) should be accessed statically thus we should do the following String.valueOf(Object obj). The toString() method is the String representation of Object class. The toString() method is advised to be overridden to give a more sensible value of our Object.

Java Code Example :

This java example source code demonstrates the use of valueOf(Object obj) method of String class. We have used the Employee class to demonstrate how to override the toString() method of Object class which our Employee class inherited automatically.

StringValueOfDemo.java

package com.javatutorialhq.java.tutorial.string;

import com.teknoscope.tutorial.utilities.Employee;

/*
* Example source code to show the use of static method valueOf(Object obj)
* We have used the Employee class to serve as our Object
* The employee class has overriden method of toString()
*/

public class StringValueOfDemo {

	public static void main(String[] args) {

		// Instantiate a new Employee class
		Employee emp = new Employee("Will Smith|30|January 12, 1983|Single|USA");
		// Getting the toString() value of Employee class
		String objString = String.valueOf(emp);
		// Printing the String equivalent of Employee class
		System.out.println(objString);
	}

}

Employee.java

package com.teknoscope.tutorial.utilities;

/**
* This sample source code shows
* how to override the toString() method of Object class
* This is the object template
* Property of teknoscope.com
* All Rights Reserved
* Version 1.0
* 07/08/2013
*/

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Employee extends Object {
	private String name;
	private int age;
	private String birthdate;
	private String status;
	private String location;
	public Employee(){

	}
	public Employee(String information) {
		super();
		List alist = new ArrayList();
		StringTokenizer st = new StringTokenizer(information, "|");
		while(st.hasMoreTokens()){
			alist.add(st.nextToken());
		}
		this.setName(alist.get(0));
		this.setAge(Integer.parseInt(alist.get(1)));
		this.setBirthdate(alist.get(3));
		this.setStatus(alist.get(2));
		this.setLocation(alist.get(4));
	}

	public void setName(String name){
		this.name = name.toUpperCase();
	}
	public String getName(){
		return this.name;
	}
	public String getBirthdate() {
		return birthdate;
	}
	public void setBirthdate(String birthdate) {
		this.birthdate = birthdate;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String toString(){
		return this.name+"-"+this.age;
	}

}

Sample Output :

Running the valueOf() method example source code of java String class will give you the following output

java string indexOf method example

java string indexOf method example

Exception Scenario :

Not Applicable

Suggested Reading List :

References :