- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to convert a Primitive Type Value to a String
For converting a primitive type value to a string in Java, use the valueOf() method.Let’s say we want to convert the following double primitive to string.
double val4 = 8.34D;
For that, use the valueOf() method. It converts it into a string.
String str4 = String.valueOf(val4);
Example
public class Demo { public static void main(String[] args) { int val1 = 5; char val2 = 'z'; float val3 = 9.56F; double val4 = 8.34D; System.out.println("Integer: "+val1); System.out.println("Character: "+val2); System.out.println("Float: "+val3); System.out.println("Double: "+val4); String str1 = String.valueOf(val1); String str2 = String.valueOf(val2); String str3 = String.valueOf(val3); String str4 = String.valueOf(val4); System.out.println("
Converted to string..."); System.out.println(str1); System.out.println(str2); System.out.println(str3); System.out.println(str4); } }
Output
Integer: 5 Character: z Float: 9.56 Double: 8.34 Converted to string... 5 z 9.56 8.34
- Related Articles
- Java Program to convert a string into a numeric primitive type using Integer.valueOf()
- Java Program to convert String to short primitive
- Convert double primitive type to a Double object in Java
- Java Program to convert a String to a float type Number
- Convert short primitive type to Short object in Java
- Convert byte primitive type to Byte object in Java
- Java Program to create a BigDecimal from a string type value
- Program to convert Primitive Array to Stream in Java
- Convert a String to a double type number in Java
- Java Program to convert an int value to String
- How to convert a CLOB type to String in Java?
- Java Program to convert Java Float to Numeric Primitive Data Types
- Java Program to convert Double to numeric primitive data types
- Java Program to convert a String to int
- Java Program to convert a String to Date

Advertisements