java上机题四取三排列_java第三章上机练习题

习题

1.

数据类型转换应用

【案例简介】下例说明了整型、浮点型、字符串型之间的相互转换。

【案例目的】掌握

Java

中两个不兼容类型能够的数据格式之间的转换。

【技术要点】高精度到低精度的数据转换时候需要进行强制转换,低精度向高精度进行转换的时候的会自

动进行,字符串到其他类型的转换需要用到特定的方法。

【代码分析】

public class Conversation

{

public static void main(String [] args)

{

String str = "123";

int j;

byte b;

int i=257;

double d = 323.142;

System.out.println("\nConversion of int to byte.");

//i

强制转换成

byte

System.out.println("i and b

"+ i + " "+b);

System.out.println("\nConversion of double to int.");

//d

强制转换成

int

System.out.println("d and i "+ d + " "+i);

//d

强制转换成

byte

System.out.println("d and b "+ d + " "+b);

j=Integer.parseInt(str);

//str

转换成

int

System.out.println("j="+j);

}

}

【相关知识】

数据类型转换

(

1

)简单数据类型中各类型数据间的优先关系和相互转换

不同类型数据间的优先关系如下

----------------------------------------------->

byte->short->char-> int -> long -> float -> double

自动类型转换规则

整型,实型,字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算,转

换从低级到高级。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值