Java笔记 --- 基本数据类型与String转换

Java笔记 --- 基本数据类型与String转换,第1张

Java笔记 --- 基本数据类型与String转换 一、基本数据类型与 String类型转换

将基本数据类型转成String类型,或者将String类型转成基本数据类型。

1、基本数据类型转String类型

package demo01;

public class String01 {
    public static void main(String[] args) {
        int a1 = 10;
        long a2 = 100;
        float a3 = 100.0f;
        double a4 = 10.00;
        boolean a5 = false;
        System.out.println(a1 + "");
        System.out.println(a2 + "");
        System.out.println(a3 + "");
        System.out.println(a4 + "");
        System.out.println(a5 + "");
    }
}

2、String类型转基本数据类型

package demo01;
import java.lang.Integer;
public class String02 {
    public static void main(String[] args) {
        String num = "321";
        int a1 = Integer.parseInt(num);
        long a2 = Long.parseLong(num);
        float a3 = Float.parseFloat(num);
        double a4 = Double.parseDouble(num);
        boolean a5 = Boolean.parseBoolean("false");
        short a6 = Short.parseShort(num);
        char a7 = num.charAt(0);
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(a3);
        System.out.println(a4);
        System.out.println(a5);
        System.out.println(a6);
        System.out.println(a7);
    }
}

 

练习一
package demo01;

public class Test02 {
    public static void main(String[] args) {
        int n1;
        n1 = 13;
        int n2;
        n2 = 17;
        int n3;
        n3 = n1 + n2; //30
        System.out.println("n3=" + n3);
        int n4 = 38;
        int n5 = n4 - n3; //8
        System.out.println("n5=" + n5);
    }
}
   练习二
package demo01;

public class Test03 {
    public static void main(String[] args) {
        char a1 = 'n';
        char a2 = 't';
        char a3 = 'r';
        char a4 = '\';
        char a5 = '1';
        char a6 = '2';
        char a7 = '3';
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(a3);
        System.out.println(a4);
        System.out.println(a5);
        System.out.println(a6);
        System.out.println(a7);
    }
}

 

  练习三

保存两本书名,用+拼接,看效果。保存两个性别,用加号拼接,看效果。保存两本书价格,用加号拼接,看效果。

package demo01;

public class Test04 {
    public static void main(String[] args) {
        String book1 = "小黄书";
        String book2 = "小红书";
        char age1 = '男';
        char age2 = '女';
        double money1 = 59.9;
        double money2 = 85.7;
        System.out.println(book1 + book2);
        System.out.println(age1 + age2);
        System.out.println(money1 + money2);
    }
}

练习四

用变量将姓名、年龄、成绩、性别、爱好存储,使用+,添加适当的注释,添加转义字符,使用一条语句输出。

package demo01;

public class Test05 {
    public static void main(String[] args) {
      String name = "老王"; //姓名
      int age = 18; //年龄
      char sex = '男'; //性别
      double result = 88.8; //成绩
      String like = "看电影"; //爱好
        System.out.print("姓名t"+"年龄t"+"性别t"+"成绩t"+"爱好n"+
                name+"t"+age+"tt"+sex+"tt"+result+"t"+like);
    }
}

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4829318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存