This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why compiler is strict when I apply strictfp to cons....

 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tuesday, February 10, 2004
Hi Ranchers,
Why a constructor can not be declared strictfp?
class Funda{
strictfp Funda(){
int i= 123*12*234;
System.out.println(i);
}
public static void main(String args[]){
new Funda();
}
}
 
Jason Cox
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strictfp is for use with classes and methods.
The actual use of strictfp is very specific. If you need it to effect all floating-point experssions in your class, then it should be declared in the
class declaration
example -
public strictfp BasicClass {
}
Otherwise it can be applied to specific methods if you only need it in certain places
example -
public strictfp void basicMethod() {
}
However, declaring it in the constructor doesn't do any good. It would be the same as just declaring it in the class! Also, it would be impossible for a compiler to deal with one class that is created as FP-strict when another is not!
For example -
public strictfp BasicClass() {
}
public BasicClass() {
}
Either constructor would construct the same type of class, but one is FP-Strict and the other is not! That could cause serious problems. If you don't always want a class to be FP strict, then just applied the modifier at the method level.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the JLS says so, how's that for an explanation?
�15.4 FP-strict Expressions
If a class, interface, or method, X, is declared strictfp, then X and any class, interface, method, constructor, instance initializer, static initializer or variable initializer within X is said to be FP-strict.

also, you can't declare a variable as strictfp.
... but of course Rob's answer is much more thorough and informative -- nicely done Rob.
[ February 12, 2004: Message edited by: Jessica Sant ]
 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob and Jessica,
Sorry for the late reply. After reading the portions of the JLS told by the Jessica. I found that:
Every compile-time constant expression (�15.28) is FP-strict. If an expression is not a compile-time constant expression, then consider all the class declarations, interface declarations, and method declarations that contain the expression. If any such declaration bears the strictfp modifier, then the expression is FP-strict.
In the above code is think expression “123*12*234” is a strict-fp even if we apply strict-fp is applied or not.

quote:
Also, it would be impossible for a compiler to deal with one class that is created as FP-strict when another is not!
For example -
public strictfp BasicClass() {
}
public BasicClass() {
}
I think problem of selecting the class which is strict-fp and which is not never arises. Since we can have only one no-arg constructor. If we assume that the formal is excepted by the compiler then no default constructor will be there. What do u thing Rob.
quote: “The strictfp use is specific”
It seems to me that use of the strictfp is very general because even if I have to make one expression specific then I have to declare the class as strictfp or the method as the strictfp. Did I made any mistake in understanding what u wanted to say.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic