Home  >  Article  >  Java  >  Detailed explanation of Java data type conversion

Detailed explanation of Java data type conversion

高洛峰
高洛峰Original
2017-01-22 10:25:172416browse

1. Explanation of basic data types

The Java language provides eight basic types. Six numeric types (four integers, two floating point types), a character type, and a Boolean type.
[Note] JAVA does not have unsigned type
(1). Integer: int, short, byte, long
(2). Floating point type: float, double
(3). Character: char
(4). Boolean: boolean

Basic type Size Minimum value Maximum value
void
boolean ----- - ----- ------
char 16-bit Unicode 0 Unicode 2^16-1
byte 8-bit -128 +127
short 16-bit -2^15 +2^15-1
int 32-bit -2 ^31 +2^31-1
Long 64-bit -2^63 +2^63-1
Float 32-bit ieee754
Double 64-bit ieee754

, we also often use two class variables, namely String and Date.

2. Data conversion

1 Types of data type conversion
Java data type conversion is generally divided into three types, namely:
(1). Basic data Conversion between types
 (2). Conversion between strings and other data types
 (3). Conversion of other practical data types

2 Conversion between basic data types
Basic The types from low-level to high-level are (byte, short, char)--int--long--float--double
Conversion between simple data types can be divided into:
●Low-level to high-level automatic Type conversion
●High-level to low-level forced type conversion
●Wrapper class transition type can be converted

2.1 Automatic type conversion
2.1.1 Low-level variables can be directly converted to high-level variables, which is called For automatic type conversion, for example, the following statement can be passed directly in Java:

byte b;
int i=b;
long l=b;
float f=b;
double d=b;

  2.1.2 If the low-level type is char type, when converting to the high-level type (integer), Will be converted to the corresponding ASCII code value, such as

char c='c';
int i=c;
System.out.println("output:"+i);

Output: output:99;

2.1.3 For the three types of byte, short, and char, they They are flat, so they cannot be automatically converted to each other. Use the following forced type conversion.

short i=99 ;
char c=(char)i;
i =(short) c;
System.out.println("output:"+c);

Output: output:c;
But according to experience, the three types of byte, short, and int are all integers, so if you operate integer data, it is best to unify them. Use int type.

 2.2 Forced type conversion
 When converting high-level variables to low-level variables, the situation will be more complicated. You can use forced type conversion. That is, you must use the following statement format:

int i=99;
byte b=(byte)i;
char c=(char)i;
float f=(float)i;

As you can imagine, this conversion may definitely lead to overflow or loss of accuracy.

 2.3 Packaging class transition type conversion
When we discuss the mutual conversion between other variable types, we need to understand Java's packaging classes. The so-called packaging classes can directly represent simple types of variables. For a class, we will use these wrapper classes extensively when performing variable type conversions. There are six packaging classes in Java, namely Boolean, Character, Integer, Long, Float and Double. Literally we can see that they correspond to boolean, char, int, long, float and double respectively. String and Date are classes themselves. So there is no concept of packaging class.

When converting between simple data types (automatic conversion or forced conversion), we can always use wrapper classes for intermediate transitions.
Normally, we first declare a variable, then generate a corresponding packaging class, and then use various methods of the packaging class to perform type conversion. For example:
Example 1, when you want to convert float type to double type:

float f1=100.00f;
Float F1=f1;//自动装箱
double d1=F1.doubleValue();//F1.doubleValue()为Float类的返回double值型的方法  当希望把double型转换为int型时:
 
double d1=100.00;
Double D1=new Double(d1);//调用构造函数
int i1=D1.intValue();

     简单类型的变量转换为相应的包装类,可以利用包装类的构造函数和自动装箱而直接赋值。即:
    Boolean(boolean value)、Character(char value)、Integer(int value)、Long(long value)、Float(float value)、Double(double value)
    而在各个包装类中,总有形为××Value()的方法,来得到其对应的简单类型数据。利用这种方法,也可以实现不同数值型变量间的转换,例如,对于一个双精度实型类,intValue()可以得到其对应的整型变量,而doubleValue()可以得到其对应的双精度实型变量。

3 字符串型与其它数据类型的转换
  通过查阅类库中各个类提供的成员方法可以看到,几乎从java.lang.Object类派生的所有类提供了toString()方法,即将该类转换为字符串。例如:Characrer,Integer,Float,Double,Boolean,Short等类的toString()方法toString()方法用于将字符、整数、浮点数、双精度数、逻辑数、短整型等类转换为字符串。如下所示:

int i1=10;
float f1=3.14f;
double d1=3.1415926;
  
Integer I1=new Integer(i1);//生成Integer类
Float F1=f1; //自动装箱
Double D1=d1;
  
//分别调用包装类的toString()方法转换为字符串
String si1 = i1 + "";//这种方式更为通用
String sf1=F1.toString();
String sd1=D1.toString();
  
Sysytem.out.println("si1"+si1);
Sysytem.out.println("sf1"+sf1);
Sysytem.out.println("sd1"+sd1);

4. 将字符型直接做为数值转换为其它数据类型
  例如,'1'就是指的数值1,而不是其ASCII码,对于这种转换:

char c = '1';
//Character的getNumericValue(char ch)方法
int i = Character.getNumericValue(c);
//ASCII码相减
i = c -'0';

5. Date类与其它数据类型的相互转换
  整型和Date类之间并不存在直接的对应关系,只是你可以使用int型为分别表示年、月、日、时、分、秒,这样就在两者之间建立了一个对应关系,在作这种转换时,你可以使用Date类构造函数的三种形式:

  Date(int year, int month, int date):以int型表示年、月、日
  Date(int year, int month, int date, int hrs, int min):以int型表示年、月、日、时、分
  Date(int year, int month, int date, int hrs, int min, int sec):以int型表示年、月、日、时、分、秒

  在长整型和Date类之间有一个很有趣的对应关系,就是将一个时间表示为距离格林尼治标准时间1970年1月1日0时0分0秒的毫秒数。对于这种对应关系,Date类也有其相应的构造函数:Date(long date)
获取Date类中的年、月、日、时、分、秒以及星期你可以使用Date类的getYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()、getDay()方法,你也可以将其理解为将Date类转换成int。

  而Date类的getTime()方法可以得到我们前面所说的一个时间对应的长整型数,与包装类一样,Date类也有一个toString()方法可以将其转换为String类。
有时我们希望得到Date的特定格式,例如20020324,我们可以使用以下方法,首先在文件开始引入,

import java.text.SimpleDateFormat;
import java.util.*;
java.util.Date date = new java.util.Date();
  
//如果希望得到YYYYMMDD的格式
SimpleDateFormat sy1=new SimpleDateFormat("yyyyMMDD");
String dateFormat=sy1.format(date);
  
//如果希望分开得到年,月,日
SimpleDateFormat sy=new SimpleDateFormat("yyyy");
SimpleDateFormat sm=new SimpleDateFormat("MM");
SimpleDateFormat sd=new SimpleDateFormat("dd");
String syear=sy.format(date);
String smon=sm.format(date);
String sday=sd.format(date);

更多Java数据类型转换详解相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn