java学习笔记9:Object类,Scanner类,String类(字符串)

本节内容比较多,我用思维导图总结出来方便查阅

思维导图

在这里插入图片描述
对于以上内容我用代码来演示一遍,方便于理解

代码实现

一、Object类常用方法实现

public class MyTest {
    public static void main(String[] args) {
        Object obj = new Object();
        Object obj1 = new Object();
        boolean b = obj.equals(obj1);//默认情况下比较的是对象的引用是否相同。
        System.out.println(b);
        int i = obj.hashCode();//返回该对象的哈希码值。
        System.out.println(i);
        Class a = obj.getClass();//返回此 Object 的运行时类。
        System.out.println(a);
        String s = obj.toString();//返回该对象的字符串表示。
        System.out.println(s);
    }
}

二、Scanner类
Scanner就是用于获取键盘输入,这个比较简单;用代码实现Scanner类中hasNextXxx()和nextXxx()方法。

public class MyTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //nextXXX() 获取各种类型的数据
        // hasNextxxx() 判断是否录入的是该种数据
        System.out.println("请输入一个整数");
        boolean b = sc.hasNextInt();
        if (b) {
            int i = sc.nextInt();
            System.out.println(i);
        } else {
            System.out.println("你输入的不是整数类型");
        }
        sc.close();//释放资源
    }
}

三、String类中常用方法用代码实现
1、判断功能方法

public class MyTest {
    public static void main(String[] args) {
        boolean b = "abc".equals("ABC");//这种比较是区分大小写的
        boolean b1 = "abc".equalsIgnoreCase("ABC"); //忽略大小写的比较
        System.out.println(b);
        System.out.println(b1);
        boolean b2 = "abcdfdfdfdfdfd".contains("abc123");//判断一字符串,有没有包含在另一个字符串里面
        System.out.println(b2);  
        boolean g = "abcdefg".endsWith("");//判断字符串是否以传递进来的字符串结尾
        boolean ab = "abvdfefe".startsWith("a");//判断字符串是否以传递进来的字符串开头
        System.out.println(g);
        System.out.println(ab);
        boolean empty = "".isEmpty();//判断是否为空串
        System.out.println(empty);
        if (" ".length() == 0) {
            System.out.println("是空串");
        }
    }
}输出:
   false
   true
   false
   true
   true
   true

2、获取功能

public class MyDemo {
    public static void main(String[] args) {
        int length = "abcd".length();//获取字符串长度
        System.out.println(length);
        char c = "abc".charAt(2); //根据索引获取单个字符
        System.out.println(c);
        int e = "aesfasdfasdfdsafe".indexOf('e');//查找该字符在字符串中第一次出现的索引
        //如果找不到就会返回 -1  有时我们会拿 -1 作为判断条件
        int asd = "asdfadsfasdfsadfasdfdsa".indexOf("asd3333");
        System.out.println(e);
        System.out.println(asd);
        //lastIndexOf 从后往前检索,该字符第一次出现的索引
        int a = "asfasdfasfdasdfas".lastIndexOf("a");
        System.out.println(a);
        int f = "asfasfdsafdsafdsaf".indexOf('f', 6);//从指定索引处查找该字符
        System.out.println(f);
        String substring = "明天是圣诞节".substring(0);
        substring = "明天是圣诞节".substring(3);
        substring = "明天是圣诞节".substring(3, 5);//根据首尾索引来截取字符串,含头,不含尾
        System.out.println(substring);
    }
}

3、转换功能

 ①、  public class MyDemo {
        public static void main(String[] args) {
            byte[] byes = {97, 101, 98, 99, 100};
            String s = new String(byes); //把一个字节数组转换成字符串
            s = new String(byes, 0, 3); //把一个字节数组的一部分转成字符串 参2 起始索引 参数3 你转几个
            System.out.println(s);
            char[] chars = {'a', 'A', '你', '好'};
            String s1 = new String(chars);//把一个字符数组转成一个字符串
            s1 = new String(chars, 2, 2); //把一个字符数组的一部分,转成字符串 参数2 起始索引,参数3 你转几个
            System.out.println(s1);
            byte[] bytes = "abcde".getBytes();//把字符串转换为字节数组。
            for (int i = 0; i < bytes.length; i++) {
                System.out.println(bytes[i]);
            }
            System.out.println("-----------------------");
            byte[] bytes1 = "圣诞节".getBytes();
            String st = new String(bytes1);
            System.out.println(st);
            System.out.println("---------------------------");
            //把一个字符串,转换成字符数组
            char[] cha = "圣诞快乐".toCharArray();//把字符串转换为字符数组。
            for (int i = 0; i < cha.length; i++) {
                System.out.println(cha[i]);
            }
            String st2 = new String(cha);
            System.out.println(st2);
        }
    }
②、public class MyDemo2 {
    public static void main(String[] args) {
        //valueOf() 可以将很多类型转成字符串
        String s = String.valueOf(100);
        System.out.println(s);
        String s1 = String.valueOf(new char[]{'a', 'b', 'c'});
        System.out.println(s1);
        System.out.println("------------------");
        String s2 = "abc".toUpperCase();//把一个字符串转成大写
        System.out.println(s2);
        String s3 = "ABV".toLowerCase();//把一个字符串转成小写
        System.out.println(s3);
        String str2 = "abc" + "ccc" + "ddd" + "ffff";
        String concat = "abc".concat("ccc").concat("ddd").concat("fffff");//连接字符串
        System.out.println(concat);
    }
}
发布了24 篇原创文章 · 获赞 11 · 访问量 2059

猜你喜欢

转载自blog.csdn.net/weixin_43791069/article/details/85316376
今日推荐