Basic Java syntax (2)

Source: Internet
Author: User
1. Can the switch statement be applied to byte, long, or string?
In switch (expr1), expr1 can only be oneInteger expressionOrEnumerated constants. Integer expressionIt can be the basic int type or integer packaging type. Because, byte, short, and char can be implicitly converted to int, these types and the packaging types of these types are also possible. Obviously, both the long and string types do not comply with the switch language regulations. And cannot be implicitly converted to int.
Type. Therefore, they cannot be applied to swtich statements.
2. Short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?
For short S1 = 1; S1 = S1 + 1; because the S1 + 1 operation will automatically increase the expression type, the result is int type, and then assigned to short type S1, the compiler will report errors that require forced conversion.
For short S1 = 1; S1 + = 1; Because ++ = is an operator specified by the Java language, the Java compiler will perform special processing on it, so it can be compiled correctly.
3. Can a Chinese character be stored in a char variable? Why?

Char variables are used to store Unicode characters. The Unicode Character Set contains Chinese characters.Type variables can certainly store Chinese characters. However, if a special Chinese character is not included in the Unicode Character SetSo, this char variable cannot store this special Chinese character. Note: unicode encoding occupies two bytes,So, char
The Type Variable also occupies two bytes.

4. Can I call non-static methods from inside a static method?

NO. Because the non-static method must be associated with an object, you must create an object before you can call a method. You can call a static method directly without creating an object. That is, when a static method is called, no instance object is created, if you call a non-static method from a static method, which object is the non-static
method associated? This logic cannot be established. Therefore, a static method
internally calls non-static methods.

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.