Java pen Questions (1)

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/fmh2011/article/details/19690999

Java Foundation Pen test for an internet company

Many of the company's questions come from the Java interview

Can a Chinese character be stored in a 1.char variable?

Char-type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character. Supplemental Note: Unicode encoding consumes two bytes, so variables of type char are also two bytes. Note: The latter part of the answer, although not in the positive answer, but, in order to show their knowledge and performance of their own understanding of the depth of the problem, can answer some relevant knowledge, do sparing, liker. What is the difference between   2, "= =" and the Equals method? (Make one thing clear, and then another, so that the difference naturally comes out, and it's hard to say for sure) the = = operator is specifically used to compare the values of two variables for equality, that is, the value stored in the memory used to compare variables is the same, To compare two basic types of data or two reference variables for equality, use the = = operator only. If the data that a variable points to is an object type, then this time involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as objet obj = new Object (); the variable obj is a memory, and new object () is another memory, At this point, the value stored in the memory of the variable obj corresponds to the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator. The Equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code: string A=new string ("foo"); String B=new string ("foo"), two new statement creates two objects, and then with a, B, each of the two variables points to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and a are not the same, so the expression a== b returns False, and the contents of the two objects are the same, so the expression a.equals (b) returns True. In real-world development, we often have to compare whether the string content passed in is, for example, string input = ...; Input.equals ("Quit"), many people do not pay attention to use = = to compare, this is wrong, casually from the Internet to find a few project real-life teaching video to see, inThere is a large number of such errors on the face. Remember that the comparison of strings is basically using the Equals method. If a class does not define its own Equals method, it inherits the Equals method of the object class, and the implementation code for the Equals method of the object class is as follows: Boolean equals (Object o) {return this==o;} This means that if a class does not define its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method, and write your own code to determine at what time that the contents of the two objects are the same.  3.anonymous Inner Class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)? You can inherit other classes or implement other interfaces. Not only can, but must! 4.string and StringBuffer differences the Java platform provides two classes: string and StringBuffer, which can store and manipulate strings, which are character data that contain multiple characters. This string class provides a string of values that cannot be changed. The string provided by this StringBuffer class is modified. You can use StringBuffer when you know that character data is going to change. Typically, you can use Stringbuffers to dynamically construct character data. In addition, String implements the Equals method, NewString ("abc"). The result of equals (NewString ("abc") is true, and StringBuffer does not implement the Equals method, so the new StringBuffer ("abc"). Equals (Newstringbuffer ("abc") results in false. Then we will give a concrete example to illustrate that we have to put together all the numbers from 1 to 100 to form a string. StringBuffer SBF = new StringBuffer (); for (int i=0;i<100;i++) {sbf.append (i);} The code above is highly efficient because only one stringb is createdThe Uffer object, and the following code is inefficient because 101 objects were created. String str = new string (); for (int i=0;i<100;i++) {str = str + i;} When talking about the difference between the two, the number of cycles should be made into 10000, and then use Endtime-begintime to compare the time difference between the two implementation, and finally to talk about the difference between StringBuilder and StringBuffer. String overrides the Equals method and the Hashcode method, and StringBuffer does not overwrite the Equals method and the Hashcode method, so the problem occurs when the StringBuffer object is stored in the Java collection class. There are several ways to implement a thread in  5.java? What keyword modifies the synchronization method? Why is the Stop () and suspend () methods not recommended? Java5 before, like the next two: the first: New Thread () {}.start (); This means that the Run method that calls the thread subclass object is called, and new Thread () {} represents an instance object of an anonymous subclass of thread, the subclass plus the run The code after the method is as follows: New Thread () {public void run () {}}.start (); second: New Thread (new Runnable () {}). Start (); This means that the run method of the Runnable object accepted by the Thread object is called, and new Runnable () {} represents an instance object of an Runnable anonymous subclass, and the code for the Runnable subclass plus the Run method is as follows: New Thread (new Runnable () {public voidrun () {}}). Start (); Starting with Java5, there are several thread pools that create Multithreading: executorservice pool = Executors.newfixedthreadpool (3) for (int. i=0;i<10;i++) {Pool.execute (newrunable () {public void Run () {}}); Executors.newcachedthreadpool (). Execute (new runable () {publicvoid run () {}}); Executors.newsinglethreadexEcutor (). Execute (new runable () {publicvoid run () {}}), there are two implementations, using the form new thread () and new Thread (runnable), the first call to thread directly The Run method, so we tend to use the Thread subclass, which is new Subthread (). The second call to Runnable's Run method. There are two implementation methods, namely inheriting the Thread class and implementing the Runnable interface with the Synchronized keyword modifier synchronization method against using Stop (), because it is unsafe. It unlocks all locks acquired by the thread, and if the object is in an incoherent state, then other threads can examine and modify them in that state. The result is hard to check out the real problem. The suspend () method is prone to deadlocks. When calling suspend (), the target line routines stops, but still holds the lock that was acquired before. At this point, no other thread can access the locked resource unless the thread that is "suspended" resumes running. For any thread, if they want to restore the target thread while trying to use any of the locked resources, it will cause a deadlock. So instead of using suspend (), you should place a flag in your own thread class that indicates whether the thread should be active or suspended. If the flag indicates that the thread should hang, use wait () to enter the wait state. If the flag indicates that the thread should be resumed, restart the thread with a notify ().  6. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately? An example is described. If the data will be shared between threads. For example, the data being written may be read by another thread later, or the data being read may have been written by another thread, then the data is shared and must be accessed synchronously. When an application calls a method that takes a long time to execute on an object and does not want the program to wait for the method to be returned, it should use asynchronous programming, which is often more efficient in many cases with asynchronous approaches.  7. Are there several ways to implement multithreading? There are two ways to implement multithreading, namely, inheriting the Thread class and implementing Runnable interface synchronization in two ways, namely synchronized,wait and notifywait (): Causes a thread to wait and releases the lock on the object it holds. Sleep (): Makes a running thread sleep, is a static method that calls this method to catch the interruptedexception exception. Notify (): Wake up aIn a waiting state, note that when this method is called, it does not actually wake up a thread waiting for a state, but is determined by the JVM to wake up which thread, and not by priority. Allnotity (): Wakes all the threads that are in the waiting state, noting that they do not give all the wake-up threads an object lock, but instead let them compete. The elements in the  8.set cannot be duplicated, so what is the way to distinguish between repetition or not? Is it the same as or equals ()? What's the difference? The elements in the Set cannot be duplicated, and the repetition of the elements is judged using the Equals () method. The Equals () and = = methods Determine whether the reference value points to the same object Equals () is overwritten in the class so that when the contents and types of the two detached objects match, the truth is returned.

Java pen question (1)

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.