Java Interview Bible Study notes (i)

Source: Internet
Author: User
Tags bitwise throwable

Basic Java Learning (interview cookbook):
What is the difference between the "= =" and the Equals method in 1.java?
(1) The = = operator is specifically used to compare the values of two variables equal, that is, to compare variables stored in memory for the same value, to compare two basic types of data or two reference variables are equal, can only use the = = operator. If the data that a variable points to is object type, then it involves two blocks of memory, the object itself occupies a block of memory (heap memory), the variable also occupies a piece of memory, such as Object obj = new Object (); the variable obj is a memory, and new object () is another memory. 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.
(2) The Equals method is used to compare the contents of two separate objects, like to compare two people's appearance is the same, compare the two objects are independent, for two different objects, their first address is different, but the content of the object is the same,    Returns TRUE. String comparisons are basically all using the Equals method. If a class does not have its own definition of the Equals method, its default Equals method (inherited from the object class) is using the = = operator and is comparing whether the object pointed to by two variables is the same object. If you want to compare the contents of the two instance objects created by the class, you must override the Equals method.
2. What is the difference between a static variable and an instance variable?
(1) syntax definition: Static variables are preceded by the static keyword, the instance variable is not added before
(2) When a program runs: an instance variable belongs to an object's properties, an instance object must be created, and the instance variable will be allocated space before the instance variable can be used. Static variables are not part of an instance object, but belong to a class, so it is also called a class variable, as long as the program loads the class's bytecode, without creating any instance objects, the static variables will be allocated space, static variables can be used.
(3) Summary, an instance variable must be created before it can be used by this object, and static variables can be referenced directly using the class name.
3. Is it possible to make a call to a non-static method from within a static method?
No. The non-static method is to be associated with an object and must be created before a method call can be made on the object, and the static method call does not need to create the object, which can be called directly. When a static method is called, no instance object may have been created, and if a call to a non-static method is emitted from a static method, which object is the non-static method associated with? Logic cannot be established.
What is the difference between 4.Integer and int?
int is one of the original data types provided by Java in 8. Java provides encapsulation classes for each primitive type, and integer is the encapsulation class provided by Int.
(1) The default value of int is 0, and the default value of integer is Null,integer can distinguish between assignment and value 0, int cannot express the case of assignment.
(2) integer provides multiple integer-related operation methods, such as converting a string to an integer, defining the integer maximum and minimum constants.
5. Bit operations are directly supported by the CPU and are the most efficient. Shift a number to the left N-bit, which is equal to the N-shift by 2,<< shift left >> right
6. A. Java source file can include multiple classes, but only one public class, and the class name of public must match the file name
7.java has Goto,goto is reserved word, want to not use in Java
8. Tell me the difference between & and &&?
(1) The same point:& and && can be used as logical AND operator. True if both expressions on both sides of the operator are true, otherwise false
(2) different points:&& has a short-circuit function, if the first expression is false no longer computes the second expression,& does not have a short-circuit function. & can be used as an operator when an expression on either side of the & operator is not a Boolean,,& represents a bitwise AND operation. You typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of an integer.
How do I jump out of the current multiple nesting loops in 9.java?
(1) Define a label before the outer Loop statement, and then use the labeled Break statement in the code of the inner loop body to jump out of the outer loop.
Begin
Break begin;
(2) Generally do not use the label this way, but let the results of the outer loop condition expression can receive the inner Loop Body code control, you can set a marker bit.
Boolean find = false;
for (int i = 0; i < n &&!find; i++) {
for (int j = 0; J < N; j + +) {
if (data[i][j] = = key) {
find = true;
Break
}
}
}
In 10.switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant (larger font), and an integer expression can be either an int primitive type or an integer wrapper type. Because Byte,short,char can be implicitly converted to int, these types and the wrapper classes of these types are all possible. Long and string do not conform to the syntax, and cannot be implicitly converted to int, and cannot be used directly with a switch statement.
One. (1) Short S1 = 1;S1 = S1 + 1; Because the type of the expression is automatically promoted by the s1+1 operation, the result is type int, and the compiler reports an error that requires a cast type.
(2) Short S1 = 1; S1 + = 1; since + = is an operator in the Java language, the Java compiler will treat it with special processing, which can be compiled.
The type 12.char variable is used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type variable can of course store Chinese characters. Unicode encoding consumes two bytes, so variables of type char are also two bytes
13. Design a 10 billion calculator specific steps
Designing a Class (BigInteger) can represent large integers, and provides the ability to subtraction with another integer.
(1) There are two member variables inside, one for symbol (sign) and the other for the binary number of the numeric value (computed by the bitwise operation principle) in a byte array.
(2) There is a construction method that converts a string containing multiple values into internal symbols and byte arrays
(3) Provide subtraction function add substract multiply divide
14. When you use the final keyword to decorate a variable, the reference variable cannot be changed, and the contents of the object to which the reference variable refers can be changed.
The 15.Math class provides three methods related to rounding: ceil, floor, round.
(1) ceil English meaning ceiling, which means rounding up
(2) floor in English, indicating downward rounding
(3) Round method means rounding, the algorithm is Math.floor (x+0.5), will be the original number plus 0.5 after the next rounding
16.public,private,protected Scope
(1) Public current class + class within the same package + classes in different packages + descendant classes
(2) Protected current class + class in same package + descendant class
(3) friendly the current class + class within the same package (if no access modifier is written on the decorated element, representing friendly)
(4) Private Current class
What is the difference between 17.overload and override?
(1) overload is overloaded meaning, override is overriding meaning, that is, rewriting
(2) overload means that there can be more than one method with the same name in the same class, when the parameter lists of these methods are different (number of arguments or types) but if the parameter list of the two methods is exactly the same, the overloaded overload cannot be implemented by the different return values. If you do not define a variable that receives the returned result, it cannot be judged by the return type.
(3) Override means that the method in the subclass can be exactly the same as the name and parameters of a method in the parent class, and when called by the instance object created by the subclass, the method defined in the subclass is called, which is equivalent to overwriting the exact same method as defined in the parent class. This is a representation of the polymorphism of object-oriented programming.
18. The constructor constructor cannot be inherited and therefore cannot override override, but can be overloaded overload
19. The only difference between an abstract class and a normal class is that you cannot create an instance object and allow an abstract method
20. When writing the Clone () method, there is usually a line of code Super.clone (), which should have a default behavior for clone, first to copy the members of the parent class into place, and then to copy their own members.
21. What are the aspects of object-oriented features?
The object-oriented programming language has 4 main features of encapsulation, inheritance, abstraction and polymorphism.
(1) Packaging: packaging is to ensure that the software components have a good modularity of the foundation, the goal of the package is to achieve "high cohesion, low coupling" software components, to prevent the process of mutual dependence of the changes caused by the impact. In object-oriented programming languages, objects are the most basic unit of encapsulation, and object-oriented encapsulation is clearer and more powerful than traditional language encapsulation. Object-oriented encapsulation is to encapsulate the code describing the properties and behavior of an object in a "module", which is a class in which the attribute is defined by a variable, and the behavior is defined by the method, which directly accesses the attributes in the same object. In general, just remember to let the variable and access to the variable in the method put together, the member variables in a class are all defined as private, only the class of their own methods can access to these member variables, which basically implement the object encapsulation, it is easy to find the method to assign to this class, is basically object-oriented programming. Grasp a principle: Put the method of operation of the same thing and the related method in the same class, put the method and its operation data in the same class.
(2) Abstraction: Abstraction is the similarity and commonality of something, which is then categorized into a class that considers only similarities and commonalities of these things, and ignores those aspects that are irrelevant to the current subject and goal, focusing on the aspects related to the current goal. For example, when you see an Ant and an elephant, you can imagine the similarities between them, which is abstraction. Abstraction includes two aspects of behavioral abstraction and State abstraction.
(3) Inheritance: When defining and implementing a class, it can be done on the basis of an already existing class, the content defined by the already existing class as its own content, and can be added to a number of new content, or modify the original method to make it more suitable for special needs, which is inheritance. Inheritance is the mechanism by which subclasses automatically share the parent data and methods, which is a relationship between classes, which improves the reusability and extensibility of the software.
(4) Polymorphic: State refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable will point to which class of the instance object, the reference variable emitted by the method call is the method implemented in the class, Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism. Polymorphism enhances the flexibility and extensibility of the software. For example, the Userdao in the following code is an interface that defines a reference variable Userdao the instance object pointed to by Daofactory.getdao () is returned at execution time, sometimes pointing to the Userjdbcdao implementation, Sometimes pointing to the implementation of Userhibernatedao, so that, without modifying the source code, you can change the Userdao point to the specific class implementation, resulting in Userdao.insertuser () method invocation of the specific code also changed, That is, sometimes the Insertuser method of Userjdbcdao is called, sometimes the Insertuser method of Userhibernatedao is called:
Userdao Userdao = Daofactory.getdao ();
Userdao.insertuser (user);
What is the mechanism for imagine polymorphism in 22.java?
A reference variable that is defined by a parent class or interface can point to a subclass or an instance object of a specific implementation class, and the method called by the program is dynamically bound at run time, that is, the method that refers to the specific instance object that the variable points to, that is, the method of the object that is running in memory, rather than the method defined in the
What is the difference between 23.abstract class and interface?
(1) Abstract: Class containing the abstract modifier is an abstract class, an instance object that the abstract class cannot create. A class containing an abstract method must be defined as an abstract Class,abstract class in which the method does not have to be abstracted. Abstract class definitions must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static method. If the subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type.
(2) interface (interface): Can be described as a special case of abstract classes, all the methods in the interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final.
(3) Grammatical differences:
A. An abstract class can have a constructor method, which cannot be constructed in an interface.
B. There can be ordinary member variables in an abstract class, there are no ordinary member variables in the interface
C. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
D. The type of access for abstract methods in an abstract class can be public,protected and (the default type friendly, although Eclipse does not give an error, but should not), but the abstract method in the interface can only be of the public type, and the default is public The abstract type.
E. A static method can be included in an abstract class, and the interface cannot contain static methods
F. Both abstract classes and interfaces can contain static member variables, and the access types of static member variables in an abstract class can be arbitrary but the variables defined in the interface can only be public static final types, and the public static final type is the default.
G. A class can implement multiple interfaces, but can inherit only one abstract class.
(4) Application of the difference: the interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules, and the abstract class in the implementation of the code to play a role, you can implement code reuse. For example, the template method design pattern is a typical application of the abstract class, the parent class method in the middle of the code is not deterministic, left to the sub-class, the template method to design the pattern.
The method of 24.abstract can not be static, because abstract methods are to be implemented by the quilt class. Static is not related to subclasses
The Nativenative method means that the method is implemented with another platform-dependent programming language, and there is no problem with the implementation of the quilt class, so it cannot be abstract and cannot be mixed with abstract.
Synchronized should be useful in a specific way. Furthermore, the synchronization lock object used by the synchronized synchronization on the method is this, and the abstract method cannot determine what this is.
25. Inner class: The inner class is a class defined inside a class, the inner class can access the member variables of the outer class, the inner class cannot define a static member (static member is not an object's attribute, just to find a shelter, so need to put in a class, so little things, And you're going to put it in a class inside the class, too much! Provide inner class, not for letting you do this kind of thing, boring, not letting you do it. I think it might be that since static members are like global variables of the C language, and inner classes are often used to create internal objects, it is meaningless to put "global variables" in internal classes, and since they are meaningless things, they should be banned), and the inner classes can directly access member variables in the outer class. An inner class can be defined outside the method of an external class, or it can be defined in the method body of an external class.
public class outer{
int out_x = 0;
public void Method () {
Final int in_x = 0;
Inner1 inner1 = new Inner1 ();
Class inner2{
public void Method () {
out_x = in_x + 3;
}
}
Inner2 inner2 = new Inner2 ();
}
public class inner1{
Inner class defined outside the method body
}
}
(1) The access type of the inner class defined outside the method body can be public,protecte, default (friendly), private, and so on 4 types, which is as if the member variables defined in the class have 4 access types, they determine whether the definition of this inner class is visible to other classes In this case, we can also create an instance object of an inner class outside, and when creating an instance object of an inner class, you must first create an instance object of the outer class, and then use the instance object of the outer class to create an instance object of the inner class, with the following code:
Outer Outer = new Outer ();
Outer.inner1 inner1 = Outer.new Innner1 ();
(2) An inner class defined inside a method cannot have an access type modifier before it, as if the local variable is defined in the method, but the inner class can be preceded by a final or abstract modifier. This inner class cannot be referenced by other classes that are not visible to other classes, but the instance objects created by this inner class can be passed to other classes for access. This inner class must be defined first, and then used, that is, the definition code of the inner class must appear before the class is used, which is the same as if the local variable in the method must be defined before it is used. This inner class can access local variables in the method body, but must have a final modifier before the local variable.
(3) The inner class defined outside the method can be preceded by the static keyword, thus becoming a static Nested (nested) class, which no longer has an intrinsic class, all, in the narrow sense, it is not an inner class. The static Nested class has no difference in the behavior and functionality of the normal class at run time, except that there are some differences in the syntax for programming references, and the name of the static Nested class is called "External class name" in the outside reference. You can create a static Nested class directly outside the instance object that you do not need to create an external class. Outer.Inner Inner = new Outer.Inner ();
Because the static Nested class does not depend on the instance object of the external class, the static Nested class can access the non-static member variables of the outer class. When you access the static Nested class in the external class, you can directly use the name of the static Nested class without adding the name of the outer class, and in the static Nested class you can refer directly to the static member variables of the outer classes. You do not need to add the name of an external class.
26. An inner class can reference a member of its containing class. If it is not a static inner class, there is no limit.
If you treat a static nested class as a special case of an inner class, you cannot access the ordinary member variables of the outer class, but only the static members in the outer class, such as the following code:
public class outer{
static int out_x = 0;
Static Class inner{
void Test () {
System.out.println (out_x);
}
}
}
27.Anonymous Inner Class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)
The 28.super.getclass () method call.
What is the output of the following program?
Import Java.util.Date;
public class Test extends date{
public static void Main (string[] args) {
New test (). Test ();
}

public void Test () {
System.out.println (Super.getclass (). GetName ());
}
}
The result is test
In the test method, the GetClass (). GetName () method is called directly, and the test class name is returned, because GetClass () is defined in the object class as final, the subclass cannot overwrite the method, so it is called in the test method GetClass ( The. GetName () method, which is actually called the GetClass () method inherited from the parent class, is equivalent to calling Super.getclass (). GetName () method, so Super.getclass (). GetName () The method returned should also be test. If you want the name of the parent class, you should use the following code: GetClass (). Getsuperclass (). GetName ();
29.String is not the most basic data type. The basic data types are byte, int, char, long, float, double, Boolean, and short.
The Java.lang.String class is of the final type, so you cannot inherit the class or modify the class. To improve efficiency and save space, we should use the StringBuffer class.
30.String s = "Hello"; s = s + "world!"; After the execution of these two lines of code, did the content in the original string object change?
No, string is designed to be immutable (immutable) class, so all its objects are immutable objects. In this code, s originally pointed to a string object, the content is "Hello", and then we have a + operation, then, S does not point to the original object, and pointed to another string object, the content is "Hello world!", the original object is still in memory, Just s This reference variable no longer points to it.
(1) The StringBuffer class, which allows modifications to be generated instead of each different string, is to generate a new object. Also, the conversion of these two kinds of objects is very easy.
(2) For string constants, if the content is the same, Java considers them to represent the same string object. Calling the constructor with the keyword new always creates a new object, regardless of whether the content is the same.
(3) The immutable class has some advantages, for example, because its object is read-only, so there is no problem with multithreading concurrent access. Of course, there are some shortcomings, such as each different state to have an object to represent, may cause performance problems. So the Java Standard Class Library also provides a mutable version, the StringBuffer.
31.String s = new string ("XYZ"); several string Object created? What is the difference between the two?
Two or one, "xyz" corresponds to an object, which is placed in a string constant buffer, and the constant "XYZ" is the one in the buffer, no matter how many times it occurs. The new string, each time it is written, creates an object that creates a new string object based on the contents of that constant "XYZ" object. If you've used ' xyz ' before, that means it won't create "xyz" yourself, just take it from the buffer.
The difference between 32.String and StringBuffer:
(1) Same point: The Java platform provides two classes: string and StringBuffer, which can store and manipulate strings, that is, character data that contains multiple characters.
(2) different points:
The A.string class provides a string of values that cannot be changed. The string provided by the StringBuffer class is modified.
B.string implements the Equals method, new string ("ABC"). The result of equals (new string ("ABC") is true, and StringBuffer does not implement the Equals method, so the new StringBuffer ("abc"). The result of equals (new StringBuffer ("abc") is false.
C.string overrides the Hashcode method, and StringBuffer does not overwrite the Hashcode method, the problem occurs when the StringBuffer object is stored in the Java collection class.
33, how to convert a comma-delimited string into an array?
(1) String[] result = Str.split (",");
(2) StringTokenizer is a string-delimited parsing type, belonging to: Java.util package.
String str = "He,ll,on,iha,o";
StringTokenizer Tokener = new StringTokenizer (str, ",");
String[] result = new String[tokener.counttokens ()];
int index = 0;
while (Tokener.hasmoreelements ()) {
result[index++] = Tokener.nexttoken ();
}
34. The array does not have the length () method, has the length property, and string has the length () method.
35.String s= "A" + "B" + "C" + "D"; how many objects did the statement create altogether?
1, Javac compilation can be directly added to the string constants of the expression to optimize, do not have to wait until the run time to do the addition processing, but at compile time to remove the plus sign, directly compiled into a result of these constants connected.
36.try {} has a return statement, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return?
public class Smallt
{
public static void Main (String args[])
{
Smallt t = new smallt ();
int b = T.get ();
System.out.println (b);
}

public int Get ()
{
Try
{
return 1;
}
Finally
{
return 2;
}
}
}
The return result is 2.
Cause: The function called by the return statement in try executes before the function called in Finally, that is, the return statement executes first, and the finally statement executes, so the returned result is 2. Return does not return the function immediately, but the return statement executes, and the returned result is placed in the function stack, and the function does not return immediately, and it does not actually begin to return until the finally statement is executed.
Conclusion: The code in finally executes after the return and break statements.
37.final, finally, finalize the difference.
(1) Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overwritten, and that the class is not inheritable. Internal classes to access local variables, local variables must be defined as final types
(2) Finally is a part of the exception handling statement structure that is always executed.
(3) Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource collections, such as closing the file. The JVM does not guarantee that this method is always called.
Java technology uses the Finalize () method to do the necessary cleanup work before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. It is defined in the object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup work. The Finalize () method is called on the object before the object is deleted by the garbage collector.
38. What are the similarities and differences between runtime exceptions and general exceptions?
An exception represents an unhealthy state that may occur during a program's run, and a run-time exception that represents an exception that may be encountered in a typical operation of a virtual machine is a common run error. The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.
What is the difference between 39.error and exception?
Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations. Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.
The simple principle and application of exception handling mechanism in 40.Java.
(1) An exception is an unusual condition or error that occurs when a Java program runs (not compiled).
(2) Java classifies exceptions, different types of exceptions are represented by different Java classes, and the root class for all exceptions is java.lang.throwable,throwable and two sub-classes are derived: Error and Exception,error Represents a serious problem that the application itself cannot overcome and recover, and the program has only a dead copy, such as a system problem such as memory overflow and thread deadlock. Exception said that the program can also overcome and restore the problem, which is divided into system anomalies and common anomalies, system anomalies are the defects of the software itself caused by problems, that is, the software developers to consider the problem caused by poor, software users can not overcome and restore this problem, In this case, however, the software system can continue to run or let the software die, for example, array scripting out of bounds (arrayindexoutofboundsexception), null pointer exception (NULLPOINTEREXCEPTION), Class Conversion Exception (classcastexception), common exception is the operation of the environment changes or anomalies caused by the problem, is the user can overcome the problem, for example, network disconnection, hard disk space is not enough, after such an exception, the program should not die.
(3) Java provides a different solution for system exceptions and common exceptions, and the compiler enforces common exceptions that must try: Catch processing or using the throws declaration continues to be thrown to the upper call method processing, so the common exception is also called the checked exception, and the system exception can be handled or not handled, so the compiler does not enforce with try. catch processing or declaration with throws, so system exceptions are also known as unchecked exceptions.

























Java Interview Bible Study notes (i)

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.