HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Keywords / strictfp modifier in Java

strictfp modifier in Java

In Java, floating point representations and computations are platform dependent. strictfp modifier ensures that all floating-point operations across different JVMs and platforms will provide consistent and same result predicted by IEEE 754.

When we use strictfp, JVM performs floating-point computations using values that can be represented by a standard Java float or double, guaranteeing that the result of the computations will match exactly across all JVMs and platforms.

1. How to use strictfp modifier

The strictfp modifier can be used with –

  1. class – All code in the class (instance, variable, static initializers), and code in nested classes will use strictfp computations.
  2. method – All code within method will use strictfp computations.
  3. interface – All code in any class that implements the interface will use strictfp computations.

strictfp keyword cannot be used with abstract classes and method.

Please nore that strictfp behavior is not inherited by a subclass that extends a strictfp superclass. An overriding method can independently choose to be strictfp while the overridden method is not, or vice versa.

2. Java strictfp example

Let’s see an example of strictfp modifier for floating point comparisons which will yield exactly same result in all the platforms and processor architectures.

In given example, we have used Double.MAX_VALUE value which is a very big number and has different representations in each platform. Using strictfp, we can ensure that given calculation will always result in same value.

public strictfp class Main 
{
	public static void main(String[] args) 
	{
		double MAX = Double.MAX_VALUE;
		
		System.out.println(Double.MAX_VALUE - 1);
	}
}

Program output.

1.7976931348623157E308

Drop me your questions related to strictfp keyword in Java.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Vikram Bammidi

    June 19, 2019

    Hello Thank you for explanation,

    How this strictfp results platform independent. could you please explain more.

    Thanks,
    Vikram.

    • Vikram Bammidi

      June 19, 2019

      I missed that example , i got it.

      Thank you.

Comments are closed on this article!

Search Tutorials

Java Keywords

  • Java Keywords
  • Java abstract
  • Java assert
  • Java boolean
  • Java final, finally, finalize
  • Java this, super
  • Java static
  • Java strictfp
  • Java synchronized
  • Java transient
  • Java yield

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)