Java basic knowledge traps (8)

Source: Internet
Author: User

Java basic knowledge traps (8)
For the difference between "&" and "&", we all know that "&" is a bitwise operator and "&" is a logical operator. refer to 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: & condition 1 shows that when the second condition is judged, & if a short circuit is performed, the B = 3 operation is not performed. If you replace & with &, see 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) ;}check the output: & condition 1. This & Symbol does not generate a short circuit. The following judgment is also made. Polymorphism is very familiar to programmers. If you are familiar with polymorphism, I 'd like to say that the Internet is much better. First, polymorphism should be a runtime behavior. This is especially important! Many places on the Internet have said that rewriting and overloading are polymorphism. However, in some places and blogs, it is suggested that reload should not be considered as a polymorphism !! Class Parent {public void doing () {System. out. println ("Parent --> do") ;}} class Child extends Parent {@ Override public void doing () {System. out. println ("Child --> do");} public void run () {System. out. println ("Child --> run") ;}} check the call code: public static void main (String [] args) throws Exception {Parent p = new Child (); p. doing (); p. run ();} p. doing (); this can be called accurately, but p. run () is a compilation error because its method is not defined in the Parent. If you need to call It must be defined as Child. 1 Child p = new Child ();

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.