继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java数据类型的转换(二)

霜花似雪
关注TA
已关注
手记 163
粉丝 1.5万
获赞 8507

http://img3.mukewang.com/5ecba7870001322712800720.jpg

7. Java包装类型的转换

基本数据类型转换为包装类:

基本数据类型转换为包装类,可以利用包装类的构造函数实现,即:

Byte(byte value)、Short(short value)、Character(char value)、Integer(int value)、Long(long value)、Float(float value)、Double(double value)、Boolean(boolean value)


包装类转换为基本数据类型:

包装类转换为基本数据类型,可以使用包装类带有的xxxValue()来进行转化,即:

byteValue()、shortValue()、charValue()、intValue()、longValue()、floatValue()、doubleValue()、booleanValue()

示例代码:

byte b = 100;
Byte b1 = new Byte(b);  //byte -> Byte
byte b2 = b1.byteValue();   //Byte -> byte
short sb = b1.shortValue();  //Byte -> short
int ib = b1.intValue();   //Byte -> int
long lb = b1.longValue();  //Byte -> long
float fb = b1.floatValue();  //Byte -> float
double db = b1.doubleValue();  //Byte -> double  其他同理

short s = 200;
Short s1 = new Short(s);   //short -> Short
short s2 = s1.shortValue();  //Short -> short
byte bs = s1.byteValue();  //Short -> byte  其他同理

char c = 96;
Character c1 = new Character(c);  //shar -> Character
char c2 = c1.charValue();   //Character -> char  其他同理

int i = 120;
Integer i1 = new Integer(i);  //int -> Integer
int i2 = i1.intValue();       //Integer -> int
byte b3 = i1.byteValue();   //Integer -> byte
short s3 = i1.shortValue();  //Integer -> short  其他同理

long l = 1000;
Long l1 = new Long(l);  //long -> Long
long l2 = l1.longValue();  //Long -> long  其他同理

float f = 3.14f;
Float f1 = new Float(f);  //float -> Float
float f2 = f1.floatValue();  //Float -> float  其他同理


double d = 314.151617;
Double d1 = new Double(d);  //double -> Double
double d2 = d1.doubleValue();  //Double -> double
float fd = d1.floatValue();  //Double -> float
long ld = d1.longValue();  //Double -> long
int id = d1.intValue();  //Double -> int  其他同理

boolean bool = false;
Boolean bool1 = new Boolean(bool);  //boolean -> Boolean
boolean bool2 = bool1.booleanValue();  //Boolean -> boolean 其他同理

8. 字符串与其它类型(封装和基本)间的转换

其他类型转换为字符串:

(1)调用类的串转换方法:X.toString();  针对封装类型转换为字符串

(2)自动转换:X+"";  基本类型与封装类型都可以使用此方法转化为字符串

(3)使用String的方法:String.volueOf(X); 基本类型与封装类型都可以使用此方法转化为字符串

示例代码:

//Byte -> String
Byte by = 120;
String bStr = by.toString();
String bStr1 = by + "";
String bStr2 = String.valueOf(by);

//Short -> String
Short sh = 150;
String sStr = sh.toString();
String sStr1 = sh + "";
String sStr2 = String.valueOf(sh);

//Character -> String
Character ch = 100;
String cStr = ch.toString();
String cStr1 = ch + "";
String cStr2 = String.valueOf(ch);
String cStr3 = new String(new char[]{ch});  //使用匿名数组

//Integer -> String
Integer in = 100;
String iStr = in.toString();
String iStr1 = in + "";
String iStr2 = String.valueOf(in);

//Float -> String
Float fl = 314.15F;
String fStr = fl.toString();
String fStr1 = fl + "";
String fStr2 = String.valueOf(fl);

//Double -> String
Double dou = 618.618;
String dStr = dou.toString();
String dStr1 = dou + "";
String dStr2 = String.valueOf(dou);

//Boolean -> String
Boolean bool = true;
String boolStr = bool.toString();
String boolStr1 = bool + "";
String boolStr2 = String.valueOf(bool);

字符串转化为其他类型:

(1)首先转化为相应的封装类实例,再调用对应的xxxValue()方法转化成其他类型,即:

byteValue()、shortValue()、charValue()、intValue()、longValue()、floatValue()、doubleValue()、booleanValue()


(2)使用封装类自带的parseXXX方法,即:

parseByte(String s)、parseShort(String s)、parseInt(String s)、parseLong(String s)、parseFloat(String s)、parseDouble(String s)、parseBoolean(String s)


(3)字符串转化为char类型:

方法一:使用 String.charAt( index ) 方法,返回在index位置的char字符。(返回值:char )

方法二:使用 String.toCharArray( ) 方法,将String 转化为 字符串数组。(返回值:char[] ) 

示例代码:

String str = "114";
String str1 = "314.15926";
String str2 = "true";
String str3 = "ABCDEF";

//String --> byte/Byte
byte b1 = new Byte(str).byteValue();  //方法1
byte b2 = Byte.valueOf(str).byteValue();  //方法2
byte b3 = Byte.parseByte(str);   //方法3   下同
Byte b4 = Byte.parseByte(str);

//String --> short/Short
short s1 = new Short(str).shortValue();
short s2 = Short.valueOf(str).shortValue();
short s3 = Short.parseShort(str);
Short s4 = Short.parseShort(str);

//String --> int/Integer
int i1 = new Integer(str).intValue();
int i2 = Integer.valueOf(str).intValue();
int i3 = Integer.parseInt(str);
Integer i4 = Integer.parseInt(str);

//String --> long/Long
long l1 = new Long(str).longValue();
long l2 = Long.valueOf(str).longValue();
long l3 = Long.parseLong(str);
Long l4 = Long.parseLong(str);

//String --> float/Float
float f1 = new Float(str1).floatValue();
float f2 = Float.valueOf(str1).floatValue();
float f3 = Float.parseFloat(str1);
Float f4 = Float.parseFloat(str1);

//String --> double/Double
double d1 = new Double(str1).doubleValue();
double d2 = Double.valueOf(str1).doubleValue();
double d3 = new Float(str1).doubleValue();
double d4 = new Long(str).doubleValue();
double d5 = new Integer(str).doubleValue();
double d6 = new Short(str).doubleValue();
Double d7 = new Byte(str).doubleValue();

double d8 = Double.parseDouble(str1);
Double d9 = Double.parseDouble(str1);

//String --> boolean/Boolean
boolean bool = new Boolean(str2).booleanValue();
boolean bool1 = Boolean.valueOf(str2).booleanValue();
boolean bool2 = Boolean.parseBoolean(str2);
Boolean bool3 = Boolean.parseBoolean(str2);

//String --> char
char[] ch = str3.toCharArray();  //转化为char数组
char ch1 = str3.charAt(2);  //转化为char字符


9. Jdk1.7 Date类与其它数据类型的相互转换

(1)Date型与int的相互转化

int型转化为Date:

使用int型来分别表示年、月、日、时、分、秒,然后利用Date的构造函数进行转化,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转化为int:

可以使用Date类的相关方法,来获取对应的int型,如下:

int getYear()     //获取年份

int getMonth()    //获取月份

int getDate()     //获取日期

int getHours()    //获取小时

int getMinutes()  //获取分钟

int getSeconds()  //获取秒数


(2)Date型与long的转化

Date型转化为long型,可以使用Date类自带的方法进行转化:

long getTime()  //将时间转为long型


long型转化为Date类,可以使用Date类自带的方法进行转化:

Date(long date) //将long转换为Date


(3)Date型与String的转化

Date型转化为String型:

可以使用Date类自带的toString()方法实现:

String toString()  //将Date转换为String


也可以使用SimpleDateFormat的format(date)方法实现:

String format(Date date)  //Date转化为String

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String timeStr1 = sdf.format(date);


String型转化为Date型:

可以使用SimpleDateFormat的parse()方法实现:

Date parse(String source)   //String转化为Date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = "2020-05-05 15:51:25";
Date date = sdf.parse(timeStr);

示例代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

int year = 2020;  //年
int month = 04;  //月
int day = 26;  //日
int hrs = 12;  //时
int min = 14; //分
int sec = 36;//秒

//jdk1.7
Date time = new Date(year, month, day, hrs, min, sec);
System.out.println("time = " + time);

int dateStr = time.getDate();
System.out.println("dateStr = " + dateStr);

int day1 = time.getDay();
System.out.println("day1 = " + day1);

//Date 转化为 long
Date date = new Date();
long longTime = date.getTime();
System.out.println("longTime = " + longTime);

//long转换为Date
long longTimeNew = 1588923444897L;
Date dateNew = new Date(longTimeNew);
System.out.println("dateNew = " + dateNew);

//Date转化为String
String timeStr = date.toString();
System.out.println("timeStr = " + timeStr);


String timeStr1 = sdf.format(date);
System.out.println("timeStr1 = " + timeStr1);


//String转化为Date
String timeStr2 = "2020-05-05 15:51:25";
Date dateNew2 = sdf.parse(timeStr2);
System.out.println("dateNew2 = " + dateNew2);


10. Jdk1.8 LocalDateTime类与其他数据类型的相互转换

(1)LocalDateTime与int的相互转化

int型转化为LocalDateTime

使用int型来分别表示年、月、日、时、分、秒,然后利用LocalDateTime的构造函数进行转化,LocalDateTime的构造函数如下:

LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)

LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)

LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)


LocalDateTime转换为int:

可以使用LocalDateTime类的相关方法,来获取对应的int类型,如下:

int getYear()

int getMonthValue()

int getDayOfMonth()

int getDayOfYear()

int getHour()

int getMinute()

int getSecond()

int getNano()


(2)LocalDateTime与long的相互转化

//LocalDateTime转化为long

Long localDateTimeToLong = Timestamp.valueOf(LocalDateTime.now()).getTime();

System.out.println("LocalDateTime -> Long:  " + localDateTimeToLong);


//long转化为LocalDateTime

LocalDateTime longToLocalDateTime =LocalDateTime.ofInstant(Instant.ofEpochMilli(1588928910530L), ZoneId.systemDefault());

System.out.println("Long -> LocalDateTime:  " + longToLocalDateTime);


(3)LocalDateTime与String的相互转化

//LocalDateTime转化为String

String localDateTimeToString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

System.out.println("LocalDateTime -> String:  " + localDateTimeToString);


//String转化为LocalDateTime

LocalDateTime stringToLocalDateTime =LocalDateTime.parse("2020-05-08 15:30:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

System.out.println("String -> LocalDateTime:  " + stringToLocalDateTime);

示例代码:

int year = 2020;  //年
int month = 04;  //月
int date = 26;  //日
int hrs = 12;  //时
int min = 14; //分
int sec = 36;//秒

//jdk1.8
LocalDateTime dateTime =  LocalDateTime.of(year, month, date, hrs, min, sec);
System.out.println("dateTime = " + dateTime);

//LocalDateTime转化为long
Long localDateTimeToLong = Timestamp.valueOf(LocalDateTime.now()).getTime();
System.out.println("LocalDateTime -> Long:  " + localDateTimeToLong);

//long转化为LocalDateTime
LocalDateTime longToLocalDateTime =LocalDateTime.ofInstant(Instant.ofEpochMilli(1588928910530L), ZoneId.systemDefault());
System.out.println("Long -> LocalDateTime:  " + longToLocalDateTime);

//LocalDateTime转化为String
String localDateTimeToString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("LocalDateTime -> String:  " + localDateTimeToString);

//String转化为LocalDateTime
LocalDateTime stringToLocalDateTime =LocalDateTime.parse("2020-05-08 15:30:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("String -> LocalDateTime:  " + stringToLocalDateTime);


总结:

1. 只有boolean不参与数据类型的转换


2. 自动类型的转换:

a.常数在表示数范围内是能够自动类型转换的

b.数据类型范围小的可以自动向数据类型范围大的转换

c.引用类型能够自动转换为父类的

d.基本数据类型和它们对应的包装类型是能够互相转换的


3. 强制类型转换:用圆括号括起来目标类型,置于变量前


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP