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

在 Java 中表示浮點(diǎn)值

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

問題描述

看下面的三行代碼.

  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 中的浮點(diǎn)字面量默認(rèn)是 double 值.

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

如果浮點(diǎn)文字以 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.

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

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

  • 對于文字,使用后綴 fF 來表示 float
  • 對于非文字,使用顯式轉(zhuǎn)換 (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!


關(guān)于擴(kuò)大轉(zhuǎn)化率

這樣編譯的原因:


On widening conversions

The reason why this compiles:

float f = 1;

是因?yàn)閺?intfloat 的擴(kuò)大轉(zhuǎn)換可以在賦值的上下文中隱式完成.

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

賦值轉(zhuǎn)換發(fā)生在將表達(dá)式的值賦值給變量時:必須將表達(dá)式的類型轉(zhuǎn)換為變量的類型.賦值上下文允許使用以下之一:

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:

  • 擴(kuò)大的原始轉(zhuǎn)換(§5.1.2)
  • [...]

以下 19 種基本類型的特定轉(zhuǎn)換稱為加寬基本類型轉(zhuǎn)換:

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

  • intlong、floatdouble
  • [...]


文字的其他數(shù)據(jù)類型后綴

如上所述,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(小寫字母).強(qiáng)烈建議您使用大寫變體.

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.

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

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

這篇關(guān)于在 Java 中表示浮點(diǎn)值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
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 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲高清在线视频 | 欧美888| 国产精品久久久久久久午夜片 | 91在线免费观看 | 国产真实乱全部视频 | 在线观看国产视频 | 特级生活片 | av免费入口 | 国产免费一区二区 | 国产 欧美 日韩 一区 | 亚洲在线一区 | av免费网址 | 亚洲欧美在线一区 | 九九成人 | 91在线免费视频 | 成人免费视频网站在线看 | 国产精品久久久久久久久久久久冷 | 国产一区不卡在线观看 | 成人伊人网 | 日韩视频一区在线观看 | 日韩高清一区二区 | 中文字幕在线视频观看 | 日韩成人在线观看 | 日韩中文字幕在线观看 | 午夜天堂精品久久久久 | 日韩中文在线观看 | 久久久久久久久久久久91 | 欧美一级视频 | 久久久久精 | 亚洲福利| 北条麻妃99精品青青久久 | 亚洲精品国产成人 | 亚洲欧美中文日韩在线 | 在线视频亚洲 | 亚洲va欧美va天堂v国产综合 | 亚洲综合在线视频 | 日本精品一区二区三区在线观看视频 | 91视频免费在观看 | 精品一区二区三区不卡 | 欧美不卡一区二区三区 | 日本不卡一区 |