CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Floating point in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Floating point in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 2 min read ยท 0 Comments
Share: in X

In Java, the float and double data types are used to represent floating-point numbers, which are numbers with a fractional part. In this post, we will learn the detail of floating point in Java.

Table of Contents

float Data Type
double Data Type
Arithmetic Operations
Floating-Point Literals
Precision and Rounding Errors

float Data Type

The float data type is a 32-bit floating-point type in Java. It is denoted by the keyword float. It can represent decimal numbers with a precision of about 6-7 significant digits.

When declaring a float variable, you append an f or F suffix to the value to indicate it as a float. For example:

float pi = 3.14f;

double Data Type

The double data type is a 64-bit floating-point type in Java. It is denoted by the keyword double. It provides higher precision compared to float, with about 15 significant digits.

By default, floating-point literals without a suffix are treated as double.

For example:

double pi = 3.14;

Arithmetic Operations

  • Floating-point numbers support standard arithmetic operations, including addition (+), subtraction (-), multiplication (*), and division (/).
  • Floating-point arithmetic follows the IEEE 754 standard for floating-point operations, but it may have limitations due to the nature of representing real numbers with finite precision.

Floating-Point Literals

Floating-point literals in Java are represented using decimal notation. You can include a decimal point and/or an exponent to specify fractional or scientific notation. For example:

double number1 = 3.14;
double number2 = 1.23e-4;  // Scientific notation (1.23 x 10^-4)

Precision and Rounding Errors

  • Floating-point numbers are inherently prone to rounding errors due to their finite precision.
  • Some decimal numbers cannot be represented exactly in binary floating-point format, leading to small discrepancies in calculations.
  • If you require exact decimal calculations or need higher precision, consider using BigDecimal

Frequently Asked Questions

What are floating-point numbers in Java?

Floating-point numbers in Java are used to represent real numbers with fractional parts. They include types like float and double.

How many types of floating-point numbers are there in Java?

Java supports two floating-point types: float, which is a 32-bit single-precision type, and double, which is a 64-bit double-precision type.

What is the default value of an uninitialized floating-point variable in Java?

The default value for uninitialized float and double variables in Java is 0.0.

What is the difference between float and double in Java?

double has greater precision and a larger range compared to float. It’s the preferred choice for most floating-point calculations.

What are some common issues with floating-point arithmetic in Java?

Common issues include precision loss, rounding errors, and problems comparing floating-point numbers for equality due to their binary representation.

Related Posts:

  • Primitive data types in Java
  • Literals in Java
  • final Keyword in Java
  • Integers In Java
  • Default Variable Initialization in Java
  • Variable in Java
Tags:java
Was this article helpful?
โ† Previous ArticleIntegers In Java
Next Article โ†’Character data type in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap