Java Data Types - Numeric Data
Java Data Types - Numeric Data
Written by Ian Elliot   
Thursday, 06 October 2016
Article Index
Java Data Types - Numeric Data
Literals and Expressions
Numeric Output


Numeric Output

Before a number can be displayed it has to be converted to a string - more on strings in the next chapter.

Again beginners often have a problem with this idea because they don't realize that there is a need to convert the internal binary representation of a number into characters that can be displayed.  

The simplest way to get numeric output is to use the println or print methods of out

double myDouble=1.523454565;
int myInt=2;

 In each case the printlin method performs a default conversion of the numeric value to a string.

What if we want to control the conversion?

The answer is that you can use the format or printf methods in place of print or println.

The way that this works is that you specify a format and the variables to be printed. The rules for the format can be complicated but the first rule is that any regular character in the format is just displayed. So for example:

System.out.printf("this is my number",myDouble);

just displays "this is my number" the value in myDouble isn't displayed.

To get a the value in a variable displayed you have to include a format specifier which all start with %. How the number is displayed depends on the format specifier you choose.

For example:

System.out.printf("this is my number %f",myDouble);


this is my number 1.523455

Notice that the value of the variable is displayed where the format specifier occurs - the value replaces the format specifier in the output.

If you want to specify the number total number of characters and the digits after the decimal point to use then you can precede the f  by two numbers c.d which gives the total number of characters including the decimal point and fractional digits f, For example:

System.out.printf("this is my number %5.2f",

displays the floating point number using a total of 5 characters with 2 digits after the decimal point i.e.

this is my number  1.5

You can use other format specifiers for other numeric types and for general formatting:

  • %d is a decimal integer
  • %f is a floating point value
  • %n is a newline

If you add a leading + you get a sign printed, a comma includes a locale specific grouping character and a - left justifies the value in the space.

System.out.printf("this is my number %+-10.2f",myDouble);


this is my number +1.52

There are a lot more ways to format numbers and you should lookup the DecimalFormat class if you want to control the way values are displayed even more.

However when we are working with a GUI like Swing we generally want to convert the numeric values to strings before they are displayed and this is covered in more detail in the next chapter. 


Modern Java
With NetBeans And Swing



  1. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Java Data Types - Numeric Data 
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

  9. Java Data Types - Arrays And Strings
  10. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 
  11. Advanced OOP - Type, Casting, Packages
  12. Value And Reference 
  13. Java Lambdas, SAMs And Events


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.





or email your comment to:






Last Updated ( Tuesday, 14 November 2017 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.