A variety of wretched Java written/interview topics __java

Source: Internet
Author: User
Tags finally block stringbuffer

The original question http://blog.csdn.net/smcwwh/article/details/7315041, oneself do some experience

1.

public static void Main (string[] args) {  
        int k = 0;  
        int ret = ++k + k++ + ++k + k;  
        The value of the RET is how many   
        System.err.println (ret);  

1+1+3+3=8

2.

int i1 = ten, i2 = ten;  
        System.err.println ("I1 + i2 =" + I1 + i2);  
        System.err.println ("I1-i2 =" + i1-i2);  
        SYSTEM.ERR.PRINTLN ("I1 * i2 =" + I1 * i2);  
        System.err.println ("I1/i2 =" + I1/i2);  

I1 + i2 = 1010 (String)

Anomaly (good wretched string-in t)
I1 * i2 = 100
I1/i2 = 1

3.

public void MyMethod (String str) {  
        System.err.println ("string");  
    }  
      
    public void MyMethod (object obj) {  
        System.err.println ("Object");  
    }  
      
    public static void Main (string[] args) {  
        Test t = new Test ();  
        T.mymethod (null);  
    }  

String (The principle of child precedence matching)

4.

Date date = new Date ();  
        System.err.println (Date.getmonth () + "" + date.getdate ());  

8 8

GetMonth returned 0-11.

5.

Double val = 11.5;  
        System.err.println (Math.Round (Val));  
        System.err.println (Math.floor (Val));  
        System.err.println (Math.ceil (Val));  

12

11.0

12.0

Math.Round returns the nearest parameter of long

Math.floor returns the largest (nearest positive infinity) Double value that is less than or equal to the argument and is equal to an integer.

Math.ceil returns the smallest (closest negative infinity) Double value that is greater than or equal to the argument and equals an integer.

6. Program output a directory of all directories and file names, between the table of contents with the tab

public static void Main (string[] args) {  
        new Test (). Read ("D:/test", "");  
    }  
      
    public void Read (string path, String tab) {  
        File File = new file (path);  
        file[] Childfiles = File.listfiles ();  
        for (int i = 0; Childfiles!= null && i < childfiles.length; i++) {  
            System.err.println tab + childfiles[i]. GetName ());  
            if (Childfiles[i].isdirectory ()) {  
                read (Childfiles[i].getpath (), tab + "\ t");}}  
      


7. Read 10 integers from the keyboard and then output from large to small

public static void Main (string[] args) {  
        Scanner in = new Scanner (system.in);  
        Note that the array here, not the   
        integer[of int] arr = new INTEGER[10];  
        for (int i = 0; i < i++) {  
            Arr[i] = In.nextint ();  
        }  
        Arrays.sort (arr, new comparator<integer> () {  
            @Override public  
            int compare (integer o1, integer o2) {  
                if (O1 > O2) return-1;  
                if (O1 < O2) return 1;  
                return 0;  
            }  
              
        });  
        System.err.println (arrays.tostring (arr));  
    }  


8.

public static void Main (string[] args) {  
        Base b = new Test ();  
        B.method ();  
          
        Test T = new test ();  
        T.method ();  
    }  
  
    @Override public  
    Void Method () {  
        System.err.println ("test");  
    }  
  
Class Base {public  
    void method () throws Interruptedexception {  
        System.err.println ("Base");  
    }  


Test

Test

9.

public class Test extends Base {public  
  
    static void Main (string[] args) {  
        new Test (). method ();  
  
    public void Method () {  
        System.err.println (Super.getclass (). GetName ());  
        System.err.println (This.getclass (). Getsuperclass (). GetName ());  
    }  
  
Class Base {  
}  

Test

Base

In the test method, call the GetClass (). GetName () method directly, returning the test class name
Since GetClass () is defined as final in the object class, subclasses cannot override the method, so
The GetClass (). GetName () method is invoked in the test method, which is essentially invoking the GetClass () method inherited from the parent class, which is equivalent to invoking the Super.getclass (). GetName () method, so Super.getclass ( ). The GetName () method should also return test.
If you want the name of the parent class, you should use the following code:
GetClass (). Getsuperclass (). GetName ();
————————————————————
The above is the topic and analysis, I also did the test, the result is really test, but I do not understand the following analysis, I think he said ambiguous, did not explain the white substance problem.
To do this, I looked at the API for the GetClass () method of object, which explains: Returns the Run-time class for this object. The returned class object is the object that is locked by the static synchronized method of the class being represented.
Look at this. This.getclass () is the type that returns the Run-time object, but it is not clear why Super.getclass () returned the type of the subclass.
Later on the Internet to check the meaning of super, there is a post that super is not a reference to the superclass, but rather represents the parent class in the subclass of the method or attribute, and give an example
Class B extends a{
public void print () {
System.out.println (Super.getclass ())//Call the GetClass () method in Class A, a is a subclass of object, the GetClass () in A is in object, and the instance of the runtime is Class B, So the output is still class B.
System.out.println (This.getclass ()); Call the GetClass () method in Class B, which inherits from a, a is inherited from object, and the instance of the runtime is Class B, so it outputs Class B


}
I think the explanation behind is more, but I am not sure the explanation is correct, so, sent to the version, please help look at the master, give a reasonable explanation. Because this question of the so-called answer has been reproduced many times, if the explanation is really incorrect, it will blind a lot of people ...

Original link http://blog.sina.com.cn/s/blog_60cad3500102dx11.html

10.

public static void Main (string[] args) {  
        string str1 = new String ("abc");  
        String str2 = new String ("abc");  
        System.err.println (Str1.equals (str2));  
          
        StringBuffer sb1 = new StringBuffer ("abc");  
        StringBuffer SB2 = new StringBuffer ("abc");  
        System.err.println (Sb1.equals (SB2));  
    }  

True

False

The string class provides methods for comparing strings. This class implements the Equals () method of the object parent class, which is used to compare the values of the two strings for equality.

However, the StringBuffer class does not implement the Equals method of the Objcet class, so you cannot use this method to compare the strings of two stringbuffer classes for equality

Details of the connection http://www.west263.com/info/html/chengxusheji/Javajishu/20080403/57254.html

11.

public static void Main (string[] args) {  
        System.err.println (new Test (). METHOD1 ());  
        System.err.println (New Test (). METHOD2 ());  
      
    public int method1 () {  
        int x = 1;  
        try {return  
            x;  
        } finally {  
            ++x  
        }  
      
    } public int method2 () {  
        int x = 1;  
        try {return  
            x;  
        } finally {return  
            ++x  
        }  
    }  

1

2

Finally blocks must be used in conjunction with try or try/catch blocks. In addition, it is not possible to exit a try block without executing its finally block. If a finally block exists, it is always executed. (from that point of view, the statement is correct.) There is a way to exit a try block without executing the finally block. If the code executes a system.exit (0) inside the try; Statement, the application terminates without executing the finally execution. On the other hand, if you dial out the power during the try block execution, finally it is not executed. )

Finally structure makes the code always execute, regardless of whether there are exceptions second try, catch, finally in the fianlly Throw/return highest level
12.

The difference between synchronized and static synchronized

: a:synchronized Static is the scope of a class, synchronized static csync{} prevents multiple threads from simultaneously accessing the synchronized static method in this class. It can work on all object instances of a class.

B:synchronized is the scope of an instance, synchronized Issync () {} prevents multiple threads from simultaneously accessing the Synchronized method in this instance.



2.synchronized method and synchronized Code fast Difference
There is no difference between synchronized methods () {} and synchronized (this) {}, but synchronized methods () {} is easy to read and synchronized (this) {} More precise control of the conflict limits access to the region, and sometimes more efficient performance.


The 3.synchronized keyword is not inherited
This is seen in the http://www.learndiary.com/archives/diaries/2910.htm, I think this is also very noteworthy, the inheritance of the subclass of the override method must display defined as synchronized

Original http://www.cnblogs.com/shipengzhi/articles/2223100.html

13.

public static void Main (string[] args) {  
        Integer i1 = 127;  
        Integer i2 = 127;  
        System.err.println (I1 = = i2);  
          
        I1 = 128;  
        i2 = 128;  
        System.err.println (I1 = = I2);  
    }  

True

False

For integers, the cache is set in the integer class, and the (-128) ~127 is saved, so when you create an integer object less than 128, you don't assign a new object each time, but first you judge the
14.

public static void Main (string[] args) {  
        String str1 = "a";  
        String str2 = "a";  
        String str3 = new String ("a");  
          
        System.err.println (str1 = = str2);  
        System.err.println (str1 = = STR3);  
        STR3 = Str3.intern ();  
        System.err.println (str1 = = STR3);  
    }  

True
False
True

Intern () Returns a normalized representation of the string object.

15.

System.err.println (12-11.9 = 0.1);  

False

0.5 can be accurately represented in a computer, and 0.1 cannot be accurately expressed in binary notation. Same as 0.75, it should be all right.
Deep http://blog.csdn.net/smcwwh/article/details/7080878

16.

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 ());  

0

The final output is sum, the broken name of the

BigInteger c= Sum.add (three);  
		        System.out.println (c);  

So output C is to be evaluated and

17.

set<string> set = new Hashset<string> ();  
        Set.add ("one");  
        Set.add ("two");  
        Set.add ("three");  
        Set.add ("four");  
        Set.add ("five");  
        for (iterator<string> it = Set.iterator (); It.hasnext ();) {  
            System.err.println (It.next ());  
        }  

Disordered









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.