Java Foundation interview (II.)

Source: Internet
Author: User

11. Is it possible to make a call to a non-static method from within a static method?

No. Because the non-static method is to be associated with an object, you must create an object before you can make a method call on the object, and the static method call does not need to create the object, which can be called directly. That is, when a static method is called, there may not be any instance objects created, and if a call to a non-static method is emitted from a static method, which object does that non-static method relate to? This logic cannot be set up, so a static method is issued inside a call to a non-static method.

12, the difference between integer and int

int is one of the 8 raw data types provided by Java. Java provides encapsulation classes for each primitive type, and integer is the wrapper class provided by Java for Int. The default value for int is 0, and the default value for integer is null, which means that integer distinguishes between unassigned and value 0, and int cannot express an unassigned condition.

For example, if you want to express the difference between not taking an exam and 0 of your exam results, you can use only integer. In JSP development, the default for integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the default value of int defaults to 0, so when the El expression is displayed in the text box, the result is 0, so that int does not work as a type of form data for the Web tier.

In Hibernate, if the OID is defined as an integer type, hibernate can determine whether an object is temporary based on whether its value is null or not, and if the OID is defined for the int type, It is also necessary to set its Unsaved-value property to 0 in the HBM mapping file.

In addition, the integer provides multiple integer-related action methods, such as converting a string to an integer, and also defining constants that represent the maximum and minimum values of integers.

13, Math.Round (11.5) how much? Math.Round (-11.5) how much?

The math class provides three methods related to rounding: Ceil, floor, and round, which correspond to the meanings of their English names.

For example, the English meaning of Ceil is the ceiling, the method represents the upward rounding, the result of Math.ceil (11.3) is the result of 12,math.ceil (-11.3) is the -11;floor of the English meaning is the floor, the method represents a downward rounding, Math.ceil ( 11.6) The result of 11,math.ceil (-11.6) is the result of-12; the most difficult to master is the round method, which means "rounding", the algorithm is Math.floor (x+0.5), The original number is added to 0.5 and then rounded down, so the result of Math.Round (11.5) is 12,math.round (-11.5) as a result of-11.

14. What is the difference between overload and override? Can the overloaded method change the type of the return value?

Overload is the meaning of overloading, override is the meaning of overrides, that is, rewriting.

Overloaded overload means that there can be more than one method with the same name in the same class, but the parameter lists of these methods vary (that is, the number of arguments or the type differs).

overriding override means that a method in a subclass can be exactly the same as the name and parameters of a method in the parent class, and invoking the method from an instance object created by the subclass invokes the definition method in the subclass, which is equivalent to overwriting the exact same method defined in the parent class. This is also a representation of the polymorphism of object-oriented programming. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception thrown by the parent class, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class. The subclass method can only be accessed more than the parent class, and cannot be smaller. If the method of the parent class is private, then the subclass does not have an override limit, which is equivalent to adding a new method to the subclass.

As to whether the overloaded method can change the type of return value, it depends on what you want to ask. The subject is very vague. If several overloaded methods have different parameter lists, their type of returnees can of course be different. But I guess the question you want to ask is: if the parameter list of the two methods is exactly the same, can you make them different from the return values to implement the overloaded overload. This is not possible, we can use contradiction to illustrate this problem, because we sometimes call a method can also not define the return result variable, that is, do not care about its return results, for example, when we call the Map.Remove (key) method, although the Remove method has a return value, However, we usually do not define a variable to receive the return result, when it is assumed that the class has two names and the parameter list is exactly the same method, only the return type is different, Java will not be able to determine the programmer's bottom is to invoke which method, because it can not be judged by the return result type.

Override can be translated as an overlay, literally knowing that it is covering a method and rewriting it to achieve different effects. The most familiar overlay for us is the implementation of the interface method, which is generally just a declaration of the method in the interface, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class with the subclass in the inheritance. Be aware of the following points in the overlay:

1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;

2. The return value of the overridden method must be the same as the return of the overridden method;

3. The exception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;

4. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.

Overload may be more familiar to us, can be translated as overloaded, it means that we can define some of the same name, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, to choose the appropriate method of execution. The following points are to be noted in using overloading:

1. You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must not be the same, such as can be Fun (int,float), but not fun (int,int));

2, can not be overloaded by access rights, return type, thrown exception;

3, the method of the exception type and number will not affect the overload;

4, for inheritance, if a method in the parent class is the access permission is PRIAVTE, then it can not be overloaded in the subclass, if defined, but also only defined a new method, but not to achieve the overloaded effect.

15. Can interfaces inherit interfaces? Can an abstract class implement (implements) interfaces? Can an abstract class inherit a concrete Class (Concreteclass)? Is there a static main method in the abstract class?

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes. There can be static main methods in an abstract class.

Note: As long as you understand the nature and role of interfaces and abstract classes, these questions are good answers, and you think, if you are the Java language Designer, will you provide such support, if not provided, what is the reason? If you have no reason not to provide, then the answer is yes.

Just remember that the only difference between an abstract class and a normal class is that you cannot create an instance object and allow an abstract method.

16. What is the mechanism for polymorphism in 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

17. What is the difference between AbstractClass and interface syntax?

1. Abstract classes can have construction methods, and interfaces cannot have constructors.

2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface

3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.

4. The access type of an abstract method in an abstract class can be public,protected and (the default type, although

Eclipse does not give an error, but it should not), but the abstract method in the interface can only be of the public type, and the default is the public abstract type.

5. Abstract classes can contain static methods, and interfaces cannot contain static methods

6. Abstract classes and interfaces can contain static member variables, the access type of static member variables in the abstract class can be arbitrary, but the variables defined in the interface can only be publicstatic final type, and the default is the Publicstatic final type.

7. A class can implement multiple interfaces, but can inherit only one abstract class.

18, the abstract method can be static at the same time, whether it can be native at the same time, whether it can be synchronized at the same time?

Abstract methods can not be static, because the abstraction of the method is to be implemented by the quilt, and static and the sub-class is not related!

The native 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. For example, the Fileoutputsteam class wants to deal with hardware, and the underlying implementation uses an operating system-related API implementation, for example, in the C language of Windows, so view the source code of the JDK, You can see that the FileOutputStream open method is defined as follows:

Private native Void Open (Stringname) throwsfilenotfoundexception;

If we want to use Java to call the C language function written by others, we can not directly call, we need to follow the Java requirements to write a C language function, and our C-language function to call someone else's C language function. Because our C language function is written according to Java requirements, we this C language function can interface with Java, Java side docking way is to define the method corresponding to our C function, the corresponding method in Java does not need to write the specific code, but need to declare native before.

I don't think it's a problem with synchronized and abstract, because I've never seen it in my years of study and development, and I think 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.

19. Can an inner class reference a member of its containing class? Are there any restrictions?

It's perfectly possible. If it's not a static inner class, there's no limit!

If you treat a static nested class as a special case of an inner class, you cannot access the normal member variables of the outer class, but only the static members in the outer class, for example, the following code:

Class Outer

{

static int x;

Static Class Inner

{

Voidtest ()

{

SYSO (x);

}

}

}

20, 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. Because string is designed as an immutable (immutable) class, all of 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 on S, then the object pointed to by S has changed it? The answer is no. At this point, S does not point to the original object, and point to another string object, the content is "Hello world!", the original object is still in memory, but s this reference variable no longer points to it.

With the instructions above, it is easy to derive another conclusion that if you frequently make a variety of changes to the string, or if the changes are unpredictable, then using string to represent the strings can cause significant memory overhead. Because a string object cannot be changed after it is established, a string object is required for each different string. At this point, you should consider using the StringBuffer class, which allows you to modify instead of creating a new object for each different string. Also, the conversion of these two kinds of objects is very easy.
At the same time, we can also know that if you want to use the same content string, you do not have to new a string each time. For example, we want to initialize a string reference variable named S in the constructor, set it to the initial value, and should do this:
Public class Demo {
Private String S;
...
Public Demo {
s = "Initial Value";
}
...
}

Rather than
s = new String ("Initial Value");
The latter invokes the constructor every time, generates new objects, performs poorly and has a large memory overhead, and makes no sense, because the string object is immutable, so for a string of the same content, just one string object to represent it. It also says that multiple calls to the above constructor create several objects whose string type property s all point to the same object.
The above conclusion is also based on the fact that, 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.
As for why the string class is designed as an immutable class, it is the purpose of its decision. In fact, not only string, but many classes in the Java Standard Class library are immutable. In the development of a system, we sometimes need to design immutable classes to pass a set of related values, which is the embodiment of object-oriented thinking. Immutable classes have some advantages, such as because their objects are 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.

Keep up!

Java Foundation interview (II.)

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.