这个星期深圳又开始疫情了,一起来准备去上班,一出门就被拉着去做核酸去了,也是我第一次做核酸,不说了,总结一下这个礼拜学了哪些Java知识点;
equals() 方法用于将字符串与指定的对象比较。
String 类中重写了 equals() 方法用于比较两个字符串的内容是否相等。
package com.jinglong.zou;
/*==是进行对象地址值比较,如果确实需要字符串的内容比较,可以使用两个方法
* public boolean equals(Object obj):参数可以是任何人对象,只有参数是一个字符串并且内容相同的才会给true,否则就是false
* 任何对象都可以用Object来接收
* equals方法具有对称性,也就是a.equals(b)和 b.equals(a)效果是一样的
* 如果比较双方一个常量一个变量,推荐把常量字符串写在前面*/
/*public boolean equalsIgnoreCase(String str)*/
public class Demo01StringEquals {
public static void main(String[] args) {
String str1="hello";
String str2="hello";
char[] charArray={'h','e','l','l','o'};
String str3=new String();
//str1与str2进行比较
System.out.println(str1.equals(str2));//true
System.out.println(str1.equals("hello"));//true
System.out.println("hello".equals(str1));
String str5=null;
System.out.println("abc".equals(str5));//推荐
// System.out.println(str5.equals("abc"));//不推荐,因为如果是null的话会引起空指针异常NullPointerException
System.out.println("---------------------------------");
String strA="JAVA";
String strB="java";
//英文字母不区分大小写
System.out.println(strA.equalsIgnoreCase(strB));//true
}
}
字符串的常用方法
跟javascript的数组方法很相似,有一些前端基础学这个还是很快的;
package com.jinglong.zou;
/*public int length()获取字符串当中的字符个数,拿到字符串长度
* public String concat(String str)将当前字符串和参数字符串拼接成为返回值新的字符串
* public char charAt(int index)获取指定索引位置的单个字符
* public int indexOf(String str)查找参数字符串在原先字符串当中首次出现索引位置,如果没有返回-1值 */
public class Demo02StringGet {
public static void main(String[] args) {
//获取字符串的长度
int length = "qwertyuioppoiuytrewq".length();
System.out.println("字符串的长度是"+length);
//拼接字符串
String str1="Hello";
String str2="Java";
System.out.println(str1.concat(str2));//新的字符串
System.out.println(str1.concat(str1));
//获取指定索引位置的单个字符
char ch="Hello".charAt(1);
System.out.println("1号索引位置的字符是"+ch);
//查找参数字符串在本来字符串当中出现第一次索引位置
String str3="HelloHello";
int index = str3.indexOf("llo");
System.out.println("第一次索引位置"+index);//2
System.out.println(str3.indexOf("abc"));//-1
}
}
截取字符串
package com.jinglong.zou;
import org.w3c.dom.ls.LSOutput;
/*public String substring(int index)截取从参数位置一直到字符串末尾,返回新字符串
* public String substring(int begin int end)截取从begin开始,一直到end结束,中间的字符串*/
public class Demo03SubString {
public static void main(String[] args) {
String str1="HelloJava";
//1个参数的Substring方法
String str2=str1.substring(5);
System.out.println(str2);//java
//2个参数的Substring方法
String str3=str1.substring(2,5);//llo
System.out.println(str3);
//字符串的内容依然是没有改变的
// strA当中保存的是地址值
String strA="Hello";
System.out.println(strA);//Hello
strA="JAVA";
System.out.println(strA);//JAVA
}
}
String当中与转换相关的常用方法
- public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值
- public byte[] getBytes():将当前字符串底层的字节数组
- public String replace():将所有出现的老字符串替换成新的字符串,返回替换之后的结果新字符串。
package com.jinglong.zou;
/*String当中与转换相关的常用方法
*
* public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值
* public byte[] getBytes():将当前字符串底层的字节数组
* public String replace():将所有出现的老字符串替换成新的字符串,返回替换之后的结果新字符串。*/
public class Demo04StringConvert {
public static void main(String[] args) {
char[] chars="hello".toCharArray();
System.out.println(chars[0]);//h
System.out.println(chars.length);//5
System.out.println("---------------------------------");
String lang1="疫情你大爷的";
String lang2=lang1.replace("疫情你大爷的","****");
System.out.println(lang2);//疫情****
}
}
split方法的参数其实是一个正则表达式.
package com.jinglong.zou;
/*split方法的参数其实是一个正则表达式.
//.输出是
aaa
bbb
ccc
* */
public class Demo05StringSplit {
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("---------------------------------");
String str2="aaa bbb ccc";
String[] array2 = str2.split(" ");
for (int i = 0; i <array2.length ; i++) {
System.out.println(array2[i]);
}
System.out.println("-----------------------------------------");
//这样的情况不会输出任何东西
// String str3="aaa.bbb.ccc";
// String[] array3 = str3.split(".");
// for (int i = 0; i <array3.length ; i++) {
// System.out.println(array3[i]);
// }
System.out.println("------------------------------------------------");
String str3="aaa.bbb.ccc";
String[] array3 = str3.split("\.");
for (int i = 0; i <array3.length ; i++) {
System.out.println(array3[i]);
}
}
}
static方法就是没有this的方法。在static方法内部不能调用非静态方法,反过来是可以的。而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途。” 这段话虽然只是说明了static方法的特殊之处,但是可以看出static关键字的基本作用,简而言之,一句话来描述就是方便在没有创建对象的情况下来进行调用(方法/变量)。很显然,被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问。static可以用来修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性能。
特点:当第一次用到本类时,静态代码执行唯一的一次 静态内容总是优先于非静态,所以静态代码块比构造方法先执行
package cn.bzu.look.dao;
public class MyObject {
//非静态变量
private String str1 ="property";
//静态变量
private static String str2 ="staticProperty";
public MyObject() {
}
//非静态方法
public void print1() {
System.out.println(str1);
System.out.println(str2);
print2();
}
//静态方法
public static void print2() {
//这一句报错,报错信息是Cannot make a static reference to the non-static field str1
System.out.println(str1);
System.out.println(str2);
/*
* 调用非静态的方法会报错,
* Cannot make a static reference to the non-static method print1() from the type MyObject
*/
print1();
}
}
Math类常用方法
//获取绝对值
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(0));
System.out.println(Math.abs(-22));
//向上取整
System.out.println(Math.ceil(51));//51
System.out.println(Math.ceil(51.1));//52
//向下取整
System.out.println(Math.floor(21.9));//21
//四舍五入
System.out.println(Math.round(32.456));//32
System.out.println(Math.round(33.768));//34