首页  >  文章  >  Java  >  Java 中的转换

Java 中的转换

WBOY
WBOY原创
2024-08-30 15:12:13834浏览

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 Booleans下一篇:Servlet Architecture