学习java第十七天
String类
String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享
常用的构造方法
1、public String() 创建一个空白的字符串,不包含任何内容
2、public String(char[] array) 根据字符数组来创建字符串
3、public String(byte[] array) 根据字节数组来创建字符串
最直接的方式:
String str=“class5”;
例1
package demo01;
public class Demo01String {
public static void main(String[] args) {
String str1 = new String();//小括号留空,没有内容。
System.out.println("第一个字符串:"+str1);
System.out.println("====================");
//字符数组
char[] chararray = new char[10];
System.out.println(chararray);
String str2 = new String(chararray);
System.out.println(str2);
System.out.println("====================");
//字节数组
byte[] bytearray = {10,20,30};
System.out.println(chararray);
String str3 = new String(chararray);
System.out.println(str3);
//直接创建字符串
//String str4 = new
String str4 = "class5";
System.out.println(str4);
}
}
字符串之间的比较
方法一:public Boolean equals(Object obj);(参数可以是任何对象,只有参数是一个字符串并且内容相同时才会是true,否则返回false)
方法二:Public Boolean equalsIgnoreCase(String str);忽略大小写,进行内容比较
package demo1;
/*
* 字符串常量池
* int a=5
* int b=6
*
* 对于基本类型==比较的是值
* 对于引用类型==比较的是地址值
*/
public class Stringpool {
public static void main(String[] args) {
String str1="abc";
String str2="abc";
System.out.println(str2);
char[] chararray= {'a','b','c'};
String str3=new String(chararray);
System.out.println(str1==str2);
System.out.println(str1==str3);
System.out.println(str2==str3);
str2="cde";
System.out.println(str2);
}
}
public boolean equal(Object obj) 方法
public boolean equals(Object obj)指示一些其他对象是否等于此。 该equals类方法Object实现对象上差别可能性最大的相等关系; 也就是说,对于任何非空的参考值x和y ,当且仅当x和y引用相同的对象( x == y具有值true )时,该方法返回true 。 但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了,他们中的equals方法用于比对相同包装类中的值是否相等。
请注意,无论何时覆盖该方法,通常需要覆盖hashCode方法,以便维护hashCode方法的通用合同,该方法规定相等的对象必须具有相等的哈希码。
package demo02;
public class Demo01StringEquals {
public static void main(String[] args) {
String str1 = "Why";
String str2 = "Why";
System.out.println(str2);
char[] chararray = {'W','h','y'};
String str3 = new String(chararray);
System.out.println(str1.equals(str2));
System.out.println(str2.equals(str3));
System.out.println (tr1.equals(str3));
System.out.println(str1.equals("Why"));
System.out.println("Why".equals(str1));//建议这样写
System.out.println("====================");
String str4 = null;
// System.out.println(str1.equals("Why"));
System.out.println("Why".equals(str4));
System.out.println("====================");
String str5 = "why";
// System.out.println(str1.equals("Hio"));
System.out.println("why".equals(str5));
System.out.println("====================");
System.out.println("Why");
}
public int length ( ) 方法
package demo2;
public class StringGet {
public static void main(String[] args) {
//字符串长度
int length="ewifewnferiejg4fgjgh".length();
System.out.println("字符串长度"+length);
//字符串拼接
String str1="Hello";
String str2="class5";
String str3=str1.concat(str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
//获得指定索引位置的单个字符
char ch="Hello".charAt(4);
System.out.println("0号索引位置的字符是:"+ch);
//查找某个字符串第一次出现的位置
String origanl="HelloworldHelloworldHelloworld";
int index=origanl.indexOf("llo");
System.out.println(index);
System.out.println(origanl.indexOf("class"));
}
}
分割字符串
public String[] split(String)
package demo2;
/*
* 分割字符串
* public String[] split (String)
*/
public class StringSplit {
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]);
}
}
}
SubString方法
public class Demo03SubString {
public static void main(String[] args) {
String str1= "helloclass5";
String str2=str1.substring(5);
System.out.println(str1);
System.out.println(str2);
String str3=str1.substring(2,5);
System.out.println(str3);
}
}
Arrays类
一、Arrays类的定义
Arrays类位于 java.util 包中,主要包含了操纵数组的各种方法
使用时导包:import java.util.Arrays
二、Arrays常用函数(都是静态的)
1.void Arrays.sort()
void Array.sort(Object[] array)
功能:对数组按照升序排序
Arrays.sort(Object[] array, int from, int to)
功能:对数组元素指定范围进行排序(排序范围是从元素下标为from,到下标为to-1的元素进行排序)
2…Arrays.fill(Object[] array,Object object)
功能:可以为数组元素填充相同的值
Arrays.fill(Object[] array,int from,int to,Object object)
功能:对数组的部分元素填充一个值,从起始位置到结束位置,取头不取尾
3.Arrays.toString(Object[] array)
功能:返回数组的字符串形式
4.Arrays.deepToString(Object[][] arrays)
功能:返回多维数组的字符串形式
package demo03;
import java.util.Arrays;
public class Demo01Arrays {
public static void main(String[] args) {
int[] intArray = {10,20,30};
System.out.println(intArray);
String intStr = Arrays.toString(intArray);
System.out.println(intStr);
int[] array1 = {1,2,3,4,5};
System.out.println(Arrays.toString(array1));
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
String[] array2 = {"zzz","xxx","ccc"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));
}
}
Math
public class Demo03Math {
public static void main(String[] args) {
//绝对值
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(0));
System.out.println(Math.abs(-3.14));
System.out.println("========");
//向上取整
System.out.println(Math.ceil(3.14));
System.out.println(Math.ceil(3.91));
System.out.println(Math.ceil(3.00001));
System.out.println("========");
//向下取整
System.out.println(Math.floor(3.14));
System.out.println(Math.floor(3.91));
System.out.println(Math.floor(3.00001));
System.out.println("========");
//四舍五入
System.out.println(Math.round(3.14));
System.out.println(Math.round(3.91));
}
}
StaticFiled
public class Student {
private int id;
private String name;
private int age;
private static String room;
public Student(){}
public Student(int id, String name, int age, String room){
super();
this.id=id;
this.name=name;
this.age=age;
this.room=room;
}
public Student(String name,int age){
super();
this.name=name;
this.age=age;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
}
package demo04;
/*
* 如果一个成员变量用static关键字修饰,那么这个内容不再属于对象自己,而是属于类的。所以凡是本类的对象,都共享一份。
*/
public class Demo01StaticFiled {
public static void main(String[] args) {
Student one = new Student("廖磊",19);
one.setRoom("3306教室");
System.out.println(one.getName()+" "+one.getAge()+" "+one.getRoom());
Student two = new Student("juju",18);
System.out.println(two.getName()+" "+two.getAge()+" "+two.getRoom());
}
}
StaticMethod
public class MyClass {
int num;
static int numstatic;
public void method() {
System.out.println("这是成员方法");
}
public static void methodStatic() {
System.out.println("这是静态方法");
}
}
如果用static关键字修饰成员方法 , 变成了静态方法,静态方法不属于对象,属于类。
可以通过对象来调用 也可以通过类直接调用
package demo04;
public class Demo02StaticMethod {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.method();//普通方法,必须通过对象类调用
obj.methodStatic();//可以通过对象类调用 正确 不推荐
MyClass.methodStatic();//可以通过类直接调用 正确 推荐
}
}