[Java] fundamental part of interview preparation

Java Fundamentals

Caused his interview preparation questions, add your own and integrate other people sharing!

1. a ".java" source files may include a plurality of whether the class (not an internal class)? What are the limits?

There can be multiple classes, but only one public class, and the public class name must be consistent with the file name.

2.Java there is no goto?

java reserved word, there is no use in java.

3. Talk about the difference between & and &&

& && is the logical operator and, when the result of the expression operators on both sides are true, the result is only true, otherwise, as long as one of them is false, the result is false.
&& short-circuit function, i.e., if the first expression is false, the second expression no longer.
No & shorting function, i.e., if the first expression is false, the second expression is still counted.

4. How to jump out of the current multiple nested loops in JAVA?

Definition of a label on the outer loop, then the loop body break numerals, out of the can.

ok:
for(int i=0;i<10;i++) {
  for(int j=0;j<10;j++) {
    System.out.println(“i=+ i +,j=+ j);
    if(j == 5) break ok;
  }
}

5. In a switch statement, an expression of the type which can be?

In the type of the expression in the java Switch
the JDK 1.6: byte, Short, char, int
the JDK 1.7: byte, Short, char, int, String

6.short s1 = 1; s1 = s1 + 1; there is nothing wrong with short s1 = 1;? S1 + = 1; there anything wrong?

For short s1 = 1; s1 = s1 + 1; since s1 + automatically elevated expression of type 1 operation, so the result is an int, s1 then assigned to the short type, the compiler would report an error type require cast .
For short s1 = 1; s1 + = 1; a = + Since a predetermined java language operators, it will java compiler special treatment, it is possible to correct translation.

7.char type variable can store a Chinese characters? Why?

char type variable is used to store the Unicode characters, Unicode coded character set contains characters, so, of course, char type variable characters can be stored in it. However, if a special unicode characters not included in the coded character set, then the char type variable can not store the special characters.

8.2 multiplied by calculating the most efficient method of 8 equal to a few?

2 << 3, equivalent to a multiplication of the n-th power of 2.

9. Do not use the keyword final modification of a variable, a reference can not be changed, an object or a reference can not be changed?

When modifying a variable with the final keyword, refer to reference variables can not be changed, the content object reference variable points in the still can be changed. For example, the following statement:
Final the StringBuffer the StringBuffer new new = A ( "the immutable");
execute statement reports compile-time error:
A = the StringBuffer new new ( "");
however, the following statement may be performed by the compiler:
a.append ( "broken!");

10. The difference between the "==" and "the equals ()" in

"==": Analyzing the two string objects in memory first address are equal.
"Equals ()": Analyzing the two values are equal string object.

11. The difference between static variables and instance variables?

Differences in syntax definition: former static variable to add the static keyword, but not before adding instance variables.
Static variables using the class to call, to call an instance variable by creating an instance of an object.

12. Can I issue a call to a non-static method from within a static method?

No, you must create object to call.

13. The difference with the int Integer

Is a wrapper class int Integer, Integer the default is null, a default value is 0 int.

14. How many Math.round (11.5) equal? Math.round (-11.5) equals how much?

Results Math.round (11.5) is 12, the result Math.round (-11.5) -11.

15. Please tell scope public private, protected, and when the difference is not written,

作用域    	当前类	 同一package	 子孙类	   其他package 
public		  √          √             √       	√ 
protected	  √          √         	   √      	× 
friendly	  √          √             ×      	× 
private		  √          ×             ×      	× 

16. The difference Overload and Override

Overloaded:
the same class, the same method name, a list of parameters (the number of parameters or parameter types) different from, and independent of the method return value and the modifier.
Override:
same method name, the same parameter list, return the same value type (or subclass thereof), modifier can not access strict parent.

17. whether Constructor constructor can be override?

Constructor constructor can not be inherited, and therefore can not rewrite Override, but can be overloaded Overload.

18. The difference between interfaces and abstract classes?

(1) An abstract class can have a constructor, interface can not have a constructor.
(2) abstract class member variables can have common, there is no common member variables interface
(3) An abstract class may contain static methods, static methods can not contain an interface
(4) A class can implement multiple interfaces, but only inherit an abstract class.
(5) multiple interfaces can be implemented only by a single abstract class inherited
(6) if the abstract class implements an interface, the interface can be mapped to the abstract class methods as abstract methods without necessarily achieving, in an abstract subclass the method implemented in the interface

19. The object-oriented features have what?

Object-oriented programming languages ​​are encapsulation, inheritance, abstract, four main features and polymorphism.

20. What is the mechanism of polymorphism in java implementation is?

1, subclass inherits the parent class (the extends)
2, class implements the interface (the implements)

21. The inner class can refer to it containing class members do? Are there any restrictions?

absolutely okay. If it is not static inner classes, that there is no limit!

22. The String is the most basic data type?

Not, java.lang.String class is final, and therefore can not inherit this class, the class can not be modified.

23. A String S = "the Hello"; S = S + "World!"; The two lines of code execution, the content of the original String object is not changed in the end?

No. Because String is designed to be immutable (the immutable) class, so that all objects are immutable. In this code, s original point to a String object, content is "Hello", and then we were on the s + operator, then that object s points to whether there has been change it? The answer is no. At this time, s does not point to the original object, while pointing to another String object, content is "Hello world!", The original object still exists in the memory, just s this reference variables no longer point to it.

24. The String S = new new String ( "xyz"); created several String Object What is the difference between the two??

Or a two, "xyz" corresponds to an object, the object in a string constant buffer, constant "xyz" No matter how many times appears, is that a buffer. New String each write again, to create a new object, it creates a new String object based on the content of that constant "xyz" object. If you previously used the 'xyz', on behalf of the phrase does not create "xyz" himself, take directly from the buffer.

25. The String, StringBuffer, the StringBuilder What is the difference?

String String is an immutable,
the StringBuffer and StringBuilder variable string.
StringBuffer is thread-safe, StringBuilder is not thread safe.
StringBuilder is faster than StringBuffer speed.

26. The array has no length () this method? String has no length () this method?

Arrays are not length () this method, length attributes. String with a length () this method.

27. The following statement creates a total number of objects: String s = "a" + "b" + "c" + "d";

One object.
String s = "a" + " b" + "c" + "d"; System.out.println (s == "abcd");
results of the final print to be true.

28. What are the similarities and differences with the general exception runtime exception?

Anomalies indicate abnormal status during program execution can occur, runtime exceptions generally indicate abnormal operation of virtual machines that may be encountered, is a common run-time error. When abnormal, but does not require a statement uncaught when running java compiler must request a declaration of non-operational throw exceptions that may occur.

29. The error and exception What is the difference?

Restoration of error in the case of a serious problem not impossible but very difficult. For example, memory overflow. Program can not be expected to handle such a situation. exception that a design or implementation issues. In other words, it means that if run properly, the situation never happened.

30. Please write your most commonly seen in five runtime exception.

NullPointerException,ArrayIndexOutOfBoundsException,ClassCastException,SQLException,FileNotFoundException,IOException …

31. How JAVA language exception handling, keyword: throws, throw, try, catch , finally , respectively, what is the point on behalf of? In the try block you can throw an exception it?

throws is to obtain an exception
throw is thrown
try statements will be the exception quotes, to perform exception handling, you can also throw in the try block new exception
catch is that if there is an exception which will carry out his sentence
finally whether or not an exception will be executed statement

32. The JAVA There are several ways to implement a thread? What keywords synchronization method modified? Stop () and suspend () method Why not recommended?

① inheritance Thread class and override the run method

② implement runnable interfaces, override the run method of
synchronization methods with the synchronized keyword modifications.
stop () unsafe.
suspend () prone to deadlocks.

33. The SLEEP () and wait () What is the difference?

sleep is a method of the Thread class (Thread), leading to this thread to suspend the specified time, the opportunity to execute another thread, but monitoring status remains, to the post will be automatically restored. Call sleep will not release the object lock. wait is a method of the Object class, object calls this method leads to wait this thread to give the object lock, waiting to enter the pool waiting for a lock for this object, and only issued after notify method (or notifyAll) for this thread to lock this object before entering the target pool to get ready object lock into operation.

33. The synchronous and asynchronous similarities and differences, they use under what circumstances were? for example.

If data will be shared between threads. For example, it may be read by another thread after the writing of data, or data is being read may have been written by another thread, then the data is shared data, access must be synchronized.
When the application calls the method takes a long time to execute on the object, and do not want the program to wait for the process to return, you should use asynchronous programming, using asynchronous approach in many cases tend to be more efficient.

34. Synchronization There are several methods to achieve

There are two aspects to achieve synchronization, which are synchronized, wait and notify.

35. Start a thread is run () or start ()?

Start a thread that calls start () method, the thread is ready, then you can be scheduled to run state, a thread must be associated with some specific code execution, run () method is executing code associated with the thread.

36. When a thread enters a synchronized method of an object, other threads can access this object other methods?

Sub-situations:
whether other methods 1. Before adding the synchronized keyword, if not increase, it can.
2. If this internal method calls wait, you can enter other synchronized methods.
3. If the other two methods are added to the synchronized keyword, and internal do not call wait, can not.
4. If other methods are static, the synchronization lock it with the current class bytecode can not synchronize with the non-static method, since non-static method is this.

37. The thread state What?

In which Java threads usually have five states, create, ready, running, blocking and death.

38 describes the structure of the frame Collection

title

39. The difference between ArrayList and the Vector

Vector is thread-safe, ArrayList line is unsafe procedures, among which the method is thread sync. If only one thread can access to the collection, it is best to use ArrayList, because it does not consider thread safety, efficiency will be higher.
Vector data to the original 1-fold increase, ArrayList data growth of 0.5 times the original.

40. The difference between the HashMap and Hashtable

HashMap allows null as a key entry or the value, and not allowed Hashtable.
Hashtable is thread safe, and HashMap line program is unsafe.
HashMap efficiency faster than the HashTable.

41. The List and Map difference?

List data is stored in order, and allowing repeated; Map data is not stored in the order that the key can not be repeated, and its value can be duplicate.

42. The List, the Set, whether inherited from the Map Collection Interface?

List, Set Shi, Map not.

43. The List, the Map, the Set three interfaces, accessing elements, what are the differences?

List use the add (), Map using the put (), Set using the add ().

44. say storage properties and characteristics ArrayList, Vector, LinkedList of

ArrayList and Vector are using an array of stored data, query speed, while using a doubly linked list LinkedList implement storage, fast data insert.

45. remove duplicate set of a Vector elements

= New new newVector the Vector the Vector ();
the For (int I = 0; I <vector.size (); I ++) {
Object obj = vector.get (I);
IF (newVector.contains (obj);!
NewVector.add ( obj);
}
there is a simple way, HashSet set = new HashSet (vector );

46. The difference between the Collection and Collections

Collection is a set of interfaces parent class, inheritance and his main interface Set and List.
The Collections is help for a class set of classes, he offers a series of static methods to achieve a variety of search collection, sorting, and so thread-safe operations .

47. ** Set in the element is not repeated, then the method used to distinguish whether or not repeat it? Is == or equals ()? **

Set in the elements is not repeated, the elements used or not is repeated equals () methods of determination.

48. The two objects the same value (x.equals (y) == true) , but may have different hash code, this sentence right?

Correct.
If the object you want to save HashSet or HashMap, their equals are equal, then their hashcode value must be equal.
If it is not to be kept in HashSet or HashMap, it is not anything to do with the hashcode, and this time hashcode range is possible, for example arrayList stored object hashcode do not realize, of course, we have no reason not to achieve, usually to achieve.

49. say some commonly used classes, packages, interfaces, requested give 5

常用的类:BufferedReader BufferedWriter FileReader FileWirter String Integer
java.util.Date,System,Class,ArrayList,HashMap

常用的包:java.lang java.io java.util java.sql ,javax.servlet,org.apache.strtuts.action,org.hibernate
常用的接口:Remote List Map Document NodeList ,Servlet,HttpServletRequest,HttpServletResponse,Transaction(Hibernate)、Session(Hibernate),HttpSession

50. The difference between a byte stream and a character stream

Byte stream is Common binary stream, is read out of the bit
stream of characters is based on the byte stream in accordance with a character encoding process, the process is char

51. What is java serialization, how to achieve java serialization? Please explain the role or Serializable interface.

Converting the byte sequence object is called object serialization, deserialization to restore the object is called object byte sequence.
Implement the Serializable interface can be realized serialization.

52. Describe the principle mechanism JVM class document loading?

The JVM is loaded by a class and its subclasses ClassLoader achieved, Java ClassLoader is an important component of the Java runtime system. It is responsible for finding and class loading class files at run time.

53. What GC is the? Why should there be GC?

GC is a garbage collection means (Gabage Collection), memory handling is where programmers prone to problems, and forget or false memory recovery program will lead to instability or even collapse of the system, provided Java GC function can automatically monitor whether the object is more than scope so as to achieve the purpose of automatically reclaims the memory, Java language does not provide a display method operation of the release of allocated memory.

54. The the Java memory leak will do, please describe.

The so-called memory leak or a variable that refers to an object is no longer used by the program it has been occupied in memory. There are java garbage collection mechanism that ensures that when a object is no longer referenced by that object-oriented programming orphans when the object is garbage collector will automatically be removed from memory. Because Java uses garbage collection has managed to chart the way, you can eliminate the problem of reference cycles, for example, there are two objects refer to each other as long as they do not reach the root process, then the GC is also possible to recover them.

55. Can you write your own classes, also known as java.lang.String?

Yes, but at the time of application, you need to use your own class loader to load. Otherwise, the class loader to load the system is always just the java.lang.String jre.jar package.

Guess you like

Origin blog.csdn.net/weixin_43266090/article/details/90301409