java 数字与字符串

1. 封装类、装箱、拆箱

封装类:基本类型对应的类类型

Number类的子类有Byte, Short, Integer, Long, Float, Double

基本类型->封装类: int i = 1; Integer intVal = new Integer(i), 自动装箱: Integer intVal = i;

封装类型—>基本类型:  int i = intVal.intValue(); 自动拆箱: int i = intVal;

最大(小)值: Integer.MAX_VALUE, Integer.MIN_VALUE.

2. 数字与字符串

数字转字符串: int i = 1; String s = String.valueOf(i); 或 Integer it = i; String s = it.toString();

字符串转数字: String s = "10"; int i = Integer.parseInt(s);

3. 数学运算:

java.lang.Math提供常用数学运算方法(静态)

四舍五入: Math.round()

随机浮点数[0,1): Math.random()

开方: Math.sqrt()

幂: Math.pow(a, b);

pi: Math.PI

自然常数e: Math.E

4. 格式化输出

%s, %d, %n: String strFormat = "%s ... %d ... %n"; System.out.printf(strFormat, a, b, c);或 System.out.format(strFormat, a, b, c);

%,md, %,.nf, %-md。(","为千分位分隔符)

4. char

char -> Character

char c = 'a'; Character ch = c; c = ch;

Character.isLetter(), isDigiter(), isWhitespace(), isUpperCase(), isLowerCase(), toUpperCase(), toLowerCase(); toString();

char->String; String s = Character.toString();

不能直接把char转成String.

5. 字符串

String s = "";

String s = new String("");

char[] cArr = new char[] {};

String s = new String(cArr);

String s = s1 + s2;

String被final修饰,不能继承。

String对象内容不能改变。

String.format(strFormat, a, b, c)

字符串长度: s.length()

方法:

charAt(), toCharArray(), subString(), split(), trim(), toLowerCase(), toUpperCase(), indexOf(), lastIndexOf(), contains(), replaceAll(), replaceFirst();

是否是同一对象: ==

String s1 = "abc"; String s2 = new String(s1); s1 == s2->false;

String s1 = "abc"; String s2 = "abc"; s1 == s2->true;

s1.equals(s2); s1.equalsIgnoreCase(s2); 内容是否相同

s1.startsWith(), endsWith();

 

6. StringBuffer

可变长字符串

append(), delete();, insert(), reverse();

Stirng s = "abc"; StringBuffer sb = new StringBuffer(s); sb.append(" def");

sb.delete();

length:长度

capacity: 分配的空间。当第一次分配空间不够时,另分配空间,将原数据拷贝到新空间。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值