java tips

1. if(find = true) // this  sentence is always true, if(find = = true) is the right way

2. ArrayList uses size(), not length()
    Array uses length(field, not method length())
    String uses length()
    String[] is an array, so use length(field)

3. pay attention to the difference between return i++, return i--

4. when you print a return value
System.out.format("%n");

5. int to char, int to String(转载)
(1) int to char
int a = 1;
char b = (char) a;
System.out.println(b);

will print out the char with ascii value 1 (start-of-heading char, which isn't printable).

int a = '1';
char b = (char) a;
System.out.println(b);

will print out the char with ascii value 49 (one corresponding to '1')

(2) int to String

What's the real difference?

  1. Integer.toString(i)
  2. new Integer(i).toString()

Integer.toString calls the static method in the class Integer. It does not need an instance of Integer.

If you call new Integer(i) you create an instance of type Integer, which is a full Java object encapsulating the value of your int. Then you call the toString method on it to ask it to return a string representation of itself.

If all you want is print an int, you'd use the first one because it's lighter, faster and doesn't use extra memory (aside from the returned string).
If you want an object representing an integer value to put it inside a collection for example, you'd use the second one, since it gives you a full-fledged object to do all sort of things that you cannot do with a bare int

(3) char to int
Integer.parseInt(Character.toString(char c))




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值