Java Foundation into pit set

Source: Internet
Author: User

Pit 1
public class Testdemo {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (Test ("0") + "," +test (NULL) + "," +test ("66"));
}
public static int test (String str) {
try {
Return Str.charat (0)-' 0 ';
} catch (NullPointerException e) {
return 2;
} catch (Indexoutofboundsexception e) {
return 3;
} catch (Exception e) {
return 4;
}finally{
return 5;
}
}
}

Print three 5,finally{} The contents of the code block are bound to run and will produce an overlay.

Str.charat (0)-' 0 '; The line code returns the character of STR subscript 0 compared to ' 0 ', and if so, returns 0, otherwise-1;

such as ' 123 '. CharAt (0)-' 1 ' is 0; ' 123 '. CharAt (0)-' 1 ' is-1;

Pit 2

public class Test {

public static void Main (string[] args) {
int sum=0;
for (int i=0;i<10;i++) {
sum=sum++;
}
SYSTEM.OUT.PRINTLN (sum);
}
}

The result of printing is 0, because it is sum++, not ++sum;

Java Foundation into pit set

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.