Java学习Day8——String类、Static关键字、Arrays类、Math类

1.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共享。

③"abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。

String str = "abc";
相当于:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。

 字符串数组、字符数组和字符串之间的转换:

(1)字符串和字符数组的转化

// 字符串转化成字符数组
  String str = "abcdefg";
  char[] ch = str.toCharArray();
  //输出a
  System.out.println(ch[0]);
  //字符数组转化成字符串
  String  strs = new String(ch);

(2)字符串和字符串数组的转化

// 字符串转化成字符串数组
  String str = "hello world";
  //根据空格把字符串隔开
  String[] array = str.split(" ");
  //输出hello
  System.out.println(array[0]);
 
  //字符串数组转化成字符串,只能通过循环
  //StringBuffer主要侧重于对字符串的变化
  StringBuffer sb= new StringBuffer();
  for(int i=0;i<array.length;i++){
       sb.append(str[i]);//追加内容到sb的末尾
  }
  String s = sb.toString();

字符串拼接问题:

注:字符串常量在拼接时,是先拼接开辟一个空间;

        字符串变量在拼接时会产生多个空间

 构造方法及编码解码问题:

  • public String() :初始化新创建的 String对象,以使其表示空字符序列。

  • public String(char[] value) :通过当前参数中的字符数组来构造新的String。

  • public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String

  • public String(byte[] bytes,String charsetName):charsetName : GBK  UTF-8 ISO-8859-1

  •   byte[] getBytes() : 将字符串转换为字节序列

  •  byte[] getBytes(String charsetName) : 根据指定的编码编码一个字符串

public class case4 {
    public static void main(String[] args) throws UnsupportedEncodingException {

        char[] chs = {'好','好','学','习'};
        //将字符数组转换为字符串
        String s1 = new String(chs);
        System.out.println(s1);

        byte[] bys = {97,98,99,100,101,102};
        //将一个字节数组转换为字符串
        String s2 = new String(bys);
        System.out.println(s2);

        String s3 = "今天天气不错";
        //获取字符串字节序列 (编码)
        byte[] bytes = s3.getBytes("GBK");

        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        //(解码)
        String s4 = new String(bytes,"gbk");
        System.out.println(s4);
    }
}

 

 常用方法:

  • public boolean equals (Object anObject) :将此字符串与指定对象进行比较。

  • public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。

  • 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 static void main(String[] args) {
        String s1 = "Java";
        String s2 = "JAVA";
        String s3 = "Java";
        String s4 = "ABCDEF";
        System.out.println("s1=s2?" + s2.equals(s1));
        System.out.println("s1=s2?" + s1.equals(s3));
        System.out.println("s1=s2?" + s2.equals(s1));
        System.out.println("s1=s2?" + s2.equalsIgnoreCase(s1));
        System.out.println("字符串的长度为:"+s1.length());
        System.out.println("s1连接s2:" + s1.concat(s2));
        System.out.println("拼接后s1为:");
        System.out.println("返回A在s2中的索引:" + s2.indexOf('A'));
        System.out.println("截取S4中索引为2、3的位置之间的内容:" + s4.substring(2, 4));
        System.out.println("返回s2中索引为2的元素:" + s2.charAt(2));
    }

 

  • public char[] toCharArray () :将此字符串转换为新的字符数组。

  • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。

  • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。

 

  

2.static关键字

调用格式:

// 访问类变量
类名.类变量名;
// 调用静态方法
类名.静态方法名(参数);
  • 静态方法可以直接访问类变量和静态方法。

  • 静态方法不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。

  • 静态方法中,不能使用this关键字。

3.Arrays类

java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等。其所有方法均为静态方法,调用起来非常简单。

  • public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。

  • binarySearch (int[] a) ; 二分查找法

  • public static String toString(int[] a) :返回指定数组内容的字符串表示形式。

4.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 class case13 {
    /*

        Math : 数学运算相关属性、方法
     */
    public static void main(String[] args) {

        System.out.println(Math.E);
        System.out.println(Math.PI);


        // abs(double a) :返回 double 值的绝对值。
        System.out.println(Math.abs(-11));

        // ceil(double a) :返回大于等于参数的最小的整数.
        System.out.println(Math.ceil(-3.14));

        // floor(double a) :返回小于等于参数最大的整数。
        System.out.println(Math.floor(-3.14));

        // round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
        System.out.println(Math.round(3.55));
        // 获取随机数

        System.out.println(Math.random());


        //Math 相关的API,计算在 -10.8 到 5.9 之间,绝对值大于 6 或者小于 2.1 的整数有多少个?

        int  count=0;
        for (int i = (int)Math.ceil(-10.8); i < Math.floor(5.9); i++) {
            if(Math.abs(i)>6 || Math.abs(i)<2.1){
                count++;
            }
        }

        System.out.println("count="+count);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值