JAVA基础(Object、String、StringBuffer、Arrays,Interger)

自定义类初始化方法和属性

package com.tqh.day11;

public class Student {
    private String name;
    private int age;
    public Student() {
        
    }
    public Student(String name,int age) {
        super();
        this.name = name;
        this.age = age;
    }
    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;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "class:"+this.getClass().getName()+" "+
                "name:"+this.name+" "+
                "age:"+this.age;
    }
    
    
}

Object

        //获取哈希码值
        Object object = new Object();
        int hashCode = object.hashCode();
        System.out.print(hashCode);
        
        //获取真实类的全名称
        Class class1 = object.getClass();
        class1.getName();
        System.out.print(class1);

        //toString就是类描述
        Student student = new Student("小王", 22);
        System.out.print("\n"+student.toString());
        
        //比较对象中的属性值
        Object object1 = new Object();
        object.equals(object1);
        

String


        String string = "abcd";
        string = "wahaha";
        System.out.print(string);
        
        String string1 = "acc";
        
        /**************字符串判断****************/
        
        //比较字符串内容是否相等区分大小写
        string.equals(string1);
        
        //比较字符串内容是否相等忽略大小写
        string.equalsIgnoreCase(string1);
        
        //判断大字符串是否包含小字符串
        string.concat(string);
        
        //判断字符串是否是以某个字符串开头
        string.startsWith("a");
        
        //判断字符串是否以某个字符串结尾
        string.endsWith("a");
        
        //判断字符串是否为空
        string.isEmpty();
        
        /**************字符串获取****************/
        
        //获取字符串长度
        string.length();
        
        //获取置顶索引位置
        string.charAt(0);
        
        //返回字符第一次索引位置
        string.indexOf('a');
        string.indexOf("a");
        
        //返回字符串从指定位置开始后出现的索引
        string.indexOf("a", 1);
        
        //从指定位置开始截取
        string.substring(2);
        
        //从开始位置和结束位置截取
        string.substring(1, 2);
        
        /**************字符串的转换****************/
        
        //把字符串转为字节组
        string.getBytes();
        
        //把字符串转为字节数组
        string.toCharArray();
        
        //把字符数组转为字符串
        char[] arr= {'a','b','c'};
        String.valueOf(arr);
        
        //把int数据转为字符串
        String.valueOf(10);
        
        //把字符串转成小写
        string.toLowerCase();
        
        //把字符串转成大写
        string.toUpperCase();
        
        //字符串拼接
        string.concat("a");
        
        /**************字符串的替换,比较和其他****************/
        
        //将a替换成b
        string.replace('a', 'b');
        string.replace("a", "aaa");
        
        //除去字符串前空格和后空格
        string.trim();
        
        //按字典顺序比较两个字符串
        string.compareTo(string1);
        string.compareToIgnoreCase(string1);
        
        //字符串拼接
        string = string+"hahaha"+1+'a';

StringBuffer

         //构造
        StringBuffer sb = new StringBuffer();
        StringBuffer sb1 = new StringBuffer(10);
        StringBuffer sb2 = new StringBuffer("hahaha");
    
        //返回当前容量
        sb.capacity();
        //返回长度(字符数)
        sb.length();
        
        /*****常用功能******/
        sb.append("haha");
        sb.delete(0,2);
        sb.replace(0, 2, "很强势");
        sb.reverse();//反转
        //从指定位置到末尾截取
        sb.substring(3);
        //截取,开始位置,结束位置
        sb.substring(0, 3);
        
        //string stringbuffer互相转换
        String str = sb.toString();
        StringBuffer sb3 = new StringBuffer("hahaha");
        
        //StringBuilder线程不安全,StringBuffer线程安全,String不可变
        

Arrays


        int[]a = {1,99,2,3,11,4,5,6,7};
        //将数组转为string
        String string = Arrays.toString(a);
        System.out.println(string);
        //将数组排序
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        //查找数组中的元素
        System.out.println(Arrays.binarySearch(a, 99));

Integer

        //int转string
        Integer integer = new Integer(10);
        integer.toString();
        
        //string转int
        Integer integer2 = new Integer("12");
        int newa = integer2.intValue();
        Integer.parseInt("12");
        
        //自动装箱
        Integer ii = 100;
        ii+=200;

你可能感兴趣的:(JAVA基础(Object、String、StringBuffer、Arrays,Interger))