In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the interview questions for Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the interview questions for Java"?
11. Is it possible to make calls to non-static methods from within a static method?
No. Because non-static methods are associated with an object, an object must be created before a method call can be made on that object, while the static method can be called directly without creating an object. That is, when a static method is called, no instance object may have been created. If a call to a non-static method is made from a static method, which object is the non-static method associated with? This logic does not hold, so a call to a non-static method is made internally by a static method.
12. The difference between Integer and int
Int is one of the eight raw data types provided by java. Java provides wrapper classes for each primitive type, and Integer is the wrapper class that java provides for int. The default value of int is 0, while the default value of Integer is null, that is, Integer can tell the difference between an unassigned value and a value of 0, while int cannot express an unassigned case.
For example, to express the difference between not taking the exam and having a test score of zero, you can only use Integer. In JSP development, the default of Integer is null, so when the el expression is displayed in the text box, the value is a blank string, while the default value of int is 0, so when the el expression is displayed in the text box, the result is 0, so int is not suitable for the web layer form data type.
In Hibernate, if OID is defined as Integer, then Hibernate can determine whether an object is temporary based on whether its value is null, and if OID is defined as int, you also need to set its unsaved-value property to 0 in the hbm mapping file.
In addition, Integer provides several integer-related operations, such as converting a string to an integer, and Integer defines constants that represent the maximum and minimum values of an integer.
13. How much is Math.round (11.5)? how much is Math.round (- 11.5)?
Three rounding-related methods are provided in the Math class: ceil, floor, and round, which correspond to the meaning of their English names.
For example, the English meaning of ceil is ceiling, and the result of Math.ceil (11.3) is 12 Math.ceil (- 11.3). The result of Math.ceil (11.6) is-12. The most difficult to master is the round method, which stands for "rounding", and the algorithm is Math.floor (x = 0.5), which adds 0.5 to the original number and then rounds it down, so the result of Math.round (11.5) is-11.
Here are some clerical mistakes. Floor means floor in English, and this method means rounding down. The result of Math.floor (11.6) is 11 Math.floor (- 11.6). The result is-12.
14. What's the difference between Overload and Override? Can the method of Overloaded change the type of return value?
Overload means overloading and Override means overwriting, that is, rewriting.
Overloading Overload means that there can be multiple methods with the same name in the same class, but each of these methods has a different parameter list (that is, a different number or type of parameters).
Overriding Override means that a method in a subclass can have exactly the same name and parameters as a method in the parent class. When this method is called through the instance object created by the subclass, the defined method in the subclass will be called, which is equivalent to overwriting the exact same method defined in the parent class, which is also a manifestation of the polymorphism of object-oriented programming. When a subclass overrides the methods of the parent class, it can only throw fewer exceptions than the parent class, or the child exception thrown by the parent class, because the subclass can solve some of the problems of the parent class and cannot have more problems than the parent class. The access rights of subclass methods can only be larger than those of the parent class, not smaller. If the method of the parent class is of type private, then there is no override restriction for the subclass, which is equivalent to the addition of an entirely new method to the subclass.
As for the question of 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 return types can of course be different. But I guess the question you want to ask is: if the parameter lists of the two methods are exactly the same, whether you can overload Overload by making their return values different. This is not possible, we can use absurdity to illustrate this problem, because sometimes we can call a method without defining the return result variable, that is, do not care about the return result. For example, when we call the map.remove (key) method, although the remove method has a return value, we usually do not define the variable that receives the return result. At this point, assuming that there are two methods in the class with exactly the same name and parameter list, but with different return types, java cannot determine which method the programmer wants to call, because it cannot tell by the return result type.
Override can be translated as overwriting. Literally, it overrides a method and rewrites it in order to achieve different effects. The most familiar overlay for us is the implementation of interface methods, which are generally only declared in the interface, but when we implement them, we need to implement all the methods declared by the interface. In addition to this typical usage, we may also override methods in the parent class in the subclass in inheritance. Pay attention to the following points when covering:
1. The logo of the covered method must exactly match that of the covered method in order to achieve the effect of coverage.
2. The return value of the overridden method must be the same as that of the overridden method
3. The exception thrown by the overridden method must be consistent with the exception thrown by the overridden method, or its subclass
4. The overridden method cannot be private, otherwise a new method is defined in its subclass and it is not overridden.
Overload may be familiar to us and can be translated as overloading, which means that we can define some methods with the same name, distinguish these methods by defining different input parameters, and then when called again, VM will choose the appropriate method to execute according to different parameter styles. Note the following when using overloads:
1. Only different parameter styles can be passed when overloading is used. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must be different, for example, it can be fun (int,float), but not fun (int,int))
2. Cannot be overloaded by access permission, return type, and thrown exception
3. The exception type and number of methods will not affect the overloading.
4. For inheritance, if a method has access permissions of priavte in the parent class, then it cannot be overloaded in the subclass. If defined, it will only define a new method and will not achieve the effect of overloading.
15. Can the interface inherit it? Can abstract classes implement (implements) interfaces? Can abstract classes inherit concrete classes (concreteclass)? Can there be static main methods in abstract classes?
An interface can inherit an interface. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes. You can have static main methods in an abstract class.
Note: these questions are easy to answer as long as you understand the nature and function of interfaces and abstract classes. Think about whether you would provide such support if you were the designer of the java language, and if not, is there any reason why not? If you have no reason not to provide, the answer is yes.
Just remember that the only difference between an abstract class and a normal class is that you cannot create instance objects and allow abstract methods.
16. What is the mechanism for implementing polymorphism in Java?
The reference variable defined by the parent class or interface can point to the subclass or the instance object of the concrete implementation class, while the method called by the program is dynamically bound at run time, that is, the method that refers to the specific instance object pointed to by the variable, that is, the method of the object that is running in memory, rather than the method defined in the type of the variable.
What is the grammatical difference between abstractclass and interface?
1. Abstract classes can have constructors, and interfaces cannot have constructors.
two。 There can be ordinary member variables in an abstract class, and there are no ordinary member variables in the interface.
3. An abstract class can contain non-abstract ordinary methods, and all methods in an interface must be abstract, and there can be no non-abstract ordinary methods.
4. The access types of abstract methods in an abstract class can be public,protected and (the default type, although
No errors are reported under eclipse, but the abstract methods in the interface can only be of type public, and the default is the type public abstract.
5. Abstract classes can contain static methods, and interfaces cannot contain static methods
6. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but the variables defined in the interface can only be publicstatic final types, and the default is publicstatic final type.
7. A class can implement multiple interfaces, but can only inherit one abstract class.
18. Can the method of abstract be static, native or synchronized at the same time?
Abstract's method cannot be static's, because abstract methods are implemented by subclasses, and static has nothing to do with subclasses!
The native method indicates that the method should be implemented in another platform-dependent programming language, and there is no problem with the implementation of quilt subclasses, so it cannot be abstract and cannot be mixed with abstract. For example, the FileOutputSteam class has to deal with hardware, and the underlying implementation uses the operating system-related api implementation; for example, in windows, it is implemented in C language, so if you look at the source code of jdk, you can see that the definition of the open method of FileOutputStream is as follows:
Private native void open (Stringname) throwsFileNotFoundException
If we want to use java to call c language functions written by others, we cannot call them directly. We need to write a c language function according to the requirements of java, and our c language function will call other people's c language functions. Since our c language function is written according to the requirements of java, our c language function can be docked with java. The docking method on the java side is to define the method corresponding to our c function. The corresponding method in java does not need to write specific code, but needs to declare native before.
I don't think it's possible to use synchronized with abstract, because I've never seen such a situation in my years of study and development, and I think synchronized should only make sense in a specific way. Moreover, the synchronization lock object used for synchronized synchronization on methods is this, and there is no way to determine what this is on abstract methods.
19. Can an inner class refer to a member of its containing class? Are there any restrictions?
Absolutely. 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 ordinary member variables of the external class, but only the static members of the external 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 these two lines of code are executed, has the content in the original String object changed?
No. Because String is designed to be an immutable class, all of its objects are immutable. In this code, s originally points to a String object with the content "Hello", and then we do a + operation on s, so does the object that s points to change? The answer is no. At this point, s no longer points to the original object, but points to another String object with the content "Hello world!". The original object still exists in memory, but the reference variable s no longer points to it.
From the above, it is easy to draw another conclusion that using String to represent a string will incur a lot of memory overhead if you often make various changes to the string, that is to say, unforeseen changes. Because the String object cannot be changed after it is created, a String object is needed to represent each different string. At this point, you should consider using the StringBuffer class, which allows modification instead of generating a new object for each different string. Moreover, the conversion between these two kinds of objects is very easy.
At the same time, we also know that if you want to use a string with the same content, you don't have to new one String at a time. For example, to initialize a String reference variable called s in the constructor and set it to the initial value, you should do this:
Public class Demo {
Private String s
...
Public Demo {
S = "Initial Value"
}
...
}
Instead of
S = new String ("Initial Value")
The latter calls the constructor every time to generate a new object, which is slow in performance, high in memory overhead, and meaningless, because the String object is immutable, so for a string with the same content, it only needs a String object to represent it. That is, the above constructor is called multiple times to create multiple objects, and their String type properties s all point to the same object.
The above conclusion is also based on the fact that for string constants, Java believes that they represent the same String object if the contents are the same. Calling the constructor with the keyword new always creates a new object, regardless of whether the content is the same or not.
Why the String class is designed to be immutable is determined by its purpose. In fact, not only String, the classes in many Java standard class libraries are immutable. When developing a system, we sometimes need to design immutable classes to pass a set of related values, which is also the embodiment of object-oriented thinking. Immutable classes have some advantages, for example, because its objects are read-only, there is no problem with multithreaded concurrent access. Of course, there are some disadvantages, such as each different state is represented by an object, which may cause performance problems. So the Java standard class library also provides a variable version, StringBuffer.
At this point, I believe you have a deeper understanding of "what are the interview questions of Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.