AppDividend
Latest Code Tutorials

Java String valueOf() Example | valueOf() Function In Java

0

Java String valueOf() Example | valueOf() Function In Java is today’s topic. Java String valueOf() method converts the different types of values (data types) into string type. This method is static, it can take up to two parameters, one is the String, and the other is the radix.

Java String valueOf()

Java String valueOf() method returns a string representation of the int, long, float, boolean, char, char array, and double arguments. We have different versions of the valueOf() method for each type of argument.

Variants

public static String valueOf(boolean b):

Converts a boolean value to a String.

public static String valueOf(char c):

Converts char to String.

public static String valueOf(int i):

Converts int to String.

public static String valueOf(long l):

Converts long to String.

public static String valueOf(float f):

Converts float to String.

public static String valueOf(double d):

Converts double to String.

public static String valueOf(String s, int radix):

Returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.

Programming Example

See the following code.

class valueof {
	public static void main(String args[]) {
		int num = 123;
		String s = "45";
		// converting num to a string data type
		String str = String.valueOf(num);
		System.out.println("Value after concatenating is: " + str + s);
	}
}

See the output.

 

Programming Example

On the above example, we are trying to concatenate an integer value with a string.

But to do so, firstly, we have to convert that integer value to a string, and after that, only we can operate. So we did the same, and we are getting the output of it.

See another code.

class valueof {
	public static void main(String args[]) {
		int i = 100; // int value
		float f = 10.10f; // float value
		long l = 111L; // long value
		double d = 2222.22; // double value
		char ch = 'A'; // char value
		char array[] = { 'a', 'b', 'c' }; // char array

		// converting int to String
		String str1 = String.valueOf(i);
		// converting float to String
		String str2 = String.valueOf(f);
		// converting long to String
		String str3 = String.valueOf(l);
		// converting double to String
		String str4 = String.valueOf(d);
		// converting char to String
		String str5 = String.valueOf(ch);
		// converting char array to String
		String str6 = String.valueOf(array);
		// converting using radix value
		Integer b = Integer.valueOf("444", 16);

		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
		System.out.println(str4);
		System.out.println(str5);
		System.out.println(str6);
		System.out.println(b);

	}
}

See the output.

 

valueOf() Function In Java

Java String valueOf()(boolean bol) method example

See the following program.

public class StringValueOfBool {
	public static void main(String[] args) {
		// Boolean to String
		boolean bolA = true;
		boolean bolB = false;
		String s1 = String.valueOf(bolA);
		String s2 = String.valueOf(bolB);
		System.out.println(s1);
		System.out.println(s2);
	}
}

See the output.

➜  java java StringValueOfBool
true
false
➜  java

Java String valueOf(char ch) Method Example

Let’s pass the char ch value to the valueOf() method.

It takes the char value and returns the string. Let’s see an example.

public class StringValueOfBool {
	public static void main(String[] args) {
		// char to String
		char ch1 = 'K';
		char ch2 = 'B';
		String s1 = String.valueOf(ch1);
		String s2 = String.valueOf(ch2);
		System.out.println(s1);
		System.out.println(s2);
	}
}

See the output.

➜  java javac StringValueOfBool.java
➜  java java StringValueOfBool
K
B
➜  java

Conclusion

With the help of string valueOf() function, you can convert long to string, int to string, character to string, boolean to string, float to string, double to string, object to string and char array to string.

Finally, the Java valueof() method example is over.

Recommended Posts

Java String getchars() Method

Java String Equals Example

Java String Contains Example

Concat() In Java Example

Java String endsWith() Function

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.