在Java中表示浮点值 [英] Representing float values in Java

查看:163
本文介绍了在Java中表示浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的三行代码。

  float f = 1; 

float g = 1.1;

float h = 1.1f;

第二行有编译错误,而其他行没有编译错误。第一行工作正常,没有后缀f,第三行是后缀f。为什么会这样?

解决方案

Java中的浮点文字是 double 默认值。


JLS 3.10.2浮点文字



浮点文字是一种类型如果后缀为ASCII字母 F f ,则 float >;否则它的类型是 double ,它可以选择后缀为ASCII字母 D d


您不能指定 double 值赋予 float ,而不需要明确的缩小转换。因此,您有两个选项:


  • 对于文字,使用后缀 f F 来表示一个 float

  • 对于非文字,转换为(float)



后者的一个例子是:

  double d = 1.1; 
float f =(float)d; //编译好!






扩展转换



为什么这样编译:

  float f = 1; 

是因为从 int
$ b


float 可以隐式地在赋值语境中完成。 =http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2 =noreferrer> JLS 5.2作业转换



赋值转换在将表达式的值分配给变量时发生:表达式的类型必须转换为变量的类型。赋值上下文允许使用以下之一:
$ b $ ul

  • 扩展原始转换(§5.1.2)
  • [...]



  • JLS 5.1.2扩展原始转换



    以下19个原始类型上的特定转换被称为扩展原始转换:


    • int code>至 long float double

    • [b]






    其他数据类型后缀为文字



    如上所述,还有 D d 后缀为 double 。例如:

      static void f(int i){
    System.out.println(( INT));

    static void f(double d){
    System.out.println((double));
    }

    // ...
    f(1); //打印(int)
    f(1D); //打印(double)

    还有一个后缀 long 文字,即 L l (小写字母)。强烈建议您使用大写变体。


    JLS 3.10.1 Integer Literals



    如果后缀为ASCII字母 L 或<$ c,则整数字符串的类型为 long $ c> l ell );否则它是 int 类型。后缀 L 是首选,因为字母 l ell )通常很难与数字 1 一个)区分开来。



    Look at the three lines of code below.

      float f = 1;
    
      float g = 1.1;
    
      float h = 1.1f;
    

    Second line has compilation errors, while the other lines do not have compilation errors. First line is working fine without suffix f and third line is working with suffix f. Why is this?

    解决方案

    Floating point literals in Java is a double value by default.

    JLS 3.10.2 Floating-Point Literals

    A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.

    You can't assign a double value to a float without an explicit narrowing conversion. You therefore have two options:

    • For literals, use the suffix f or F to denote a float value
    • For non-literals, use an explicit cast (float)

    An example of the latter is:

    double d = 1.1;
    float f = (float) d; // compiles fine!
    


    On widening conversions

    The reason why this compiles:

    float f = 1;
    

    is because the widening conversion from int to float can be done implicitly in the context of an assignment.

    JLS 5.2 Assignment Conversion

    Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

    • a widening primitive conversion (§5.1.2)
    • [...]

    JLS 5.1.2 Widening Primitive Conversion

    The following 19 specific conversions on primitive types are called the widening primitive conversions:

    • int to long, float, or double
    • [...]


    Other data type suffix for literals

    As mentioned above, there's also the D or d suffix for double. Consider this snippet for example:

    static void f(int i) {
        System.out.println("(int)");
    }
    static void f(double d) {
        System.out.println("(double)");
    }
    
    //...
    f(1);   // prints "(int)"
    f(1D);  // prints "(double)"
    

    There's also a suffix for long literals, which is L or l (lowercase letter). It is highly recommended that you use the uppercase variant.

    JLS 3.10.1 Integer Literals

    An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int. The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).

    这篇关于在Java中表示浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆