Some problems with Java

Source: Internet
Author: User

1. Determine if it is odd:

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")); --The output 0.90 should be quoted, 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); ---output: 500654080, as int, resulting in an out-of-bounds.
SYSTEM.OUT.PRINTLN (24 * 60 * 60 * 1000); ---output: 86400000
System.out.println (Micros_per_day/millis_per_day); ---output: 5

For long integer calculations, 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[[email protected]

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

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

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 and then print(char[]) println() .

\U0022 is a Unicode encoding of double quotation marks.

System.out.println ("a\u0022 + \u0022b". Length ());  Equivalent to: System.out.println ("a" + "B". Length ()); , Output A2. (There is a space behind 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, matches the literal target sequence with the specified literal replacement seq Uence. The replacement proceeds from the beginning of the string to the end, for example, replacing ' AA ' with ' B ' in the string " AAA "would 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, 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 ' as the 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 line break
System.out.println (Word.append ("a")); Output A

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

Output 100 line 0 (the value of J is always 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);  --Cycle of death. When I reaches 2147483647, then the increase will become negative.
}

Some problems with Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.