Java face question the first day (a source file multiple classes,& and && differences)

Source: Internet
Author: User
Tags bitwise

One source file multiple classes

Can a Java source file contain more than one class (not an inner class)? What are the restrictions?

A: There can be multiple classes, but there can only be one public class, and the public class name tutorial matches the file name.

The sample code is as follows:

public class Test1 {private Integer id;public void print () {System.out.println ("id=" +id);}} class test11{private Integer ID;} Inner class outer{private int age;class inner{private int age2;}}

Extended knowledge: Inner classes have member inner class, method inner class, anonymous inner class, Static nested Class 4.

From a certain point of view, internal classes make multiple inheritance solutions complete. The interface solves some of the problems, while the inner class effectively implements "multiple inheritance."

the difference between & and &&

Their same point:& and && can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the entire result is true, otherwise, the result is false, as long as there is a side of false.
Their different points:

1, && also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated.
For example, for an if (Str!=null&&!str.equals (")) expression, when STR is NULL, subsequent expressions do not execute, so the nullpointerexception will not appear if you change && to &, the NullPointerException exception is thrown. if (x==33 & ++y>0) y grows, if (x==33 && ++y>0) does not grow.
2, & can also be used as a bitwise operator, when the expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer to get the lowest 4 bit bits of the integer.
For example, the result of 0x31 & 0x0f is 0x01.
Note: This topic first said the similarities between the two, and the special points of && and &, and cite some classic examples to show that they understand thorough, practical experience
The sample code is as follows:

Package Java basic topic;/** * Talk about the difference between & and && */public class Test2 {public static void main (string[] args) {String str = nu ll;//String str = ""; int x = 3;int y = 0;//method1 (str);//Does not report java.lang.NullPointerException (&&)//METHOD2 (str); Will report Java.lang.NullPointerException (&) System.out.println ("=================");//Method3 (x, y);//&& If the front of the non-establishment then will not be executed//METHOD4 (x, y);& words if the front of the non-establishment so the back still want to judge!!! System.out.println ("================="); Method5 ();//bitwise AND operation remove a hexadecimal low four-bit}public static void method1 (String str) {if ( str! = null &&!str.equals ("")) {System.out.println ("str=" + str);} else {System.out.println (&&) str is null, but not java.lang.NullPointerException ");}} public static void Method2 (String str) {if (str! = null &!str.equals ("")) {System.out.println ("str=" + str);} else {System.out.println (&) STR is NULL, will report java.lang.NullPointerException ");//If STR is NULL, then this line of code will not execute throw null pointer exception}} public static void method3 (int x, int y) {if ((x = =) && (++y); 0) {//null operation}SYSTEM.OUT.PRINTLN ("y=" + y),//Y or 0, no self-increment}public static void method4 (int x, int y) {if ((x = =) & (++y) & Gt 0) {//null operation}SYSTEM.OUT.PRINTLN ("y=" + y);//Y becomes 1, since the increase, indicating that although x==33 is not established, but ++y the execution of the!!! }public static void Method5 () {int b = 0x0f;//16 is automatically converted to decimal int c = 0x31;//16 is automatically converted to decimal int d = b & c;//bitwise AND operation (computer will eventually automatically convert Bitwise AND operation for binary) String DD = integer.tohexstring (d);//Convert the bitwise AND operation results to Hex System.out.println ("0x31 & 0x0f=" + dd); The////process is as follows:/ * * 0x for 16 binary, 0f to binary words is 0000 1111,31 to binary Word is 0011 0001, this two number is bitwise and &, 0000 1111 * & 0011 0001 = 0000 0001 get 0000 0001, using 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of the integer */}}





Java face question the first day (a source file multiple classes,& and && differences)

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.