Java面向对象与类(三):String类,static类,Arrays类,Math类

1.String类

  • 字符串概述
  • java.lang.String 类代表字符串。Java程序中所有的字符串字面值(例如 “abc” )都可以被看作是实现此类的实 例。
  • 程序中的所有双引号字符串,都是String类的对象(无new,都是String类对象)
  • 类 String 中包括用于检查各个字符串的方法.
  • 使用步骤
  • 字符串不变 : 字符串的值在创建之后不能被更改
String s1 = "abc"; 
s1 += "d"; 
System.out.println(s1); // "abcd" 
// 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。
  • String对象不可变,可以被共享使用(字符串使用频率高)
String s1 = "abc"; 
String s2 = "abc"; 
// 内存中只有一个"abc"对象被创建,同时被s1和s2共享。
  • 字符串 等效于 char[] 字符数组,底层原理是byte[ ] 字节数组 。
例如: 
String str = "abc"; 

相当于: 
char data[] = {'a', 'b', 'c'}; 
String str = new String(data); 
// String底层是靠字符数组实现的。
  • 使用步骤
  • 查看类 : java.lang.String :此类不需要导入。
  • 直接创建,字符串直接写上双引号,就是字符串对象
//直接创建
String str4 = "Hello";
System.out.println("第4个字符串:"+str4);
  • 查看构造方法 :
  • public String() :初始化新创建的 String对象,以使其表示空字符序列。
  • public String(char[] value) :通过当前参数中的字符数组来构造新的String。
  • public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的 String。
// 无参构造 
String str = new String(); 

// 通过字符数组构造 
char chars[] = {'a', 'b', 'c'}; 
String str2 = new String(chars); 

// 通过字节数组构造 
byte bytes[] = { 97, 98, 99 }; 
String str3 = new String(bytes);
  • 字符串的常量池
  • 程序当中直接写的双引号字符串,就在字符串常量池中.
  • 对于基本类型来说,==是数值的比较
  • 对于引用类型来说,==是进行地址值的比较.
public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";

        char[] charArray = {'a','b','c'};
        String str3 = new String(charArray);

        System.out.println(str1==str2);//true
        System.out.println(str1==str3);//false
        System.out.println(str2==str3);//false
    }
  • 字符串对象存储在堆的字符串常量池中,底层实现是字节数组
  • 字符数组作为参数,jdk自动翻译为字节数组,同时自动创建一个字符串对象

在这里插入图片描述

  • 常用方法
  • 判断功能方法
  • public boolean equals (Object anObject) :参数是任何对象,任何对象均可用object接收,参数类型必须是字符串且内容相同时才返回true,将此字符串与指定对象进行比较。 此方法具有对称性,a.equals(b)和b.equals(a)等价.
public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        char[] charArray = {'H','e','l','l','o'};
        String str3 = new String(charArray);

        System.out.println(str1.equals(str2));//true
        System.out.println(str2.equals(str3));//true
        System.out.println(str3.equals("Hello"));//true
        System.out.println("Hello".equals(str1));//true
        System.out.println("===============================");

        String str5 = "Hello";
        System.out.println("Hello".equals(str5));//推荐
        System.out.println(str5.equals("Hello"));//不推荐,如果str5变化为null,报错,NullPointException
		
		String strA = "Java";
        String strB = "java";
        System.out.println(strA.equals(strB));//false
        System.out.println(strA.equalsIgnoreCase(strB));//true ,忽略大小写
        System.out.println("abc-123".equalsIgnoreCase("abc易123"));//英文字母区分大小写
    }
    }
  • public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小 写。

  • 获取功能方法

  • public int length () :返回此字符串的长度,即字符个数。

  • public String concat (String str) :将指定的字符串连接到该字符串的末尾。

  • public char charAt (int index) :返回指定索引处的 char值(索引从0开始)。

  • public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引,不存在就返回-1。

public static void main(String[] args) {
        //长度
        int length = "asdfghjkasdfghjklaasdfghjkl".length();
        System.out.println("字符串长度为:"+length);

        //拼接
        String str1 = "Hello";
        String str2 = "World";
        String str3 = str1.concat(str2);
        System.out.println(str1);//Hello
        System.out.println(str2);//world
        System.out.println(str3);//HelloWorld
        System.out.println("============================");

        char ch = "Hello".charAt(1);
        System.out.println("1号索引位置的字符为:"+ch);
        System.out.println("============================");

        String original = "HelloWorld";
        int index = original.indexOf("llo");
        System.out.println("第一次索引值是:"+index);//2
        System.out.println("HelloWorld".indexOf("abc"));

    }
  • public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。
  • public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。
public static void main(String[] args) {
        String str1 = "HelloWorld";
        String str2 = str1.substring(5);
        System.out.println(str1);//HelloWorld
        System.out.println(str2);//World
        System.out.println("==========================");

        String str3 = str1.substring(4,7);
        System.out.println(str3);//oWo
        System.out.println("==========================");

        //strA保存了地址值,由Hello的地址值变化为Java的地址值
        String strA = "Hello";
        System.out.println(strA);
        strA = "Java";
        System.out.println(strA);
    }
  • 转换功能方法
  • public char[] toCharArray () :将此字符串转换为新的字符数组。
  • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
  • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
 public static void main(String[] args) {
        //z转换为字符数组
        char[] chars = "Hello".toCharArray();
        System.out.println(chars[0]);//
        System.out.println(chars.length);
        System.out.println("========================");

        //转换为字节数组
        byte[] bytes = "abc".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
        System.out.println("========================");

        //敏感词屏蔽
        String lang1 = "会不会玩儿呀!你大爷的!你大爷的!你大爷的!!!";
        String lang2 = lang1.replace("你大爷的","****");
        System.out.println(lang2);
    }
  • 分割功能方法
  • public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
public static void main(String[] args) {
        String str1 = "aaa,bbb,ccc";
        String[] array1 = str1.split(",");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        System.out.println("=======================");
   }     

2.static类

  • 概述
  • static 关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属 于某个对象的。也就是说,既然属于类,就可以不靠创建对象来调用了。
  • 定义和使用格式
  • 类变量
  • 当 static 修饰成员变量时,该变量称为类变量。该类的每个对象都共享同一个类变量的值。任何对象都可以更改 该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行操作。
  • 类变量:使用 static关键字修饰的成员变量。
//定义格式
static 数据类型 变量名;
static int numberID;
  • 静态方法
  • 当 static 修饰成员方法时,该方法称为类方法 。静态方法在声明中有 static ,建议使用类名来调用,而不需要 创建类的对象。调用方式非常简单。
  • 类方法:使用 static关键字修饰的成员方法,习惯称为静态方法。
//格式
修饰符 static 返回值类型 方法名 (参数列表){ 
	// 执行语句 
}
//示例
public static void showNum() { 		   System.out.println("num:" + numberOfStudent); 
}
  • 注意事项
  • 静态方法可以直接访问类变量和静态方法。
  • 静态方法不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。
  • 静态方法中,不能使用this关键字。
  • 调用格式
  • 被static修饰的成员可以并且建议通过类名直接访问。虽然也可以通过对象名访问静态成员,原因即多个对象均属 于一个类,共享使用同一个静态成员,但是不建议,会出现警告信息。
//格式
// 访问类变量 
类名.类变量名; 

// 调用静态方法 
类名.静态方法名(参数)public class StuDemo2 { 
	public static void main(String[] args) { 
		// 访问类变量 	System.out.println(Student.numberOfStudent); 
		// 调用静态方法 
		Student.showNum(); 
		} 
	}
  • 静态原理图解
  • 是随着类的加载而加载的,且只加载一次。
  • 存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。
  • 它优先于对象存在,所以,可以被所有对象共享。
    在这里插入图片描述
  • 静态代码块
  • 静态代码块:定义在成员位置,使用static修饰的代码块{ }。
  • 位置:类中方法外。
  • 执行:随着类的加载而执行且执行唯一的一次(第二次不执行静态代码块),优先于main方法和构造方法的执行。
//格式
public class ClassName{ 
	static { 
	// 执行语句 
	} 
}
public class Person {
    static {
        System.out.println("静态代码块");
    }

    public Person(){
        System.out.println("构造方法执行!!!");
    }
}
...
 public static void main(String[] args) {
        Person one = new Person();
    }

3.Arrays类

  • 概述
  • java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等。其所有方法均为静态方法.
  • 操作数组方法
  • public static String toString(int[] a) :返回指定参数数组内容的字符串表示形式,按照字符串默认格式。
  • public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
public static void main(String[] args) {
        int[] intArray = {10,20,30};
        //toString
        String intStr = Arrays.toString(intArray);
        System.out.println(intStr);//[10, 20, 30]

        //sort
        int[] array1 = {2,1,3,10,5};
        Arrays.sort(array1);
        System.out.println(Arrays.toString(array1));
    }

4.Math类

  • 概述
  • java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。
  • 基本运算方法
  • public static double abs(double a) :返回 double 值的绝对值。
  • public static double ceil(double a) :返回大于等于参数的最小的整数。向上取整.
  • public static double floor(double a) :返回小于等于参数最大的整数。向下取整.
  • public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
public static void main(String[] args) {
        //abs
        System.out.println(Math.abs(-3.14));//3.14

        //ceil
        System.out.println(Math.ceil(3.9));//4.0

        //floor
        System.out.println(Math.floor(3.5));//3.0

        //round
        System.out.println(Math.round(3.4));//3
        System.out.println(Math.round(3.5));//4
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Siri_only

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值