Home > Java > javaTutorial > body text

Summary of problems encountered in java

巴扎黑
Release: 2017-07-24 14:00:55
Original
1392 people have browsed it

1. Determine whether it is an odd number:

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

2. System. out.println(2.0 - 1.1); Output: 0.89999999 99999999 (Double type)

System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.10"))); --To output 0.90, you need to add quotation marks, otherwise there will be decimals.

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

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

System.out.printf ("%.2f\n", 2.0-1.115); --Output: 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); boundary .
System.out.println(24 * 60 * 60 * 1000 ); Output: 5

For long integer calculation, add L: System.out.println(24L * 60 * 60 * 1000 * 1000); --- Output: 86400000 000

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

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

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

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

System.out.println("a" + numbers); Output: 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)<code> and then println()<code>.

System.out.println(numbers); Output: 123

void java.io.PrintStream.println(char[] x) Overloaded method
Prints an array of characters and then terminate the line. This method behaves as though it invokes

print(char[])<code> and then println()<code>.

\u0022 is the unicode encoding of double quotes.

System.out.println("a\u0022 + \u0022b ".length()); Equivalent to: System.out.println("a" + "b ".length()); , output a2. (There is a space after b)

System.out.println(Test.class.getName().replace(".","/")) ; Output: 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(".","/")); Output: ////////          

System. out.println(Test.class.getName().replaceAll("\\.","/")); Output: 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' is treated as an int number.

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); Output a

int j = 0;

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

Output 100 lines 0 (the value of j is always 0):

0

0

……

final int END=Integer.MAX_VALUE; count = 0;

for(int i = START; i <= END; i++){
Count++;
System.out.println(i); -- Infinite loop. When i reaches 2147483647, it will become negative if it increases further.
}


The above is the detailed content of Summary of problems encountered in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template