Please disable adblock to view this page.

← Go home

Primitive Values to Strings in Java


April 30, 2017
Published By : Pratik Kataria
Categorised in:

Understanding through Code

import java.text.NumberFormat;

public class PrimitiveToString {
	public static void main( String[] args ){
		int intVal = 24;
		String intToStr = Integer.toString(intVal);

		boolean boolVal = true;
		String boolToStr = Boolean.toString(boolVal);

		long longVal = 12_400;
		NumberFormat formatter = NumberFormat.getNumberInstance();
		String longValFormatted = formatter.format(longVal);


  • 24
  • true
  • 12,400


  • 12_400 is same as 12400 for the compiler. The underscore helps to make it more readable.
  • Using underscore as separators was introduced in Java V7.
  • NumberFormat is part of different package and not java.lang. So we need to import the package.

Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.