首页 > 学院 > 开发设计 > 正文

JAVA数据类型自动转换,与强制转换

2019-11-15 00:16:19
字体:
来源:转载
供稿:网友
java数据类型自动转换,与强制转换 Posted on 2015-05-23 00:37 杨波php 阅读(...) 评论(...) 编辑 收藏

一、数据类型自动转换

public class Test{    public static void main(String[] args){        int a    =    1;        double    b    =    1.5;        double    a_b_count    =    a+b;    //整型与双精度型相加时,结果会自动转换为双精度型                String c    =    "凌";        char d    =    '晨';        char e    =    '好';        String c_d_e_content    =    c+d+e;    //字符型与字符串型相加时,结果会自动转换为字符串型                System.out.PRintln(a_b_count);            System.out.println(c_d_e_content);    }}

二、数据类型强制转换

public class Test{    public static void main(String[] args){        int    a    =    (int)10.2;    //将双精度类型数据强制转换为整形        double    b    =    (double)10;    //将整型类型的数据强制转换为双精度型        System.out.println(a);        System.out.println(b);    }}

int与double之间可以强制互相转换。

char与String类型之间不能强制互相转换。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表