久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Java 中表示浮點值

Representing float values in Java(在 Java 中表示浮點值)
本文介紹了在 Java 中表示浮點值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

看下面的三行代碼.

  float f = 1;

  float g = 1.1;

  float h = 1.1f;

第二行有編譯錯誤,而其他行沒有編譯錯誤.第一行在沒有后綴 f 的情況下工作正常,第三行在后綴 f 下工作.這是為什么呢?

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?

推薦答案

Java 中的浮點字面量默認是 double 值.

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

如果浮點文字以 ASCII 字母 Ff 為后綴,則為 float 類型;否則它的類型是 double 并且可以選擇以 ASCII 字母 Dd 為后綴.

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.

如果沒有明確的縮小轉換,您不能將 double 值分配給 float.因此,您有兩種選擇:

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

  • 對于文字,使用后綴 fF 來表示 float
  • 對于非文字,使用顯式轉換 (float)
  • For literals, use the suffix f or F to denote a float value
  • For non-literals, use an explicit cast (float)

后者的一個例子是:

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


關于擴大轉化率

這樣編譯的原因:


On widening conversions

The reason why this compiles:

float f = 1;

是因為從 intfloat 的擴大轉換可以在賦值的上下文中隱式完成.

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:

  • 擴大的原始轉換(§5.1.2)
  • [...]

以下 19 種基本類型的特定轉換稱為加寬基本類型轉換:

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

  • intlongfloatdouble
  • [...]


文字的其他數據類型后綴

如上所述,double 也有 Dd 后綴.以這個片段為例:


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)"

long 文字還有一個后綴,即 Ll(小寫字母).強烈建議您使用大寫變體.

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.

如果整數文字以 ASCII 字母 Ll (elllong>);否則它是 int 類型.后綴 L 是首選,因為字母 l (ell) 通常很難與數字 1 區分開來(一個).

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 中表示浮點值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 91久久精品一区二区三区 | 理论片免费在线观看 | 久久成人免费 | 国产精品福利久久久 | 91久久久久久久久久久 | 久久99一区二区 | 国产91丝袜在线18 | 在线小视频 | 最新中文字幕第一页视频 | 在线看h| 欧美日韩国产一区二区三区 | 午夜视频在线免费观看 | 久久久精品综合 | 亚洲精品成人av久久 | 午夜国产 | 欧美日韩亚洲一区 | 精品久久久久久亚洲精品 | 欧美日韩国产中文字幕 | 欧美伊人| 午夜精品网站 | 亚洲第一区国产精品 | 91免费高清视频 | 国产成人免费视频网站视频社区 | 久久69精品久久久久久久电影好 | 亚洲超碰在线观看 | 成年人在线观看 | 人人艹人人爽 | 欧美黄色绿像 | 欧美日韩不卡合集视频 | 在线观看国产视频 | 在线视频一区二区 | 艹逼网 | 国产精品久久久久久一区二区三区 | 精品久久伊人 | 日本欧美黄色片 | 国产欧美精品在线 | 成人影院在线 | 久草色播 | 精品国产乱码久久久久久88av | xx视频在线 | 日韩在线国产 |