Convert primitives to String type with valueOf method - Java Language Basics

Java examples for Language Basics:Primitive Types

Introduction

  • static String valueOf(int i) Converts argument int to String
  • static String valueOf(float f) Converts argument float to String
  • static String valueOf(long l) Converts argument long to String
  • static String valueOf(double i) Converts argument double to String
  • static String valueOf(char c) Converts argument char to String
  • static String valueOf(boolean b) Converts argument boolean to String
  • static String valueOf(Object o) Converts argument Object to String

Demo Code

 
public class Main {
 
  public static void main(String args[]){
 
  int i=110;//from   w ww .  ja v  a 2s  .c o  m
  float f = 110.0f;
  long l = 110;
  double d=110.0d;
  char c='a';
  boolean b = true;
  Object o = new String("Hello World");
 
  /* convert int to String */
  System.out.println( String.valueOf(i) );
  /* convert float to String */
  System.out.println( String.valueOf(f) );
  /* convert long to String */
  System.out.println( String.valueOf(l) );
  /* convert double to String */
  System.out.println( String.valueOf(d) );
  /* convert char to String */
  System.out.println( String.valueOf(c) );
  /* convert boolean to String */
  System.out.println( String.valueOf(b) );
  /* convert Object to String */
  System.out.println( String.valueOf(o) );
 
  }
 
}

Result


Related Tutorials