栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java String 达内cbg day11

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java String 达内cbg day11

Object

hashCode():默认实现:根据对象的地址值生成一个唯一的哈希码值

toString():默认实现:打印对象的【类名+十六进制的哈希码值】
–重写后:Student自定义类打印的是类型+所有属性和属性值

equals():默认实现:比较两个对象的地址值,默认使用==比较
–重写后:比较的是两个对象的类型+所有属性和属性值

综上:如果执行的效果和Object的默认效果不同,说明子类重写了该方法

String
package cn.tedu.api;

import java.util.Arrays;
import java.util.Locale;


public class TestString2 {
    public static void main(String[] args) {
        String s1 = "abc";
        char[] c = {'a','b','c'};
        String s2 = new String(c);
        System.out.println(s1);
        System.out.println(s2);

        //测试常用方法
        
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());

        System.out.println(s1.equals(s2));//true,比较的是字符串的内容

        System.out.println(s1.length()); //3,查看当前字符串的长度

        System.out.println(s1.toUpperCase());
        System.out.println(s1.toLowerCase());

        System.out.println(s1.startsWith("a")); //判断字符串是不是以a开头的
        System.out.println(s1.endsWith("a"));//判断字符串是不是以a结尾的

        System.out.println(s1.charAt(0)); //根据下标获取字符串指定位置上的字符

        String s3 = "abcdacab";
        System.out.println(s3.indexOf("b")); //获取指定字符第一次出现的位置
        System.out.println(s3.lastIndexOf("b"));

        System.out.println(s2.concat("cxy")); //abccxy,拼接字符串,不会改变原串
        System.out.println(s2);
        String s4 = s2.concat("cxy");
        System.out.println(s4);

        String s5 = "   hh hhhh   ";
        System.out.println(s5.trim()); //用于去除字符串首尾两端的空格

        String s6 = "abcdefgh";
        System.out.println(s6.substring(3)); //deghf,从指定下标劫取子串[3,结束]
        System.out.println(s6.substring(3,6)); //def,从指定下标截取字串[3,6]

        String s7 = "afbfcfdfe";
        String[] fs = s7.split("f"); //以指定的字符串f分割字符串
        System.out.println(Arrays.toString(fs));

        System.out.println(String.valueOf(10)+10);//1010,将int类型的参数10转为String类型的"10"

        byte[] bytes = s2.getBytes();//将字符串转为bute类型的数组
        System.out.println(bytes);//[97,98,99]
    }




}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号