首頁  >  文章  >  Java  >  Java 中的轉換

Java 中的轉換

WBOY
WBOY原創
2024-08-30 15:12:13838瀏覽

Java中的轉換是指變數可以被宣告為某種資料類型,並且為了特定操作/函數能夠成功執行而將其轉換為不同的資料類型的現象。此資料型別轉換適用於所有八種資料型別:int、char、long、boolean、float、double、byte 和short。轉換的類型可以分為隱式轉換和明確轉換。當兩種資料類型相容或目標資料類型大於來源資料類型時,可以實現隱式轉換方法。字串的明確轉換方法有「string to int」、「string to long」、「string to float」、「string to boolean」等多種方法實現,日期轉換則有「string to date」等實現'日期到字串'。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

java 中的轉換類型

根據變數轉換為哪種資料類型,我們可以將其分為兩類:

1.隱式轉換

它也稱為自動轉換,因為它不需要任何明確程式碼來進行轉換過程,並且就像為變數分配另一種資料類型值一樣簡單。一個非常基本的範例是將整數值指派給 long 變數。讓我們用範例來示範簡單的隱式轉換。

範例程式碼:

public class con_java {
public static void main(String[] args) {
int a = 22;
long b = a;
System.out.println("Converted Value is : " + b);
}
}

在上面給出的範例中,我們只是向 long 變數提供了一個整數值,它的作用就像一個魅力。 Int 和 Long 都是數值資料型,可以順利配合。

程式碼解釋:在一個帶有 main 的簡單類別中,我們聲明了一個值為 22 的整數「a」變量,然後聲明了長變數「b」。這裡我們將a的整數值賦給b,它是long資料型別。透過列印行,列印長資料類型 b 的值。由於是隱式轉換,不需要額外的程式碼。

輸出:

Java 中的轉換

對於隱式轉換,有兩個基本規則。只有滿足這些屬性,轉換才會順利執行。

a。兩種資料型態必須相容

  • 如果任何一種資料類型與另一種不相容,則轉換不會發生。如果來源資料類型屬於數字類別,則目標資料類型也必須屬於同一類別。
  • 數位資料類型彼此非常相容且易於轉換。但這些相同的數字資料類型無法輕鬆轉換為 char 類型或布林類型。
  • Byte、Short、Int、Long、Float 和 Double 是數值資料型態。

b。目標資料型態必須大於來源資料型別

  • 這僅僅意味著我們嘗試轉換的資料類型必須具有更大的位元大小。
  • 例如,我們將一個 int 值指派給一個 long 變數。這裡,int的位元大小是32位元;相反,long 的位元大小為 64 位元。意思是,64 位長的值比 32 位元整數大。

滿足上述規則後,就會發生簡單的隱式轉換。現在,讓我們了解顯式轉換。

隱式轉換的第二個要求是較低位元資料類型只能轉換為較大位元資料類型,這會導致轉換時不會遺失資料。但是,如果我們需要將較大位元大小的資料類型轉換為較小位元大小的資料類型呢?這裡資料遺失是不可避免的,java編譯器將拋出錯誤「UserWarni:轉換時可能的精確度遺失」或其他錯誤(取決於程式碼)。當我們了解轉換屬性及其引發的錯誤時,我們會使用明確轉換。

2.明確轉換

實現明確轉換是透過根據要求明確定義我們的自訂臨時資料類型來覆蓋 java 的預設類型轉換。當我們明確提供類型轉換時,值的資料類型將變更為短期內所需的資料類型。顯式轉換也稱為縮小類型。類型轉換的語法是:

Vaiable2 = (type) Variable1;

這裡,variable2 是 Variable1 必須轉換成的不同資料型別的目標變數。 (type) 是 Variable1 轉換為並指派給 Variable2 的資料類型的規格。

Explicit Conversion can be of immense use, where a small part of the number is kept on hold while the calculation is executed. Application for explicit conversion can be a simple calculator, where the percentage of the student has to be calculated. To demonstrate the working of Explicit Conversion, let’s try an example.

Sample Code:

public class exp_con_java {
public static void main(String[] args) {
double dou_Variable = 120.14;
long long_Variable = (long) dou_Variable;
int intVariable = (int)long_Variable;
System.out.println("The Double value is "+dou_Variable);
System.out.println("The Long value is "+long_Variable);
System.out.println("The Integer value is "+intVariable);
}
}

Output:

Java 中的轉換

Below is the list of Possible Conversions in Java:

  • String to int (using Integer.parseInt() which returns primitive int)
  • String to long (using Long.parseLong() which returns primitive long)
  • String to float (using Float.parseFloat(), it returns primitive float)
  • String to Boolean (using Boolean.parseBoolean(), it returns primitive boolean, either TRUE or FALSE)

SimpleDateFormat(): is a Java class that helps in formatting and parsing of the data. It simply allows us to convert a simple String into a Date object.

  • String to Date (using parse(), it converts a value of String into Date object)
  • Date to String (using format(), simply converts a Date into String)

The above listed are the possible conversion types and the methods required, and the output it returns.

Conclusion

Type conversion in Java or any other language is a better way of utilizing its functions and getting the desired output. We have understood two types of conversion based on properties and data types. Implicit conversion does not need any extra effort but must follow two properties. And Explicit conversion must be explicitly defined in order to override Java’s default conversion. We have understood both types with program examples.

以上是Java 中的轉換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 布林值下一篇:Java 布林值