第十一节包装类及相关面试题

常用类:’
String Math System StringBuffer StringBuilder Date Calender
1,基本数据类型–四种八类
数据类型分类: 基本数据类型 引用数据类型
Person person =new Person();数组 类
2.包装类
8种基本数据类型对应的包装类 –类类型
注意事项:
1. 8种基本数据类型中有6种是和数组相关的,他们父类是number
2.包装类被final修饰,所以不能被继承
3.装箱和拆箱
将数据类型从基本数据类型,转为包装类 过程 叫装箱
从包装类转为基本数据类型的过程 叫拆箱
在 jdk1.5后,出现了自动装箱和拆箱
Integer i =5;//Integer i=new Integer(5);
int j=i;//int j=i.intValue();

4.String 类–描述字符串
String str=”今天天气真不好!”

关于常量池介绍非常好
http://www.jianshu.com/p/c7f47de2ee80
常量池范围-128–127

Character
int compareTo(Character anotherCharacter) 根据数字比较两个 Character 对象。

boolean equals(Object obj) 将此对象与指定对象比较。

static boolean isDigit(char ch) 确定指定字符是否为数字。

static boolean isLetter(char ch) 确定指定字符是否为字母。

static boolean isLowerCase(char ch) 确定指定字符是否为小写字母。

static boolean isUpperCase(char ch) 确定指定字符是否为大写字母。

static Character valueOf(char c) 返回一个表示指定 char 值的 Character 实例
Integer
int intValue() 以 int 类型返回该 Integer 的值。

static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行解析。
String
char charAt(int index) 返回指定索引处的 char 值。

int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写。

boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束

static String copyValueOf(char[] data) 返回指定数组中表示该字符序列的 String。

byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。

int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引。

String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。

String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。

String trim() 返回字符串的副本,忽略前导空白和尾部空白。

static String valueOf(int i) 返回 int 参数的字符串表示形式。

Integer
//进制转换
//十进制转十六进制 3e8
System.out.println(Integer.toHexString(1000));
//十进制转八进制 11
System.out.println(Integer.toOctalString(9));
//十进制转二进制 11
System.out.println(Integer.toBinaryString(3));
Integer ii1 = new Integer(1234);//堆内存中取
Integer ii2 = 1234;//去方法区中找
int ii3 = 1234; //ii1 拆箱 int
System.out.println(ii1 == ii3);//T
//虽然属性值相同, 但是引用的地址不同, “==” 比较的是引用的地址
System.out.println(ii1==ii2);//F
System.out.println(ii2==ii3);//T 123
//Integer 类中重写了equals方法, 比较的是属性值
System.out.println(ii1.equals(ii2));//T

Character
char c1 = ‘A’;
char c2 = 49;
System.out.println(“c2 = ” + c2);
Character c3 = c1; //Character c4 = new Character(c1);
System.out.println(Character.isDigit(c1));//判断字符是否为数字 F
System.out.println(Character.isLetter(c1));//判断字符是否为字母 T
System.out.println(Character.isLowerCase(c1));//判断是否为小写字母 F
System.out.println(Character.isUpperCase(c1));//判断是否为大写字母 T
System.out.println(Character.toLowerCase(‘C’));//大写转小写 c
System.out.println(Character.toUpperCase(‘a’));//小写转大写 A
String
String s1 = new String();
System.out.println(“–”+s1+”–”);//对象有但是没有值
//IO流
//将数组中所有的内容构建成字符串
byte[] b1 = {97,98,99,100};
String s2 = new String(b1);
System.out.println(“s2 = ” + s2);//abcd
//使用数组中的一部分构建一个字符串
String s3 = new String(b1, 0, 2);//包前不包后
System.out.println(“s3 = ” + s3);//ab
//把字符数组, 转出String
char[] c = {‘h’,’e’,’l’,’l’,’o’};
String s4 =new String(c);
System.out.println(“s4 = ” + s4);
//字符数组, 起始的角标, 截取的数量
String s5 = new String(c,1, 3);//java.lang.StringIndexOutOfBoundsException
System.out.println(“s5 = ” + s5);//ell

String str = “HelloWorld”;

    //charAt()
    char c1 = str.charAt(5);
    System.out.println("c1 =  " + c1);//W

    //长度       str.length()
    // 数组长度: arr.length
    System.out.println("str 的长度 = " + str.length());//10

    //concat  --- "+"
    String s2 = str.concat("么么哒");
    System.out.println("s2 = " + s2);


    //contains  是否包含指定的内容
    boolean b1 = str.contains("ello");
    System.out.println("b1 = " + b1);//true


    //endWith  -- 以XX结尾
    boolean b2 = str.endsWith("d");
    System.out.println("b2 = " + b2);//true

    String fileName = "张三english.txt";
    System.out.println(fileName.endsWith(".mp3"));//F
    System.out.println(fileName.startsWith("张"));//T

    System.out.println("abc".equals("ABc"));//F
    //比较内容, 不考虑大小写  -- 验证码
    System.out.println("abc".equalsIgnoreCase("ABc"));//T

    //String -- byte   字符串转成字节数组
    byte[] byte1 = str.getBytes();
    System.out.println("byte1 = " + Arrays.toString(byte1));



    //字符串转字符数组
    char[] charArray = str.toCharArray();
    System.out.println(Arrays.toString(charArray));//[H, e, l, l, o, W, o, r, l, d]
    String str2 = "abcdccefc";
    //indexOf -- 第一次出现的位置
    System.out.println(str2.indexOf("c"));
    System.out.println(str2.indexOf("c", 5));
    System.out.println(str2.indexOf("cc"));
    System.out.println("c 最后一次出现的位置  = " + str2.lastIndexOf("c"));
    //判断是否为空
    System.out.println(str2.isEmpty());
    String str3 = "abcxedrf";
    //替换
    String str4 = str3.replace("x", "d");
    System.out.println("str3 = " + str3);
    System.out.println("str4 = " + str4);
    String str5 = "abc123abc123abc123";
    String str6 = str5.replace("123", "你开心吗");
    System.out.println(str6);
    String str7 = ",123,,45,67,78,,,,";
    //字符串切割
    String[] strArray = str7.split(",");
    for(String str8:strArray)
    {
        System.out.println("----> " + str8);//后面放多少个逗号都没有用, 只负责前面和中间
    }
    String str9 = "helloWorld";
    String str10 = str9.substring(3);//从开始的位置, 截取到末尾
    System.out.println("str10 = " + str10);
    System.out.println(str9.substring(3, 6));//loW   [3,6) 包前不包后

String s1 = new String(“abc”);
String s2 = “abc”;
String s3 = new String(“abc”);
String s4 = “abc”;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s1.equals(s4));//T
System.out.println(s1 == s2);//F
System.out.println(s1 == s3);//F
System.out.println(s2 == s4);//T

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值