JAVA SE basic feed --06: static and the other three commonly used classes

String类  Arrays类  Math类

0x01.String class

:( where the package does not need to guide package)

java.lang.String

Construction method:

public String();//无参数,表示空字符序列

public String(char[] value);//通过当前字符数组来创建一个字符串

public String(byte[] bytes);//通过使用默认的字符集解码当前参数的字节数组来构造一个字符串

Create an object to use:

String str=new String();
String str;

Common methods:

public int length();//返回字符串的长度

public String concat(String str);//将指定字符串接到当前字符串的末尾,返回接好后的字符串

public char charAt(int index);//返回字符串指定索引处的char值(索引从0开始)

public int indexof(String str);//返回指定字符串第一次出现在当前字符串的索引值

public String subString(int beginIndex);//返回从beginIndex开始截取到末尾的字符串

public String subString(int beginIndex,int endIndex);
//返回从beginIndex截取到endIndex的字符串,不包含endIndex

public char[] toCharArray();//将此字符串转换为字符数组

public type[] getBytes();//使用默认的字符集将该字符串编码转换为新的字节数组

public String replace(charSequence target,charSequence replaxement);
//将与targrt匹配的字符串用replacement替换,返回新的字符串

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

Description: by creating a string can not be modified, but a changed, new instructions are generated.

0x02.static keyword

  • Description: static keyword can be used to modify the member variables and member methods, modified members belong to the class, you can not create objects to use .

Class variables:

Definition: Use the static keyword modified member variable called class variables. Any object can change the value of the class.

format:

static 数据类型 变量名

Class Methods:

Definition: a method using a modified static member called class methods. You do not need to create an object you can use this method.

format:

修饰符 static 返回值类型 方法名(参数列表){
        ......
}
类名.方法名();//完成调用

Precautions:

1. Static methods can not use this keyword.

2. ordinary static methods can not directly access member variables and member methods.

3. The method of ordinary members can access static methods.

4. A static method can directly access static methods and static variables.

0x03.Arrays class

Whereabouts packaging:
 

java.util.Arrays
  • Description: This class contains various methods for manipulating arrays, are static methods can be called directly, you do not need to create the object.
  • Common methods:
  • public static void sort (int[] a);
    //将int型数组按升序排序 Arrys没有直接提供降序排序,需要用升序来获得
    //整数,浮点数,char型都可以这样使用
    
    public static Sting toString(int[] a);
    //将数组内容转换为字符串型
    
    public static boolean equals(int[] a,int[] b);
    //比较数组元素是否相等
    
    public static void fill(int a[],int num);
    //将指定数据赋值给数组内的每个元素
    

    0x04.Math class

  • Whereabouts packaging:
java.lang.Math

Description: This class includes basic math methods are static methods.

Common methods:

public static int abs(int a);
//返回a的绝对值,同样适用其它基本方法

public static float ceil(float a);
//返回大于等于a的最小整数

punlic static float floor(float a);
//返回小于等于a的最大整数,高斯取整

public static int round(float a);
//对a四舍五入

 

 

 

This chapter ends.

 

Published 19 original articles · won praise 7 · views 418

Guess you like

Origin blog.csdn.net/ATFWUS/article/details/104280471