JAVA interview questions (12)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_15127715/article/details/102540215

1, if you can make calls to non-static method from within a static method?

Not. Because the non-static method is associated with the object to be together, after you must create an object, it can invoke a method on the object, and the object does not need to create a static method call, can be called directly. That is, when a static method is called, may not create any instance of an object, if you issue a call to a non-static method from a static method, the non-static method on which the object is associated with it? This logic can not be established, so that a static internal method makes a call to a non-static methods.

2, the difference between the Integer and int

int is one of eight types of raw data provided by the java. Providing Java class for each primitive type package, Integer int is to provide a package java classes. int The default is 0, and the default value of Integer null, i.e. Integer can distinguish the difference between the value 0 and unassigned, int can not express the case of unassigned.
For example: To express the difference did not take the exam and test scores of 0, you can only use Integer. In the JSP development, the default Integer is null, it is used when displaying el expression text box is empty string, int default and default value 0, the display in the text box with the expression el time, the result is 0, it is, int type is not suitable as a web form data layer.

In Hibernate, if the OID defined type Integer, Hibernate can have a null value if it is judged in accordance with whether an object is temporary, if the OID defined for int type, which also need to set hbm mapping file unsaved- attribute value is 0.
Further, Integer provided a method of operating a plurality of integer-related, for example, a string to an integer, Integer also defines the maximum and minimum values of the constants represent integers.

3, Math.round (11.5) is equal to how much? Math.round (-11.5) equals how much?

Math class provides three methods of rounding related: ceil, floor, round, meaning the role of these methods and their corresponding names in English.
For example, ceil is the ceiling of the English meaning, then the method indicates rounding up the result Math.ceil (11.3) is 12, the result Math.ceil (-11.3) is -11; floor is the floor of the English meaning, the method it represents rounding down, results Math.ceil (11.6) is 11, the result Math.ceil (-11.6) -12; round method is most difficult to master, which indicates "rounding" algorithm Math.floor (x + 0.5), is about 0.5 and then adding the original digital rounding down, therefore, results Math.round (11.5) is 12, the result Math.round (-11.5) -11.
Here are some clerical error, floor is the floor of the English meaning, which says that rounding down, the result Math.floor (11.6) is 11, the result Math.floor (-11.6) -12;

4, Overload and Override the difference? Overloaded methods can change the type of return value?

Overload is overloaded meaning, Override is meant to cover, which is rewritten.
Overload Overload represents the name of the same method for a plurality of the same class, but the parameter list of these methods vary (i.e., different number or type of parameters).

Rewrite Override representation subclass can be the name of a method and parameters parent class is exactly the same instance of an object created by a subclass when calling this method, invoking the method defined subclass, which is equivalent to exactly the same manner that the parent class definition to cover, object-oriented programming which is a manifestation of polymorphism. When the parent class subclass covering, can only throw fewer exceptions than the parent, or the parent class is thrown exceptions thrown by the abnormal child, because subclasses can solve some of the problems of the parent class, the class can not be less than the Father there are more problems. Access subclass method can not be less than the greater of the parent class. If the parent class is private type, then the subclass is not restricted coverage exists, corresponds to a subclass adds new approach.

As for whether Overloaded methods can change the return type of the problem depends on what you want to ask are they going to do? The questions were very vague. If the parameter list of a few Overloaded methods are not the same, they are returnees may not be the same type of course. But I guess you want to ask the question: If the argument list of two methods exactly the same, whether they can make the return value to achieve different overloads Overload.

This is not enough, we can use reductio ad absurdum to illustrate the problem, because we sometimes call a method can not define variables return results that do not care about their return results, for example, we call map.remove (key) method, Although the remove method returns a value, but we often do not receive variables defined in the returned results, this time assume that the class has two names and a list of parameters exactly the same way, only the return of different types, java programmers can not be determined what are they going to want to call the method, because it can not be judged by the type of return results.
override can be translated to cover, from the literal you know, it covers a method and rewrite them in order to achieve different effects.

For us, the most familiar is to achieve coverage of the interface method, the interface in general is just a statement of the method, and when we realize, we need all the methods declared in the interface implementation. In addition to this typical usage, we may also cover the method of the parent class in a subclass in inheritance. In the coverage to pay attention to the following points:

1, methods of marking the cover must be exact and methods of marking are covered, in order to achieve coverage; 2, the return value of the method of covering must return a consistent and methods covered;
3. The method of covering the throwing an exception must be thrown exception consistent coating method, or a subclass;
4, the method can not be covered by the private, or in which only a new subclass defines a method and not subjected to cover.

Overload may be familiar to us, can be translated as overloading, it means that we can define some of the same name of the method, when defined by different input parameters to distinguish between these methods, then call, VM will be based on different parameters style, executed to select the appropriate method. In the heavy-duty use should pay attention to the following points:

1, in the use of heavy-duty only through different parameter style. For example, different types of parameters, number of different parameters, a different sequence parameter (of course, several types of parameters within the same method must be different, for example, may be fun (int, float), but not as fun (int, int ));
2, can not access, return type, exceptions thrown overloaded; 3, exception type and number of methods will not affect overloaded;
4, for the inheritance, if a method in the parent class is access is priavte, then it can not be overloaded in a subclass, if defined, only it defines a new approach, and will not achieve the effect of overloading.

5, the interface is inheritable interfaces? Are abstract class can implement (implements) the interface? Abstract class is inheritable specific category (concreteclass)? Can I have a static main method of abstract class?

Interface can be inherited interfaces. Abstract class can implement (implements) interface abstract class can inherit specific class. You can have static main method of abstract class.
Note: As long as understand the nature and role of interfaces and abstract classes, these questions are easy to answer, you think, if you are a designer java language, you would provide such support, if not provided, there is no reason ? If you did not provide a reason, the answer is in the affirmative.
Just remember that the only difference between abstract classes and ordinary classes that can not create an instance of an object and allow abstract methods.

6, Java What is the mechanism to implement polymorphism is?

Relying on the parent class or interface defined reference variable can point to a specific instance of an object implementation class or subclass, while the method of procedure calls only dynamic binding at runtime, the method specific instance of an object variable points is a reference, that is, the method of that object in memory that are running, but the method is not the type of reference variable defined.

7. What is the difference on abstractclass interface and grammar?

1. An abstract class can have a constructor, interface can not have a constructor.
2. abstract class can have an ordinary member variables, the interface is not an ordinary member variables
3. abstract class may contain non-abstract ordinary methods, all interface methods are abstract must not have non-abstract ordinary method.
4. Access Type abstract methods abstract class may be public, protected, and (default type, the eclipse although not given, nor it should), but the abstract method only public interface type, and the default is the public abstract type.
The abstract class may contain static methods, static methods can not contain an interface
variables 6. The abstract classes and interfaces can contain static member variables, static member variable access type abstract class may be any, but is only defined in the interface It can be publicstatic final type, and the default is the publicstatic final type.
7. A class can implement multiple interfaces, but can only inherit an abstract class.

8, abstract whether the method is also static, it also is a native, it is also synchronized?

The abstract method can not be static, because the abstract method is to achieve the quilt class, subclass and static and nothing to do!

The method represents a method to use the native another platform-independent programming language, there is no problem that subclass implementation, so it can not be abstract, can not be mixed with the abstract. For example, FileOutputSteam class hardware to deal with the underlying implementation is operating system dependent api implemented;

For example, in windows using c language, so to see jdk source code can be found in the definition of the open method of FileOutputStream as follows:
Private Native void open (String name) throwsFileNotFoundException;
if we want to use java to call someone else wrote the c language function, we are not called directly, we need to write a function in accordance with the requirements of java c language, but also that our c language function to call someone else c language function.

Since our c language function is based on the requirements of java to write, our c language function can be docked on the java, java side of the butt joint is to define a method to our c corresponding function, the corresponding java the method does not need to write specific code, but requires a statement native in front.
Questions about synchronized combination of the abstract, I think it does not work, because in my years of study and development, never not seen such a situation, and I think it should be a synchronized effect on a specific approach makes sense. Moreover, synchronization lock on the object synchronized synchronization method used is this, and abstract method can not determine what this yes.

9, inner classes can reference it contains the class members do? Are there any restrictions?

absolutely okay. If it is not static inner classes, that there is no limit!
If you put a static nested class within the class as a special case, and that in this case can not be accessed outside the class of ordinary members of the variable, and can only be accessed outside the class static member, for example, the following code:

class Outer
{
static int x;
static class Inner
    {
voidtest()
{
      syso(x);
}
    }
}

10, String s = "Hello"; s = s + "world!"; These two lines after the code is executed, the contents 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.

By the above description, it is easy to export another conclusion, if often string that various modifications, or that unforeseen changes, then use the String to represent string of words can cause a lot of memory overhead. Because String objects can not be changed after the establishment, so for each of the different strings, we need a String object to represent.

At this time, you should consider using StringBuffer class, which allows to modify, rather than have each different strings generating a new object. Moreover, the two types of object conversion very easy.
At the same time, we also know that if you want to use the same content strings, do not always have new a String. For example, we want to reference in the constructor for String s called a variable is initialized, it is set to an initial value, this should be done:

public class Demo {
private String s;
...
public Demo {
s = "Initial Value";
}
...
}

Instead of s = new String ( "Initial Value ");
the latter each will call the constructor, new objects, poor performance and memory overhead is large, and there is no sense, because the String object is not changed, the same string for the content as long as a String object to represent it. Also he said repeatedly call the above constructor to create multiple objects, their
String type attribute s point to the same object.

The above conclusion is also based on this fact: For string constants, if the content is the same, Java believe that they represent the same String object. And call the constructor with the keyword new, always creates a new object, regardless of the content is the same.

As for why should String class designed to be immutable class, it is its use decisions. In fact, not only String, many Java standard library classes are immutable. In the development of a system, we sometimes need to design an immutable class, to pass a group of related values, and this is reflected in the object-oriented thinking. Immutable class has some advantages, such as its object is read-only, so multi-threaded concurrent access there will not be any problems.

Of course there are some disadvantages, such as each state have a different object to represent, may cause performance problems. So Java standard class library also provides a variable version, StringBuffer.

If you think I write okay, please pay attention to my public number, timed push dry day.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_15127715/article/details/102540215