1.String类型的空值与null的却别:
String s1 = "";
String s2 ;
空值是一个java对象,表明这个对象已经存在,其值为“”,使用s1.length返回值为0,不会报错;
null表示为空字符串,表明并未对这个进行赋值,使用s2.length的方式时会报空指针的错误。
2.如何比较String类型是否相等
只要区分==和equals的区别:
==表示的是两个字符串所引用的地址值是否相等;
equals表示的是两个字符串的内容是否相等:
例:String s1 = “hello” String s2 = “hello” String s3 = s1
s1 == s2, s1.equals(s2), s3 == s1 依次返回的结果为false true false
3.String类型的格式化输出:
表现形式有两种:System.out.printf("Hello,%s Next year,you'll be %d",huxiaodong,22);
String.format("Hello,%s Next year you'll be %d",huxiaodong,22);
其中参数所表示的含义为:
转换符 | 类型 |
---|---|
%d | 十进制整数 |
%x | 十六进制整数 |
%o | 八进制整数 |
%f | 定点浮点数 |
%e | 指数浮点数 |
%g | 通用浮点数 |
%a | 十六进制浮点数 |
%s | 字符串 |
%c | 字符 |
%b | 布尔 |
%h | 散列码 |
标志 | 目的 | 举例 |
---|---|---|
+ | 打印正数和负数的符号 | +2222.22 |
空格 | 在正数之前添加空格 | | 2222.22| |
0 | 数字前面补0 | 02222.22 |
- | 左对齐 | |2222.22 | |
( | 将负数括在括号内 | (2222.22) |
, | 添加分组分隔符 | 2,222.22 |
#(对于f格式) | 包含小数点 | 2,222. |
#(对于x或0格式) | 添加前缀0x或0 | 0xcafe |
$ | 给定被格式化的参数索引 | 159 9F |
< | 格式化前面说明的数值 |