Extraordinary Program Java programmer details------> worthy of your thinking

Source: Internet
Author: User
Tags logical operators

Everyone should not be on the machine first to see if you can get the correct results, please write the best you do the relevant knowledge points, so that everyone can learn.
Interested friends can be a number. Title number, and then give their own ideas. Of course I think someone has done these questions.

I hope you can give the relevant ideas, related test sites, I have sorted out an answer, then posted out.

First article

1. If you ask everyone "&", "|", "^" What are these three operators. The "bitwise operator" will be answered on the basis, but only 50 points. Most Java programmers probably don't know that it can also act as other operators-Boolean logical operators (if the data types on either side are Boolean types).

The three operators in Boolean logical operators act as "Boolean Logic and", "Boolean Logic or" and "Boolean logical XOR" roles. The first two copy them again and put them together as logical operators. Boolean Logic and (&) and Boolean logic or (|) operators work in the same way as logic (&&) and logic or (| | ) Work the same way, the Boolean logical operators have a higher precedence than the logical operators, and there is a slight difference between them, and you will see what the following program will output.

The last Boolean logical XOR or (^) is used less, and the relational operator is not equal to (!=), and this is not to be delved into.

Boolean logical operators also differ from logical operators in that Boolean logical operators can be combined with assignment operators (=) into Boolean logical assignment operators (&=, |=, ^=), and logical operators do not have such a function, it may have two of its own, and then combine the words "=" changed three, hehe, Too much:)

Java Code

Java Code
public class Test1 {public static void main (string[] args) {int m = 5, n = 5; if ((M!= 5) && (n + + = 5)) { } System.out.println ("A." + N); m = n = 5; if ((M!= 5) & (n + + = 6)) {} System.out.println ("B." + N); m = n = 5; if ((m = = 5) | | (n + + = 5)) {} System.out.println ("C." + N); m = n = 5; if ((m = = 5) | (n + + = 6)) {} System.out.println ("D." + N);}}

Output:




2. The puzzle of automatic boxing

In JDK1.5, the language features of the automatic disassembly box are added, which can be converted and calculated between the basic type and the packing type. As we all know, the = = operator in Java is the same as comparing the references between two objects. There are also strange things between automatic unboxing and the "= =" operator, and see what the following program will output.

Java Code
Java Code
public class Test2 {public static void main (string[] args) {int k = 100; Integer int1 = k; Integer int2 = k; System.out.println ("A." + (Int1 = = Int2)); K = 200; Integer int3 = k; Integer int4 = k; System.out.println ("B." + (Int3 = = int4)); char c = ' A '; Character char1 = c; Character char2 = c; System.out.println ("C." + (Char1 = = char2)); c = ' state '; Character char3 = c; Character CHAR4 = c; System.out.println ("D." + (Char3 = = CHAR4));}
Output:



3. Strange \u and 0x

\u is the prefix of Unicode characters, and 0x is the prefix of hexadecimal values, can be universal. Can the following program be able to divide hello and world! into two lines of output?

Java Code
Java Code
public class Test3 {public static void main (string[] args) {/* hint \u000a represents line break (LF), and what 0x000a represents. */char C = 0x000a; System.out.println ("Hello" + C + "world!");}
Output:



4. It's not going to happen.

Let's have a simple fill in the blanks and fill in the blanks with the appropriate number so the following program can output "Hello world!".

Java Code:
Java Code
public class Test4 {public static void main (string[] args) {int num = _____________; if (num < 0 && num = = Ma Th.abs (num)) {System.out.println ("Hello world!");} }

Fill in:




5, ambiguous method overload

Invoke has two overloaded methods, one is object as parameter, and the other is int[] as an argument to see what the following program will output.

Java Code:

Java Code
public class Test5 {public static void main (string[] args) {invoke (null);} private static void Invoke (Object obj) {S Ystem.out.println ("Object");} private static void Invoke (int [] nums) {System.out.println ("Arrays");}
Output:




Calculation errors caused by 6,number

Number is an abstract parent class for many numeric types, and it can cause a calculation error. See what the following program will output.

Java Code:
Java Code
public class Test6 {public static void main (string[] args) {Number num = 12345 + 5432l; System.out.println (Num.intvalue ()); } }

Output:




Second article

The next few programs will output anything. Don't get on the machine first.
1. Output:
Java Code:
Java Code
public class Test3 {public static void main (string[] args) {parrent p = new Parrent (); Parrent C = new Child (); System.out.println (P.getname ()); System.out.println (C.getname ()); } class Parrent {public static String GetName () {return "Parrent";}} Class Child extends Parrent {public static String GetName () {return ' child ';}}

Output:




2. Output:
Java Code:
Java Code
public class Test {public static void main (string[] args) {for (int i = 0; I <= i + +) integer k = new integer (i);     System.out.println ("Java puzzlers"); }   }


Output:




3
Please fill in the Declaration of all I (Requirements: I do not allow float, double, float, and double types) so that it can output "Hello world".
Java Code:
Java Code
public class Test {public static void main (string[] args) {fill here; if (i!= i + 0) {System.out.println ("Hello wo Rld ");}}}

Fill in:



4. What is the output of the following program?

Java Code:
Java Code
Import Java.math.BigInteger; public class Test {public static void main (string[] args) {BigInteger one = new BigInteger ("1"); BigInteger two = new BigInteger ("2"); BigInteger three = new BigInteger ("3"); BigInteger sum = new BigInteger ("0"); Sum.add (one); Sum.add (two); Sum.add (three);   System.out.println (Sum.tostring ()); } }

Output:



5. Please fill in the blanks in the following procedure (see procedure)

Ignore import statement
Java Code:
Java Code
public class Test {public STA
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.