Object
1.概述
是类层次结构的根类。每个类都使用 Object
作为超类。
2.创建对象
Object() —无参构造
3.常用方法
|
|
|
|
|
|
String
1.概述
所有被一对双引号引起来的数据都是String类型
String
类代表字符串。字符串是常量;它们的值在创建之后不能更改。
String本质上是维护一个char[]来存储多个字符数据
2.源码
public final class String
private final char value[];常量,值不能被修改
3.创建对象
`**String**(char[] value)`
4.常用方法
String s=“helloworld”; System.out.println();
|
|
System.out.println(s.charAt(2)); —‘l’ | |
|
|
System.out.println(s.concat(“123”));—“hello123” | |
|
|
System.out.println(s.contains(“xyz”));—false | |
|
|
System.out.println(s.endWith(“o”));—true | |
|
|
System.out.println(s.equals(“llo”));//false | |
|
|
System.out.println(s.hashCode());//-1524582912 | |
|
|
System.out.println(s.indexOf(“wor”));//5 | |
|
|
System.out.println(s.isEmpty());//false | |
|
|
System.out.println(s.lastIndexOf(“o”));//6 | |
|
|
System.out.println(s.length());//10 | |
|
|
System.out.println(s.replace(‘l’, ‘c’));//heccoworcd | |
|
|
System.out.println(s.startsWith(“e”));//false | |
|
|
System.out.println(s.substring(1));//elloworld | |
|
|
System.out.println(s.substring(1, 3));//el | |
|
|
System.out.println(s.toLowerCase());//helloworld | |
|
|
System.out.println(s.toUpperCase());//HELLOWORLD | |
|
|
System.out.println(s.trim());//helloworld //去除前面和后面的多余空格 | |
|
|
System.out.println(String.valueOf(‘l’));//e 把各种类型转成String类型 | |
|
|
byte[] a = s.getBytes(); System.out.println(Arrays.toString(a));//[104, 101, 108, 108, 111, 119, 111, 114, 108, 100] | |
|
|
char[] b = s.toCharArray(); System.out.println(Arrays.toString(b));//[h, e, l, l, o, w, o, r, l, d] | |
|
|
String[] c = s.split(“o”); System.out.println(Arrays.toString©);//[hell, w, rld] |
5.练习题
接受用户输入的字符串,并打印每个字符
String sc=new String(System.in).nextLine();
for(int i=0;i<sc.length();i++){
char x = sc.charAt(i);
System.out.println(x);
}