Java之Object类、Scanner类、String类

API

API概述:应用程序编程接口
Java API:就是Java提供给我们使用的类,这些类将底层的实现封装了起来;

object类

object类是所有类的顶层父类,所有类是直接或者间接继承自该类;
public Object():子类的构造方法 默认访问的是父类的无参构造方法
1.public int hashCode():返回该对象的哈希码值。默认情况下,会根据该对象的地址值来计算。
注意:不同对象的hasCode()一般不同,但是同一对象的hasCode()一定相同;hasCode()返回的值不是对象的实际地址值,可以理解为逻辑地址值;

    public static void main(String[] args) {
        Object o1 = new Object();
        System.out.println(o1.hashCode());
    }
}

2.public final Class getClass():返回此Object运行时类,可以通过Class类中的一个方法,获取对象的真实类的全名称;
3.toString():返回该对象的字符串表示
注意:由于默认情况下的数据对我们来说没有意义,一般建议重写该方法

    public static void main(String[] args) {
        Object o1 = new Object();
        System.out.println(o1.hashCode());
        System.out.println(o1.getClass());
        System.out.println(o1.toString());
        
    }
}

4.equals():默认情况下比较的是对象的引用是否相同;由于比较对象的引用没有意义,一般建议重写该方法。一般用于比较成员变量的值是否相等

    public static void main(String[] args) {
        Object obj1 = new Object();
        obj1="zhangsan";
        Object obj2 = new Object();
        obj2="lisi";
        Object obj3 = new Object();
        obj3="zhangsan";
        boolean equ = obj1.equals(obj2);
        boolean equ1 = obj1.equals(obj3);
        System.out.println(equ);
        System.out.println(equ1);
    }
}

Scanner类

Scanner用于获取用户键盘录入的数据
基本格式:1.hasNextXxx() 判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等
2.nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同


public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        sc.nextInt();
       sc.next();
    }
}

Scanner获取数据出现的小问题及解决方案:
在录入数据过程中,先录入int,再录取String类型的值,会出现一个问题
由于录入int值后 换行敲击回车,系统会将回车作为String类型的值当做输入 从而结束本次录入
解决方法:第一种:先获取一个数值后,在创建一个新的键盘录入对象获取字符串
第二种:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么

String类

字符串是由多个字符组成的字符序列,字符串可以看作是字符数组
注意:1.字符串字面值"abc"也可以看成是一个字符串对象
2.字符串是常量,一旦被创建,就不能被改变
public String( byte[] bytes):把字节数组转成字符串
public String( byte[] bytes, int index, int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String( char[] value):把字符数组转成字符串
public String( char[] value, int index, int count):把字符数组的一部分转成字符串

    public static void main(String[] args) {
        String s = new String();//空参构造
        System.out.println();
        System.out.println("-----------------------------------");
        String s1 = new String("abcd");
        System.out.println(s1);
        byte [] b={65,66,67,68,69,70,71};
        String s2 = new String(b);
        String s3 = new String(b, 0, 3);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println("-----------------------------------");
        char [] c={65,66,67,68,69,70,71};
        String s4 = new String(c);
        System.out.println(s4);
        String s5 = new String(c,0,2);
        System.out.println(s5);

    }
}

String类的判断功能
public int length(): 获取字符串的长度
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引
public int indexOf(int ch,int fromIndex): 返回指定字符在此字符串中从指定位置后第一次出现处的索引
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。

    public static void main(String[] args) {
//        public int length():							    获取字符串的长度。
//        public char charAt(int index):					获取指定索引位置的字符
//        public int indexOf(int ch):						返回指定字符在此字符串中第一次出现处的索引。
//        public int indexOf(String str):					返回指定字符串在此字符串中第一次出现处的索引。
//        public int indexOf(int ch,int fromIndex):			返回指定字符在此字符串中从指定位置后第一次出现处的索引。
//        public int indexOf(String str,int fromIndex):		返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
//        可以顺带提一下lastIndexOf系列
//        public String substring(int start):				从指定位置开始截取字符串,默认到末尾。
//        public String substring(int start,int end):		从指定位置开始到指定位置结束截取字符串。
        String s = new String();
        s="ChioaR";
        System.out.println(s.length());
        System.out.println(s.charAt(5));
        String s1 = new String();
        s1="aabbcccd";
        System.out.println(s1.indexOf('c'));
        System.out.println(s1.indexOf("bccc"));
        System.out.println(s1.indexOf('d',5));
        System.out.println(s1.indexOf("ccc",1));
        System.out.println(s1.substring(6));
        System.out.println(s1.substring(3,4));
    }
}

字符串的遍历

    public static void main(String[] args) {
        String s = new String();
        s="人生若只如初见,何事秋风悲画扇";
        for (int i = 0; i <s.length(); i++) {
            char c=s.charAt(i);
            System.out.print(c+" ");
        }
    }
}

String类的转换功能
public byte[] getBytes(): 把字符串转换为字节数组
public char[] toCharArray(): 把字符串转换为字符数组
public static String valueOf(char[] chs): 把字符数组转成字符串
public static String valueOf(int i): 把int类型的数据转成字符串
注意:String类的valueOf方法可以把任意类型的数据转成字符串
public String toLowerCase(): 把字符串转成小写
public String toUpperCase(): 把字符串转成大写
public String concat(String str): 把字符串拼接

    public static void main(String[] args) {
//        public byte[] getBytes():					             把字符串转换为字节数组
//        public char[] toCharArray():					     	 把字符串转换为字符数组
//        public static String valueOf(char[] chs):		         把字符数组转成字符串
//        public static String valueOf(int i):				     把int类型的数据转成字符串
//        注意:String类的valueOf方法可以把任意类型的数据转成字符串
//        public String toLowerCase():					       把字符串转成小写
//        public String toUpperCase():					       把字符串转成大写
//        public String concat(String str):				       把字符串拼接
        String s = new String();
        s="abcdefg";
        byte[] b = s.getBytes();
        for (int i = 0; i <b.length ; i++) {
            System.out.print(b[i]+" ");
        }
        System.out.println();
        char[] c=s.toCharArray();
        for (int i = 0; i <c.length ; i++) {
            System.out.print(c[i]+" ");
        }
        System.out.println();
        char[] c3=s.toCharArray();
        String c1 = String.valueOf(c3);
        System.out.println(c1);
        int i=999;
        String s2=String.valueOf(i);
        System.out.println(s2);
        String s5 = new String();
        s5="abcdABCD";
        System.out.println(s5.toLowerCase());
        System.out.println(s5.toUpperCase());
        System.out.println(s.concat(s5));

    }

}

```public class Test3 {
    public static void main(String[] args) {
//        把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
        String s = new String();
        s = "aesgesgsescASGESG";
        System.out.println(s.substring(0,1).toUpperCase().concat(s.substring(1,s.length()).toLowerCase()));
    }
}

String类的其他功能
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
public String trim() 去除两端空格
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
如果连个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较

猜你喜欢

转载自blog.csdn.net/chioaR/article/details/83514978