Java Basics Traps (eight)

Source: Internet
Author: User

This article was posted on my blog

This time I'm talking about the difference between & and &&, we all know that & is a bitwise operator, and && is a logical operator, see the following code:

    public static void Main (string[] args) throws Exception {          int a = 1;        int b = 2;        int c = A & B;        if (a >= 1 && b >= 1) {            System.out.println ("&& condition 1");        }        if (a >= 2 && (b = 3) >= 3) {            System.out.println ("&& condition 2");        }        System.out.println (b);        System.out.println (c);            }

Then the output:

&& conditions 120

As can be seen, the second condition is judged when && a short circuit does not perform B = 3; What if we change && into &, how to look at the code:

    public static void Main (string[] args) throws Exception {        int a = 1;        int b = 2;        int c = A & B;        if (a >= 1 && b >= 1) {            System.out.println ("&& condition 1");        }        if (a >= 2 && (b = 3) >= 3) {            System.out.println ("&& condition 2");        }        System.out.println (b);        if (a >= 2 & (b = 3) >= 3) {            System.out.println ("&& condition 3");        }        System.out.println (b);        System.out.println (c);            }

Look at the output:

&& Conditions 1230

This & symbol does not produce a short-circuit function and will be judged below.

Polymorphic This is very familiar to programmers, familiar with me or to say that the internet is much better, first polymorphic should be a run-time behavior, this is particularly important! Many places on the web say that rewriting and overloading are polymorphic behaviors. But there are some places as well as blogs that explain that overloading is not interpreted as polymorphic!!

Class parent{public    void doing () {        System.out.println ("Parent-to-do");}    } Class Child extends the Parent {    @Override public    Void doing () {        System.out.println ("child-to-do");    }    public void Run () {        System.out.println ("child-to-run");}    }

Look at the calling code:

    public static void Main (string[] args) throws Exception {        Parent p = new Child ();        P.doing ();        P.run ();    }

P.doing (); This can be called exactly, however P.run () is a compilation error because its method is not defined in the parent class, and needs to be defined as child if it needs to be called.

Child p = new Child ();

to understand more, you can see here:

http://blog.csdn.net/cyq1028/article/details/6879088

http://www.cnblogs.com/mengdd/archive/2012/12/25/2832288.html


come here first this time. Keep a record of every bit of drip!


Java Basics Traps (eight)

Related Article

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.