{"id":89380,"date":"2024-04-01T18:00:48","date_gmt":"2024-04-01T12:30:48","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89380"},"modified":"2024-04-01T18:00:48","modified_gmt":"2024-04-01T12:30:48","slug":"java-strictfp-keyword","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/","title":{"rendered":"Java Strictfp Keyword with Examples"},"content":{"rendered":"<p>In order to ensure that any floating point operation results in an identical result for all platforms, strictfp is used. In the case of floating points, precision can vary from one point to another. Consistency is guaranteed on every platform by the strictfp keyword.<\/p>\n<p>strictfp can be applied to a class, method, or interface, but cannot be applied to abstract methods, variables, or constructors.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<h2>Below are valid examples of using strictfp Keyword in Java<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">strictfp class Test {\n          }\n\nstrictfp interface Test {\n          }\n\nclass Techvidvan {\n          strictfp void Test() {\n          }\n          }<\/pre>\n<h3>Illustration 1: Keyword usage with classes<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">strictfp class Test{\n \n     \/\/ All concrete methods here are implicitly strictfp.    \n       }<\/pre>\n<h3>Illustration 2: Keyword usage with Interfaces<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">strictfp interface Test {\n  \n        \/\/ All  methods here becomes implicitly \n       \/\/ strictfp when used during inheritance.    \n       }\n\n       class TechVidvan{\n       \/\/ strictfp applied on a concrete method \n        strictfp void calculateSpeed(){}\n        }<\/pre>\n<h3>Illustration 3: Keyword usage with variables:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">strictfp interface Test {\n double sum();\n\n \/\/ Compile-time error here\n strictfp double mul(); \n }<\/pre>\n<h3>The following conclusions can be drawn from the above illustrations:<\/h3>\n<p>All methods defined in the class interface and all nested types declared in that class are usually strictfp by default when a class or interface is declared with an explicit modifier. It is not possible to use abstract methods with strictfp. In addition, it may be used with abstraction classes and interfaces. The strictfp is not allowed to use any method inside an interface due to the implicit abstraction of interfaces. The strictfp keyword is not a specified reword in Java 17.<\/p>\n<h3>From the examples in the table, it can be concluded that:<\/h3>\n<p>When a class or interface is specified with theStrictfp modifier, all methods declared in classesinterface.class and each nested type defined in these classes will be strictlyfpn by default. It is not possible to use an abstract method with strictfp. However, it is possible to use this function with an abstract class interface. Due to the implicit abstract nature of interface methods, strictfp cannot be employed for any method in an interface.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class TechVidvan{\n    public strictfp double sum()\n    {\n        double num1 = 10e+10;\n        double num2 = 6e+08;\n        return (num1 + num2);\n    }\n    public static void main(String[] args)\n    {\n        TechVidvan t = new TechVidvan();\n        System.out.println(t.sum());\n    }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n1.006E11<\/p>\n<h3>Legal code for Java strictfp keyword:<\/h3>\n<p>For methods, classes and interfaces it is possible to use the strictfp keyword.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">strictfp class A{}\/\/strictfp applied on class  \n   strictfp interface M{}\/\/strictfp applied on interface  \n   class A{  \n   strictfp void m(){}<\/pre>\n<h3>Illegal code for Java strictfp keyword:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Abstract methods, variables and constructors are not supported by the strictfp keyword.\n      class B{  \n      strictfp abstract void m();\/\/Illegal combination of modifiers  \n      }  \n      class B{  \n      strictfp int data=10;\/\/modifier strictfp not allowed here  \n      }  \n      class B{  \n      strictfp B(){}\/\/modifier strictfp not allowed here  \n      }<\/pre>\n<h3>When to Use?<\/h3>\n<p><strong>If we care a lot about deterministic behavior of all FPFP calculations, Java&#8217;s strict keyword comes in handy:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public void whenMethodOfstrictfpClassInvoked_thenIdenticalResultOnAllPlatforms()\n{\nScientificCalculator calculator = new ScientificCalculator(); \ndouble result = calculator.sum(23e10, 98e17);\nassertThat(result, is(9.800000230000001E18)); \nresult = calculator.diff(Double.MAX_VALUE, 1.56); \nassertThat(result, is(1.7976931348623157E308)); \n}<\/pre>\n<ul>\n<li>The test case will be passed to all hardware platforms as this keyword is being used by the ScientificCalculator class.<\/li>\n<li><strong>Note:<\/strong> If we are not using this feature, JVM will be allowed to apply a greater degree of accuracy on the target platform hardware.<\/li>\n<li>The system which performs a high level of sensitivity medical calculations is a popular mainstream use case for it.<\/li>\n<\/ul>\n<h3>Java strictfp usage:<\/h3>\n<ul>\n<li>We can use the strictfp keyword as a non-access modifier for classes, non-abstract methods or interfaces.<\/li>\n<li>When we declare an interface or class with strictfp, all its member methods and other nested types inherit its behavior.<\/li>\n<li>Note, however, that the strictfp keyword is not allowed on variables, constructors, or abstract methods.<\/li>\n<li>Please note that in variables, constructors and abstract methods we are not allowed to use the strictfp keyword.<\/li>\n<\/ul>\n<h3>Advantages of Strictfp in Java:<\/h3>\n<p>In Java, the advantages of Strictfp The benefits related to this keyword are shown at the bottom. From a developer&#8217;s point of view,<\/p>\n<p><strong> There are many advantages to applying strictfp; some of them can be found below:<\/strong><\/p>\n<ul>\n<li>The precision of the floating point on the various machines. strictfp uses precision and speed for floating-point operations. For 32 and 64 bit, it is totally unbiased with a clear focus on improved results.<\/li>\n<li>Finally, using this keyword has no downside, no increase in time compression or execution speed.<\/li>\n<li>It is important to understand that without the strictfp keyword and its function, floating-point output can be manipulated according to the precision point of the target machine&#8217;s CPU by the JVM and JIT compiler.<\/li>\n<li>Just another way of ensuring that Java programming fundamentals, &#8220;write once run everywhere,&#8221; aren&#8217;t duplicated and don&#8217;t turn out badly around the globe except a source machine.<\/li>\n<\/ul>\n<h3>Rules to Remember:<\/h3>\n<p>Like any other keyword in any programming language, strictfp in Java has its uses and rules and to achieve the intended results, rules specified must be followed. Before we start to use that keyword, let us have a look at some of the rules.<\/p>\n<ul>\n<li>The Strictfp keyword cannot be used by constructors; it&#8217;s working fine for classes, interfaces and methods.<\/li>\n<li>It will implicitly implement strictfp for all methods and nested types when it is declared with an interface or a class.<\/li>\n<li>An abstract class or interface, but not abstract methods, may be used to implement strictfp.<\/li>\n<li>The strictfp function cannot be applied with variables in addition to the constructors; refer to the sample code below.<\/li>\n<li>The correct use of strictfpnh will be ensured, with the avoidance of a possible difference between floatpoint accuracy which gives different results, in accordance with those rules.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We understood what a Strictfp Keyword in Java is, and then we explored the uses and were not to use this keyword. In accordance with the IEE 754 float point specification, it has been implemented in JVM 1.2. We&#8217;ve demonstrated the strictfp keyword with syntax, and we&#8217;ve implemented it in the examples. It&#8217;s a modification that will give you the same results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to ensure that any floating point operation results in an identical result for all platforms, strictfp is used. In the case of floating points, precision can vary from one point to another.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":89448,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,5427,5428,327,250,5429,5430,5431],"class_list":["post-89380","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-strictfp-keyword","tag-java-strictfp-keyword-with-examples","tag-java-tutorials","tag-learn-java","tag-strictfp-keyword","tag-strictfp-keyword-in-java","tag-strictfp-keyword-in-java-with-examples"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Strictfp Keyword with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java strictfp can be applied to a class, method, or interface but cannot be applied to abstract methods, variables, or constructors.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Strictfp Keyword with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java strictfp can be applied to a class, method, or interface but cannot be applied to abstract methods, variables, or constructors.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-01T12:30:48+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Strictfp Keyword with Examples - TechVidvan","description":"Java strictfp can be applied to a class, method, or interface but cannot be applied to abstract methods, variables, or constructors.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/","og_locale":"en_US","og_type":"article","og_title":"Java Strictfp Keyword with Examples - TechVidvan","og_description":"Java strictfp can be applied to a class, method, or interface but cannot be applied to abstract methods, variables, or constructors.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-04-01T12:30:48+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Strictfp Keyword with Examples","datePublished":"2024-04-01T12:30:48+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/"},"wordCount":818,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#primaryimage"},"thumbnailUrl":"","keywords":["java","java strictfp keyword","java strictfp keyword with examples","java tutorials","Learn Java","strictfp keyword","strictfp keyword in java","strictfp keyword in java with examples"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/","url":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/","name":"Java Strictfp Keyword with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#primaryimage"},"thumbnailUrl":"","datePublished":"2024-04-01T12:30:48+00:00","description":"Java strictfp can be applied to a class, method, or interface but cannot be applied to abstract methods, variables, or constructors.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-strictfp-keyword\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Strictfp Keyword with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89380"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89380\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}