首页 > Java > java教程 > 正文

java中遇到的问题总结

巴扎黑
发布: 2017-07-24 14:00:55
原创
1552人浏览过

1. 判断是否是奇数:

public static boolean isOdd(int i) { return i %2 != 0 ; }

 

2. System.out.println(2.0 - 1.1); 输出:0.89999999 99999999 (Double型的)

   System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.10")));   --输出 0.90 要加上引号,否则会有小数。

立即学习Java免费学习笔记(深入)”;

 System.out.println(new BigDecimal("2.01").subtract(new BigDecimal("1.65")));   -- 输出 0.36

System.out.println(new BigDecimal(2.0).subtract(new BigDecimal(1.1)));  -- 输出    0.899 99999999 99999111 82158029 98747676 61094665 52734375

System.out.printf("%.2f ", 2.0-1.115);   --输出:0.89

 

 

final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(24 * 60 * 60 * 1000 * 1000);                              --- 输出: 500654080  , 当做了int型,导致越界。
System.out.println(24 * 60 * 60 * 1000 );                                        --- 输出: 86400000 
System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);         --- 输出 :5

 

对于长整型的计算,要加上L:  System.out.println(24L * 60 * 60 * 1000 * 1000);  --- 输出: 86400000 000

 

System.out.println("H" + "a"); 输出: Ha

System.out.println("H" + 'a');     Ha

System.out.println('H' + 'a');    169

 

char[] numbers = {'1','2','3'};

System.out.println("a" + numbers);     输出:a[C@c3c749

void java.io.PrintStream.println(String x)

Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

 

System.out.println(numbers);  输出:123

void java.io.PrintStream.println(char[] x)     重载的方法

Prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

 

 

u0022 是双引号的unicode编码。

System.out.println("au0022 + u0022b ".length());   相当于: System.out.println("a" + "b ".length());  , 输出 a2  。(b后面有一个空格)

 

 

System.out.println(Test.class.getName().replace(".","/"));   输出: com/Test 

String java.lang.String.replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

 

System.out.println(Test.class.getName().replaceAll(".","/"));   输出:////////              

System.out.println(Test.class.getName().replaceAll("\.","/"));   输出:com/Test

String java.lang.String.replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl)

 

StringBuffer word = null;
word = new StringBuffer('P');

‘P’ 当做了int数。

java.lang.StringBuffer.StringBuffer(int capacity)

Constructs a string buffer with no characters in it and the specified initial capacity.

  • Parameters:

  • capacity the initial capacity.


System.out.println(word);                           输出换行
System.out.println(word.append("a"));      输出 a

 

 

int j = 0;
for(int i = 0; i j = j++;
System.out.println(j);
}

输出100 行 0 (j的值总是 0):

0

0

……

 

 

final int END=Integer.MAX_VALUE;                    //2147483647
final int START = END - 100;
int count = 0;
for(int i = START; i   count++;
  System.out.println(i);     -- 死循环。  当 i 达到 2147483647 ,再增加会变成负数。
}

 

以上就是java中遇到的问题总结的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号