java的一些問題

來源:互聯網
上載者:User

標籤:lis   get   stat   ecif   ble   導致   stringbu   ssi   lang   

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 要加上引號,否則會有小數。

 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\n", 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[[email protected]

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("a\u0022 + \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 < 100; 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 <= END; i++){
  count++;
  System.out.println(i);     -- 死迴圈。  當 i 達到 2147483647 ,再增加會變成負數。
}

 

java的一些問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.