####String
String类代表字符串,java中所有的字符串都是用String类实现的。
特点:
1 字符串是常量,创建以后不可以更改。
2 正因为String对象是不可变的 所以他可以共享。
3 字符串的效果上相当于char[ ]字符数组,但底层是byte[ ]字节数组。
例如: String str = "abc";
相当于: char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。
方法——判断方法
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小 写。
== 是进行对象的地址值比较,如果确实需要字符串内容比较 可以用以上两个方法。
equals方法参数可以是任何对象,只要参数是一个字符串并且内容相同才会给true,否则返回false。
注意事项:
equals方法具有对称性,也就是说a.equals(b)和b.equals(a)效果是一样的。如果比较双方一个是常量,一个是变量,推荐把常量字符串写在前面。
推荐:“abc”.equals(str) 不推荐:str.equals(“abc”)
代码演示:
public class String_Demo01 {
public static void main(String[] args) { // 创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}
方法——获取功能方法
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 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 itheima");
System.out.println(s2);// helloworld**hello itheima
// 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 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 = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}
方法——分隔功能方法
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
代码演示:
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
}
}
}
####static
它可以修饰成员变量和成员方法,被修饰的成员属于类,而不是只属于某个对象了,所以既然属于类就可以不靠创建对象来调用。
格式
static 数据类型 变量名称;
当 static 修饰成员变量时,该变量称为类变量。
当 static 修饰成员方法时,该方法称为类方法。
格式:
修饰符 static 返回值类型 方法名(参数列表){
//执行语句
}
public static void methodStatic(){
System.out.println("这是一个静态方法");
}
无论是成员变量还是成员方法 如果有static修饰 都推荐使用类名称进行调用:
静态变量:类名称.静态变量;
静态方法:类名称.静态方法();
注意事项:
1 静态不能直接访问非静态。
原因:因为在内存中是现有静态内容 后有非静态内容。
2 静态方法中不能使用this关键字。
原因:this代表当前对象,通过谁调用的方法 谁就是当前对象。
####静态代码块
静态代码块:在成员为止使用static修饰的代码块{}
特点:随着类的加载执行而且只执行一次,优先于main方法和构造方法的执行。
用途:用来一次性的对静态成员变量进行赋值。
public class ClassName{
static{
//执行语句。
}
}
####Arrays
是一个与数组相关的工具类,里面有大量静态方法,用来实现数组的常见操作。
方法——操作数组的方法
public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
例子
public static void main(String[] args) {
// 定义int 数组
int[] arr = {2,34,35,4,657,8,69,9};
// 打印数组,输出地址值
System.out.println(arr); // [I@2ac1fdc4
// 数组内容转为字符串
String s = Arrays.toString(arr);
// 打印字符串,输出内容
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9] }
public static void main(String[] args) {
// 定义int 数组
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2};
System.out.println("排序前:"+ Arrays.toString(arr));
// 排序前:[24, 7, 5, 48, 4, 46, 35, 11, 6, 2]
// 升序排序
Arrays.sort(arr);
System.out.println("排序后:"+ Arrays.toString(arr));
// 排序后:[2, 4, 5, 6, 7, 11, 24, 35, 46, 48] }
####Math类
java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。
方法——基本运算的方法
public static double abs(double a) :返回 double 值的绝对值。
double d1 = Math.abs(‐5); //d1的值为5
double d2 = Math.abs(5); //d2的值为5
public static double ceil(double a) :返回大于等于参数的小的整数。
double d1 = Math.ceil(3.3); //d1的值为 4.0
double d2 = Math.ceil(‐3.3); //d2的值为 ‐3.0
double d3 = Math.ceil(5.1); //d3的值为 6.0
public static double floor(double a) :返回小于等于参数大的整数。
double d1 = Math.floor(3.3); //d1的值为3.0
double d2 = Math.floor(‐3.3); //d2的值为‐4.0
double d3 = Math.floor(5.1); //d3的值为 5.0
public static long round(double a) :返回接近参数的 long。(相当于四舍五入方法)
long d1 = Math.round(5.5); //d1的值为6.0
long d2 = Math.round(5.4); //d2的值为5.0