Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java String valueOf Method

by suresh

The Java String valueOf Method is one of the Java String Method which is used to return the string representation of the user specified value.

In this article we will show you the string representation of different data types using Java String.valueOf method with example. Before we get into the example, the basic syntax of the String.valueOf in Java Programming language is as shown below.

Java String valueOf Method syntax

The Java Programming Language provides nine different methods to return the String representation.

The following Java string valueof method will accept the Boolean value as the parameter and returns the string representation of Boolean value.

public static String valueOf(Boolean bool); // It will return String

//In order to use in program
String.valueOf(Boolean bool);

Following method accept the Character as the parameter and return the of char value in string representation.

public static String valueOf(char ch); // It will return String

//In order to use in program
String.valueOf(char ch);

This Java String valueOf method will accept the Character Array as the parameter. Next, it returns the string representation of Char array.

public static String valueOf(char[] anCharArray); // It will return String

//In order to use in program
String.valueOf(char[] anCharArray);

Following method will accept the Character Array as the first parameter, Starting index position (Starting_index) as the second argument and the array count as third argument. Java String.valueOf method will start representing from the Starting_index and return up to user specified count.

public static String valueOf(char[] anCharArray, int Starting_index, int count); // It will return String

//In order to use in program
String.valueOf(char[] anCharArray, int Starting_index, int count)

Following Java String valueOf method will accept the Integer value as the parameter & return string representation of Integer value.

public static String valueOf(int integer); // It will return String

//In order to use in program
String.valueOf(int integer);

This method will accept the Long value as the parameter and returns the string representation of Long value.

public static String valueOf(long l); // It will return String

//In order to use in program
String.valueOf(long l);

This Java string valueof method will accept the Double value as the parameter and returns string representation of Double value.

public static String valueOf(double dbl); // It will return String

//In order to use in program
String.valueOf(double dbl);

Following method will accept the Float value as the parameter and returns the string representation of Float value.

public static String valueOf(float fl); // It will return String

//In order to use in program
String.valueOf(float fl);

Following Java String valueOf method will accept the Object value as the parameter and returns the string representation of an Object.

public static String valueOf(Object obj); // It will return String

//In order to use in program
String.valueOf(Object obj);

Return Value

The Java String.valueOf Method accepts different types of arguments and returns the String representation of the user specified argument value.

Java String valueOf Method example

The Java String.valueOf Method is used to return the string representation of the user specified value.

In this Java program we will declare variables of different data types and return the string representation of those variables.

JAVA CODE

// Java String.valueOf Method example
package StringFunctions;

public class ValueOfMethod {
	public static void main(String[] args) {
		boolean bool = true;
		char c = 'T';
		char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
		int i = 99999;
		long l = 987654321;
		float flot = 10.25f;
		double dbl = 189.25987;
		String str = "India";
		
		System.out.println("String Representation of Boolean = " + String.valueOf(bool));
		System.out.println("String Representation of Character = " + String.valueOf(c));
		System.out.println("String Representation of Character Array = " + String.valueOf(ch));
		System.out.println("String Representation of Character Array = " + String.valueOf(ch, 5, 7));
		System.out.println("String Representation of Integer = " + String.valueOf(i));
		System.out.println("String Representation of Long = " + String.valueOf(l));
		System.out.println("String Representation of Float = " + String.valueOf(flot));
		System.out.println("String Representation of Double = " + String.valueOf(dbl));
		System.out.println("String Representation of an Object = " + String.valueOf(str));	
	}
}

OUTPUT

Java String valueOf Method 1

ANALYSIS

Within this Java String valueOf example, First we declared variable of available data types and assigned some random values using following statement

boolean bool = true;
char c = 'T';
char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
int i = 99999;
long l = 987654321;
float flot = 10.25f;
double dbl = 189.25987;
String str = "India";

Following System.out.println statement will call the public static String valueOf (boolean bool) method to return the string representation of Boolean value bool.

System.out.println("String Representation of Boolean = " + String.valueOf(bool));

Following System.out.println statement will call the public static String valueOf (char c) method. And return the string representation of Character c.

System.out.println("String Representation of Character = " + String.valueOf(c));

This Java string Valueof statement will call the public static String valueOf (char[] ch) method to return the string representation of Character array ch.

System.out.println("String Representation of Character Array = " + String.valueOf(ch));

The following statement calls the public static String valueOf (char[] ch, int Starting_Index, int count) method to return string representation of Character array ch. This will start looking for character at index position 5 (i.e., P) and count 7 characters (i.e., P, r, o, g, r, a, m), and then it will return the string representation of this sub array.

System.out.println("String Representation of Character Array = " + String.valueOf(ch, 5, 7));

Following Java string valueof statement will call the public static String valueOf (int i) method to return a string representation of integer value i.

System.out.println("String Representation of Integer = " + String.valueOf(i));

Following statement will call the public static String valueOf (long l) method to returns string representation of Long value l.

System.out.println("String Representation of Long = " + String.valueOf(l));

This statement will call the public static String valueOf (float fl) method to return the string value of Float value flot.

System.out.println("String Representation of Float = " + String.valueOf(flot));

Following Java String valueOf statement will call the public static String valueOf (double dbl) method to output string representation of Double value dbl.

System.out.println("String Representation of Double = " + String.valueOf(dbl));

Following Java String valueOf statement call the public static String valueOf (Object obj) method to return String value str.

System.out.println("String Representation of an Object = " + String.valueOf(str));

Thank You for Visiting Our Blog

Placed Under: Java Tutorial

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy