Java入门-06、String类、static、Arrays类

String类

String类:

  • 一旦创建,不会改变
  • 不发生改变,因此被共享
  • 等效于字符数组

字符串类的底层实现就是字符数组

 public static void main(String[] args) {
        //创建了对象“hello",hello不会发生改变
        //s和s1对象都是hello,故hello共享
        String s = "hello";
        String s1 = new String("hello");
        //等效于字符数组
        char[] arr = {'h', 'e', 'l', 'l', 'o'};
        String s2 = new String(arr);
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
    }

字符串类的方法:
1.获取字符串的长度:
      字符串对象.length()
2.拼接字符串:
      字符串对象1.concat(字符串对象2)
3.字符索引:
      根据位置找字符: charAt(int index)
      根据字符找位置: indexOf(字符串)
4.截取字符串:
      截取index后面的字符串:substring(int index)
      截取从begin开始到end之间的字符串:substring(int begin,int end)
5.将字符串转变成字符数组:
      public char[] toCharArray()
6.将字符串转变成新的字节数组:
      public byte[] getBytes()
7.字符串的替换:
      public String replace(String a,String b) 后者替换前者
8.字符串的分割:
      public String[] spilt(String regex) 将字符串中以字符regex进行切分

 public static void main(String[] args) {
        //获取字符串长度
        String s = "helloeqweqweqweqweqwdasaf";
        System.out.println(s.length());

        String s1 = "hello";
        String s2 = "world";
        s1 = s1.concat(s2);
        System.out.println(s1);

        //字符索引  注意不能越界访问
        //根据位置找字符: charAt(位置)
        String s3 = "helloworld";
        System.out.println(s3.charAt(1));//输出的是e
        System.out.println(s3.charAt(2));//输出的是l

        //根据字符找位置: indexOf(字符串)
        System.out.println(s3.indexOf("l"));//字符串中有3个'l',仅显示首个l的位置
        System.out.println(s3.indexOf("wor"));//字符串中仅会显示字符串第一个字符的位置
        System.out.println(s3.indexOf("dd"));//找不到字符串,返回-1


        //截取字符串:substring(int index)
        String s4 = "HelloWorld!";
        String s5 = s4.substring(5);//从第五个开始到结尾的字符串 "World!"
        System.out.println(s5);
        //substring(int begin,int end)
        String s6 = s4.substring(2, 6);//从第二个开始到第六个,但是不包含第六个 ”lloW”
        System.out.println(s6);

        //将字符串转变字符数组  public char[] toCharArray()
        String s7 = "helloworld";
        char[] c = s7.toCharArray();
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);
        }

        //将字符串转变成新的字节数组 public byte[] getBytes()
        String s8 = "abc";
        byte[] b = s8.getBytes();
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }

        //字符串的替换 前面的字符串换成后面的字符串
        String s9 = "hellojava";
        String replace1 = s9.replace("hello", "Hello");
        System.out.println(replace1);
        String replace2 = s9.replace("hello", "***");//语言和谐

        //字符串的切分
        String s10 = "abc&def&ghi&java";
        String[] strArr = s10.split("&");//["abc","def","ghi","java"]
        for (int i = 0; i < strArr.length; i++) {
            System.out.println(strArr[i]);
        }

    }

字符串类的判断方法:
equals比较格式:字符串对象1.equals(字符串对象2)  区分大小写
      相等则返回 true
      不等则返回 false
equalsIgnoreCase比较格式:字符串对象1.equalsIgnoreCase(字符串对象2)  不分大小写
       为真则返回 true
       为假则返回 false

注意:"=="不能够比较字符串对象1和字符串对象2的内容
因为字符串对象是引用类型,"=="该符号比较地址值,所以地址值不同返回false

 public static void main(String[] args) {
        //观察下列结果
        String s = "hello";
        System.out.println(s.equals("hello"));
        System.out.println(s.equals("Hello"));

        System.out.println(s.equalsIgnoreCase("hello"));
        System.out.println(s.equalsIgnoreCase("HELLO"));

//      引用类型“==”比较地址值
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        String s4 = new String("hello");
        System.out.println(s1 == s2);//true   hello共享
        System.out.println(s1 == s3);//false  new创建了新的对象,地址值发生改变
        System.out.println(s1 == s4);//false  new创建了新的对象,地址值发生改变
        System.out.println(s3 == s4);//false  new创建了新的对象,地址值发生改变

    }

static

Static使用方法:
1、当static修饰成员变量时,该变量为类变量(习惯称为静态变量)。每个对象都共用一个类变量,
        即任何对象都可以改变类变量的值。
2、当static修饰成员方法时,该方法为类方法(习惯称为静态方法)。该方法的调用不需要创建对象,
        直接类名加方法名就可以调用。
3、static静态代码块,在类的加载中首先执行的就是静态代码块里面的指令,优先于main方法
        便于初始化数据等作用,且只加载一次

class Demo1{
    public static int num=0;
    public static void PrintDemo(){
        System.out.println("静态方法的调用!");
    }

        }
        
public class Static {

    static{
        System.out.println("我是静态代码块");

    }

    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.num++;
        System.out.println(demo1.num);
        Demo1 demo2 = new Demo1();
        demo2.num=10;
        System.out.println(demo2.num);

        Demo1.PrintDemo();

    }
}

Arrays类

Arrays类中有许多操作数组的方法,譬如说排序,搜索等
其所有方法都是静态方法,调用非常简单
        简单的操作:
1、将数组内容转成字符串
public static String toString(int []a);
2、数组排序,升序
Arrays.sort(数组名);

import java.util.Arrays;
public static void main(String[] args) {
        int[] Arr = {1, 9, 3, 10, 5, 6, 7};
        //转换成字符串
        String s = Arrays.toString(Arr);
        System.out.println(s);

        //排序方法调用sort(),升序
        Arrays.sort(Arr);
        String s1 = Arrays.toString(Arr);
    }
发布了14 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/NeverFG/article/details/97612554
今日推荐