File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why compiler is strict when I apply strictfp to cons.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why compiler is strict when I apply strictfp to cons...." Watch "why compiler is strict when I apply strictfp to cons...." New topic
Author

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

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
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();
}
}


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Jason Cox
Ranch Hand

Joined: Jan 21, 2004
Posts: 287
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

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
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.
 
jQuery in Action, 2nd edition
 
subject: why compiler is strict when I apply strictfp to cons....