2023/4/4JAVA学习打卡

**

One:

一、API
概述
API(Application Programming Interface),应用程序编程接口。这些类将底层的代码实现封装了起来。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们.
API使用:我们通过Scanner类和Random类,熟悉一下查询API,并使用类的步骤。

  • java.util.Scanner :该类需要import导入后使用。
    查看构造方法
  • public Scanner(InputStream source) : 构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。
    查看成员方法
  • public int nextInt():获取下一个int数字。
  • public String nextLine():获取下一行完整字符串。

String类

String类概述
java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如"abc" )都可以被看作是实现此类的实例。

类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或小写的所有字符的字符串的副本。

特点

  1. 字符串不变:字符串的值在创建后不能被更改。

    String s1 = “abc”;
    s1 += “d”;
    System.out.println(s1); // “abcd”
    // 内存中有"abc",“abcd"两个对象,s1从指向"abc”,改变指向,指向了"abcd"。

  2. 因为String对象是不可变的,所以它们可以被共享。
    String s1 = “abc”;
    String s2 = “abc”;
    // 内存中只有一个"abc"对象被创建,同时被s1和s2共享。

  3. “abc” 等效于 char[] data={ ‘a’ , ‘b’ , ‘c’ },但是底层原理是字节数组( byte[ ] )
    例如:
    String str = “abc”;
    相当于:
    char data[ ] = {‘a’, ‘b’, ‘c’};
    String str = new String(data);
    // String底层是靠字符数组实现的。
    构造方法

  • 查看类
    • java.lang.String :此类不需要导入。
  • 查看构造方法
    • public String() :初始化新创建的 String对象,以使其表示空字符序列。
    • public String(char[] value) :通过当前参数中的字符数组来构造新的String。
    • public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
    • 直接赋值的方式创建字符串对象
 public static void main(String[] args) {
        char[] chs = {'a','b','c','d'};
        String s = new String (chs);
        System.out.println(s);
        String s2 = new String(chs,1,3);
        System.out.println(s2);

        byte [] bytes = {97,98,99,100};
        String s3 = new String(bytes);
        System.out.println(s3);
        String s4 = new String(bytes,0,3);
        System.out.println(s4);
        System.out.println("---------------------------");
        String s5 = "HelloWorld";

        char[] chars = s5.toCharArray();
        for (int i = 0;i<chars.length;i++){
            System.out.println(chars[i]);
        }
        System.out.println("---------------------");
        String s0 = "I LOVE JAVA";
        char[] chis = s0.toCharArray();
        for (int i = 0; i < s0.length(); i++) {
            System.out.println(chis[i]);
        }
    }         

常用方法
判断功能的方法

  • public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
  • public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
    Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中。
    创建字符串对象两种方式的区别
  • 通过构造方法创建
    通过 new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然内容相同,但是地址值不同
  • 直接赋值方式创建
    以“”方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM 都只会建立一个 String 对象,并在字符串池中维护

字符串的比较:==号和equals的作用

  • ==比较基本数据类型:比较的是具体的值

  • ==比较引用数据类型:比较的是对象地址值

  • equals比较String类型: 比较的是对象的内容是否相同

    /*
    使用 == 做比较:
    基本类型:比较的是数据值是否相同
    引用类型:比较的是地址值是否相同

       public boolean equals(Object anObject):
          将此字符串与指定对象进行比较。由于我们比较的是字符串对象,所以参数直接传递一个字符串
    

    */
    public class StringDemo02 {
    public static void main(String[] args) {
    //构造方法的方式得到对象
    char[] chs = {‘a’, ‘b’, ‘c’};
    String s1 = new String(chs);
    String s2 = new String(chs);

          //直接赋值的方式得到对象
          String s3 = "abc";
          String s4 = "abc";
    
          //比较字符串对象地址是否相同
          System.out.println(s1 == s2);
          System.out.println(s1 == s3);
          System.out.println(s3 == s4);
          System.out.println("--------");
    
          //比较字符串内容是否相同
          System.out.println(s1.equals(s2));
          System.out.println(s1.equals(s3));
          System.out.println(s3.equals(s4));
      }
    

    }

获取功能的方法

  • public int length () :返回此字符串的长度。
  • public String concat (String str) :将指定的字符串连接到该字符串的末尾。
  • public char charAt (int index) :返回指定索引处的 char值。
  • public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
  • public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
  • public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。

方法演示,代码如下:

/*
    获取功能的方法
    - public int length () :返回此字符串的长度。
        举例:s.length() 获取s中的字符的数量

    - public String concat (String str) :将指定的字符串连接到该字符串的末尾。
        举例:s1.cocat(s2) 把s2连接到s1的末尾

    - public char charAt (int index) :返回指定索引处的 char值。
        举例:s1.charAt(5) 获取s1中索引为5的字符

    - public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
         举例:s1.indexOf(s2) 查找s2在s1中第一次出现的位置,如果不存在,返回-1

    - public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
        举例:s1.substring(5) 截取s1字符串从索引5开始一直到最后的内容

    - public String substring (int beginIndex, int endIndex) :
        返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
        举例:s1.substring(5,10) 截取s1字符串从索引5开始一直到索引10之间的内容(包含5,不包含10)
 */
public class String_Demo02 {
  public static void main(String[] args) {
    //创建字符串对象
    String s = "helloworld";

    // int length():获取字符串的长度,其实也就是字符个数
    System.out.println(s.length());
    System.out.println("--------");

    // String concat (String str):将将指定的字符串连接到该字符串的末尾.
    String s = "helloworld";
    String s2 = s.concat("**hello doit");
    System.out.println(s2);// helloworld**hello doit

    // char charAt(int index):获取指定索引处的字符
    System.out.println(s.charAt(0));
    System.out.println(s.charAt(1));
    System.out.println("--------");

    // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回-1
    System.out.println(s.indexOf("l"));
    System.out.println(s.indexOf("owo"));
    System.out.println(s.indexOf("ak"));
    System.out.println("--------");

    // String substring(int start):从start开始截取字符串到字符串结尾
    System.out.println(s.substring(0));
    System.out.println(s.substring(5));
    System.out.println("--------");

    // String substring(int start,int end):从start到end截取字符串。含start,不含end。
    System.out.println(s.substring(0, s.length()));
    System.out.println(s.substring(3,8));
  }
}

转换功能的方法

  • public char[] toCharArray () :将此字符串转换为新的字符数组。
  • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
  • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。

方法演示,代码如下:

/*
    转换功能的方法

    - public char[] toCharArray () :把字符串变成对应的字符数组。
        举例:s1.toCharArray() 把s1变成字符数组

    - public byte[] getBytes () :把字符串变成对应的字节数组。
        举例:s1.getBytes() 把s1变成字节数组

    - public String replace (String oldStr, String newStr) :把字符串中的所有的oldStr替换成newStr。
        举例:s1.replace("a","A") 把s1中的所有的"a"替换成"A"

 */
public class String_Demo03 {
  public static void main(String[] args) {
    //创建字符串对象
    String s = "abcde";

    // char[] toCharArray():把字符串转换为字符数组
    char[] chs = s.toCharArray();
    for(int x = 0; x < chs.length; x++) {
      System.out.println(chs[x]);
    }
    System.out.println("-----------");

    // byte[] getBytes ():把字符串转换为字节数组
    byte[] bytes = s.getBytes();
    for(int x = 0; x < bytes.length; x++) {
      System.out.println(bytes[x]);
    }
    System.out.println("-----------");

    // 替换字母it为大写IT
    String str = "doit doit";
    String replace = str.replace("it", "IT");
    System.out.println(replace); // doIT doIT
    System.out.println("-----------");
  }
}

CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。

分割功能的方法

  • public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。

方法演示,代码如下:

/*
    分割功能的方法
    - public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组
        举例: "a,b,c,d".split(",") 把"a,b,c,d"按照逗号切割,将切割后的子字符串存入String[]中
 */
public class String_Demo03 {
  public static void main(String[] args) {
    //创建字符串对象
    String s = "aa,bb,cc";
    String[] strArray = s.split(","); // ["aa","bb","cc"]
    for(int x = 0; x < strArray.length; x++) {
      System.out.println(strArray[x]); // aa bb cc
    }
  }
}

Two:
不足:今天的知识主要是要求记忆的东西,但是我这记性一直不好,得多记多背。Object大类,String类以及其相关的:
1.转大小写;
2。开头,结尾。
3、连接
4、去掉两头空格
5、替换
6.切割

Three:

天气:小雨
心情:学习ing

Four:
期望:活学活用,能够在不借助资料的情况下完成独立写代码。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值