Java exercises Excerpt

Source: Internet
Author: User

2018-01-21 23:23:08

1, finally and return at the same time the situation

    • Finally, a return statement invalidates the return statement in the Try...catch
    public static void Main (string[] args) {        System.out.print (getnumber (0));        System.out.print (GetNumber (1));        System.out.print (GetNumber (2));        System.out.print (GetNumber (4));    }    public static int getnumber (int num) {        try {            int res = 2/num;            return res;        } catch (Exception e) {            return 0;        } finally {            if (num = = 0) {                return-1;            }            if (num = = 1) {                return 1;}}    } Output is:-1110
    • Finally, the modification of the return statement in Try...catch is not an effect.
    public static void Main (string[] args) {        System.out.print (GetNumber (2));    }    public static int getnumber (int num) {        try {            return num;        } finally {            num *= 2;        }    } Output is: 2

2. Initialize static variable--execute static code block--Execute Main method

    static int i = 6;    static {        i + = 9;    }    public static void Main (string[] args) {        System.out.print (i);    }    static {        I/= 3;    } Output is: 5

3. Char,byte,short type conversion problem

Byte

    • The byte data type is a 8-bit, signed, integer in binary complement notation;
    • The minimum value is -128 ( -2^7);
    • The maximum value is 127 (2^7-1);
    • The default value is 0;
    • The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable occupies only one-fourth of the int type;
    • Example: Byte a = 100,byte B =-50.

Short

    • The short data type is a 16-bit, signed integer with a binary complement representation
    • The minimum value is -32768 ( -2^15);
    • The maximum value is 32767 (2^15-1);
    • The short data type can also save space as Byte. A short variable is one-second of the space occupied by the int variable;
    • The default value is 0;
    • Example: Short s = 1000,short r =-20000.

Char

    • The char type is a single 16-bit Unicode character;
    • The minimum value is \u0000 (that is, 0);
    • The maximum value is \uffff (that is, 65,535);
    • Char data type can store any character;
    • Example: char letter = ' A '.

Byte,short and char are automatically converted to int for operation, which results in an error.

BYTE A1 = 2, a2 = 4, A3;short s = 16;a2 = S;  Error, because s will default to int, so strong turn a3 = A1 * A2;  A1 * A2 will be converted to int by default, so a strong turn is required but the + = is not a type conversion problem: short S;s = s + 1;//error, will be converted to int, need strong turn to shortshort s;s + = 1; That's right

Java exercises Excerpt

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.