阶段二1_常用API之String

一.Scanner类
1.next和nextLine的区别

  next以空格和tab健结束, nextLine以回车换行符结束

2.nextInt和nextLine一起使用

  nextInt和nextLine一起使用的时候,nextLine没有输入的机会。
  因为nextInt会把回车换行符保留,等nextLine在接受数的时候就会结束。
  解决:nextInt和next一起使用,可能会丢失数据

3.Scanner类中的next,nextInt,nextLine三个方法说明,代码演示:

/**
 * 功能:
 *   Scanner类中next(),nextInt(),nextLine()三个方法的应用
 * 执行结果:
 *  请输入字符串:
 *  1122 11
 *  1122 11
 *  请输入字符串:
 *  11 11
 *  11
 *  请输入整数:
 *  11
 *  -------------------
 *  请输入数字:
 *  2
 *  请输入字符串:
 *
 *  Process finished with exit code 0
 */
public class DemoScanner01 {
    public static void main(String[] args) {
        //创建一个scanner对象
        Scanner sc = new Scanner(System.in);
        //从键盘接受字符串
        System.out.println("请输入字符串:");
        String s = sc.nextLine();
        System.out.println(s);
        /**
        注意点1:
         next和nextLine的区别
         next以空格和tab健结束, nextLine以回车换行符结束
        */
        System.out.println("请输入字符串:");
        s = sc.next();
        System.out.println(s);
        //从键盘输入一个数字
        System.out.println("请输入整数:");
        int i = sc.nextInt();
        System.out.println(i);
        System.out.println("-------------------");
        /**
         注意点2:
         nextInt和nextLine一起使用的时候,nextLine没有输入的机会。
         因为nextInt会把回车换行符保留,等nextLine在接受数的时候就会接受。
         解决:nextInt和next一起使用,可能会丢失数据
        */
        System.out.println("请输入数字:");
        int n = sc.nextInt();
        System.out.println("请输入字符串:");
        sc.nextLine();
    }
}

二.String类
1.String类在java.lang核心包下,使用的时候不需要导入包
2.String字符串"abc"是一个对象
3.String字符串"abc"是一个常量,创建后就不能修改
4.String字符常量代码演示

/**
 * 功能:
 * 1.String类在java.lang核心包下,使用的时候不需要导入包
 * 2.String字符串"abc"是一个对象
 * 3.String字符串"abc"是一个常量,创建后就不能修改
 */
public class DemoString01 {
    public static void main(String[] args) {
        //"abc"可以直接调用方法,说明是一个对象
        "abc".length();
        //"abc"是一个对象 "abc1"也是一个对象,对象没有提供什么set修改的方法,所以值不能修改
        String s = "abc";
        s="abc1";
    }
}

5.String常见构造方法
String(),创建一个空白的字符串。
String(char[] chs),使用char数组创建一个对象。
String(String str),传入一个String创建一个对象。
String s = “abc”。
6.String常见构造方法创建对象的代码案例

/**
 * String常见构造方法
 *  String(), 创建一个空白的字符串。
 *  String(char[] chs),使用char数组创建一个对象。
 *  String(String str),传入一个String创建一个对象。
 *  String s = "abc"。
    注意:
      String这个类比较特殊, 打印其对象名的时候, 不会出现内存地址
      而是该对象所记录的真实内容.
      
    执行结果:
    
    abc
    abd
    Process finished with exit code 0
 *  
 */
public class DemoString02 {
    public static void main(String[] args) {
        //1.String(), 创建一个空白的字符串。
        String s1 = new String();
        System.out.println(s1);
        //2.String(char[] chs),使用char数组创建一个对象。
        char[] chs = {'a','b','c'};
        String s2 = new String(chs);
        System.out.println(chs);
        //3.String(String str),传入一个String创建一个对象。
        String s3 = new String("abd");
        System.out.print(s3);
    }
}

7.创建字符串对象的区别对比
结论:双引号创建的字符串对象,在字符串常量池中存储,通过构造方法创建的字符串对象,在堆内存中存储
图1:1_创建字符串读写的区别对比0.png
在这里插入图片描述

图2:1_创建字符串读写的区别对比1.png
在这里插入图片描述

图3:1_创建字符串读写的区别对比2.png
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java之书

会持续更新实用好的文章谢谢关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值