Java test trap

Source: Internet
Author: User

Today, I occasionally made a Java question and was directly despised.

PostCode

 
Package test; public class demo {public static void main (string [] ARGs) {int I = 10; Switch (I) {default: system. out. println ("default"); Case 0: system. out. println ("0"); break; Case 1: system. out. println ("1"); Case 2: system. out. println ("2 ");}}}

 
Default0

The result is this, which gets the switch operation mechanism. No matter how the switch is written, the number in the case is first determined. Therefore, wherever the default is written, the case statement is first executed.

The focus is on the following. If this value exists in case, execute the statements in this case directly. If this value is not available, the system will find default. Run the default command until the break is executed. So unfortunately, I didn't learn the C language well... It's a tragedy. Let's make up for it during the winter vacation.

Another trap is &, | and &, |. The former has a short circuit function, while the latter does not. I remember it, so I failed the exam ..

Another trap is the Java string array. Although the string is a class, this class is special and can be used in this way.

String a [] = {"", ""}; // The correct string AA [] = new string [5]; // It is also correct AA [0] = "123"; system. out. println (AA [0]);

However, for a custom class, it is very different from the string class.

 
Package test; import B. student; public class demo {public static void main (string [] ARGs) {student s [] = new student [5]; s [0]. setname ("AAA"); system. out. println (s [0]. getname ());}}

In this way, a null pointer exception occurs.

 
Exception in thread "Main" Java. Lang. nullpointerexceptionat test. Demo. Main (Demo. Java: 8)

I personally think C and Java are really profound and profound, and there are too many things. It is worth studying.

There is also a small trap

The length of the array is. length.

The string length is. Length ()

The list size is. Size ()

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.