Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic interview questions for Java?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the basic interview questions in Java". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Can a call to a non-static method be made from within a static method?

You can't. Because non-static methods are to be associated with objects, an object must be created before method calls can be made on that object, while static methods do not need to create objects when called, and can be called directly. 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 non-static method is associated with which object? This logic does not hold, so a static method internally issues calls to non-static methods.

Difference between Integer and int

int is one of eight primitive data types provided by java. Java provides wrapper classes for each primitive type, Integer is the wrapper class java provides for int. The default value of int is 0, while the default value of Integer is null, that is, Integer can distinguish between unassigned and 0, int cannot express unassigned cases.

For example, to express the difference between not taking an exam and a score of 0, you can only use Integer. In JSP development, the default value of Integer is null, so when displayed in a text box with an el expression, the value is a blank string, while the default value of int is 0, so when displayed in a text box with an el expression, the result is 0, so int is not suitable as a type of web layer form data.

In Hibernate, if OID is defined as an Integer, Hibernate can determine whether an object is temporary based on whether its value is null. If OID is defined as an int, you also need to set its unsaved-value attribute to 0 in the hbm mapping file.

In addition, Integer provides a number of integer-related operations, such as converting a string to an integer, and Integer also defines constants that represent the maximum and minimum values of integers.

How much is Math.round(11.5)? Math.round(-11.5) equals what?

The Math class provides three rounding-related methods: ceiling, floor, and round, which correspond to the meaning of their English names.

ceiling ceiling The result of (11.3) is 12, Math. ceiling (-11.3) The result is-11;floor means floor, which means rounding down, Math. ceiling (11.6) results in 11, Math. ceiling (-11.6) results in-12; the hardest to master is the round method, which means "rounding" and the algorithm is Math.floor(x+0.5), which adds 0.5 to the original number and rounds down, so Math.round(11.5) results in 12, Math.round(-11.5) results in-11.

What is the difference between override and Override? Can Overloaded methods change the type of return value?

Overload means overload, override means overwrite.

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 are different (that is, the number or type of parameters are different).

Overriding means that the method in the subclass can have exactly the same name and parameters as a method in the parent class. When calling this method through an instance object created by the subclass, the defined method in the subclass will be called. This is equivalent to overriding the exactly the same method defined in the parent class. This is also a manifestation of polymorphism in object-oriented programming. Subclasses can override methods of their parent class only by throwing fewer exceptions than their parent class, or by throwing child exceptions of exceptions thrown by their parent class, because they can solve some problems of their parent class and cannot have more problems than their parent class. Subclass methods can only have greater access rights than their parent class, not less. If the method of the parent class is of type private, then the subclass has no override restrictions, which is equivalent to adding a completely new method to the subclass.

As for whether Overloaded methods can change the type of return value, it depends on what you want to ask. The title is vague. If several Overloaded methods have different parameter lists, their returner types can of course be different. But I guess the question you want to ask is: If the parameter lists of two methods are exactly the same, can you overload Overload by making their return values different? This is not possible, we can use the argument to explain this problem, because we sometimes call a method can also not define the return result variable, that is, do not care about its return result, for example, we call map.remove(key) method, although the remove method has a return value, but we usually do not define the variable to receive the return result, then assume that there are two methods in the class with exactly the same name and parameter list, only the return type is different, Java can't determine which method the programmer wants to call because it can't tell by the return type.

Override can be translated as overriding, literally knowing that it overrides a method and overrides it to achieve a different effect. The most familiar overlay to us is the implementation of interface methods, which are generally only declared in the interface, and we need to implement all the methods declared by the interface when implementing them. In addition to this typical usage, we may also override methods in parent classes in a subclass in inheritance. The following points should be noted in coverage:

1. The flag of the covered method must match the flag of the covered method completely in order to achieve the effect of coverage.

The return value of the overridden method must be consistent with the return value of the overridden method.

The exception thrown by the overriding method must be consistent with the exception thrown by the overridden method, or a subclass thereof;

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

Overload may be familiar to us and can be translated as overload. It means that we can define some methods with the same name, distinguish these methods by defining different input parameters, and then call them again. VM will choose the appropriate method to execute according to different parameter styles. In the use of overload to note the following points:

1. When overloading is used, different parameter styles can only be adopted. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types in the same method must be different, for example, fun(int,float), but not fun(int,int));

2, can not be overloaded by access permissions, return types, thrown exceptions;

The exception type and number of methods do not affect overloading.

4. For inheritance, if a method has priavte access rights in the parent class, it cannot be overloaded in the child class. If it is defined, it only defines a new method and will not achieve the effect of overloading.

15. Can the interface inherit the interface? Can abstract classes implement interfaces? Can abstract classes inherit concrete classes? Can an abstract class have a static main method?

Interfaces can inherit interfaces. Abstract classes can implement interfaces, and abstract classes can inherit concrete classes. Abstract classes can have static main methods.

Note: once you understand the nature and role of interfaces and abstract classes, these questions are easy to answer. Think about it. If you were the designer of java language, would you provide such support? If not, what is the reason? If you have no reason not to provide it, the answer is yes.

Just remember that the only difference between abstract classes and ordinary classes is that you cannot create instance objects and you allow abstract methods.

What is the mechanism for polymorphism in Java?

By virtue of the parent class or interface definition of reference variables can point to subclasses or concrete implementation of the class instance object, and the program calls the method at runtime only dynamic binding, that is, the reference variable points to the concrete instance object method, that is, the method of the object running in memory, rather than the reference variable type defined in the method.

What is the difference between abstract class and interface syntax?

1. Abstract classes can have constructors, interfaces cannot have constructors.

2. Abstract classes can have ordinary member variables, interfaces have no ordinary member variables

3. Abstract classes can contain non-abstract ordinary methods, all methods in the interface must be abstract, there can be no non-abstract ordinary methods.

4. Access types for abstract methods in abstract classes can be public, protected, and (default type, though

Eclipse does not report errors, but it should not work), but abstract methods in interfaces can only be public types, and the default is public abstract type.

5. Abstract classes can contain static methods, 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 variables defined in interfaces can only be publicstatic final types, and the default is publicstatic final.

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

Can abstract methods be static at the same time, native at the same time, synchronized at the same time?

Abstract methods cannot be static, because abstract methods are implemented by subclasses, and static has nothing to do with subclasses!

Native method means that the method is implemented in another platform-dependent programming language, and there is no problem of sub-class implementation, so it cannot be abstract and cannot be mixed with abstract. For example, FileOutputSteam class to deal with hardware, the underlying implementation is the operating system related api implementation; for example, in windows with c language implementation, so, look at the jdk source code, you can find FileOutputStream open method definition is as follows:

private native void open(Stringname) throwsFileNotFoundException;

If we want to use java to call the c language function written by others, we cannot call it directly. We need to write a c language function according to java requirements, and our c language function calls other people's c language functions. Since our C language function is written according to the requirements of Java, our C language function can interface with Java. The interface 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 it needs to declare native in front.

I don't think synchronized and abstract can be used together, because in my years of study and development, I have never seen such a situation, and I think synchronized should be meaningful only when it works on a specific method. Also, synchronized synchronization on methods uses this as the synchronization lock object, while abstract methods cannot determine what this is.

Can an inner class refer to members of its containing class? Are there any restrictions?

Absolutely. If it's not a static inner class, there's no limit!

If you treat static nested classes as a special case of inner classes, then in this case you cannot access ordinary member variables of outer classes, but only static members of outer classes, 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 of the original String object changed?

No. Because String is designed to be immutable, all of its objects are immutable. In this code, s originally points to a String object with the content "Hello," and then we perform a + operation on s. Does the object to which s points change? The answer is no. At this point, s does not point to the original object, but points to another String object with the content "Hello world!" "The object still exists in memory, but the reference variable s no longer points to it.

From the above description, it is easy to conclude that String representation incurs significant memory overhead if strings are subject to frequent, or unforeseen, modifications. Because String objects cannot be changed after they are created, a String object is needed to represent each different string. In this case, consider using the StringBuffer class, which allows modification rather than generating a new object for each different string. Moreover, the conversion between these two types of objects is very easy.

At the same time, we also know that if we want to use strings with the same content, we don't have to new a String every time. For example, if we want to initialize a String reference variable named s in the constructor, set it to its initial value, we should do this:

public class Demo {

private String s;

...

public Demo {

s = "Initial Value";

}

...

}

rather than

s = new String("Initial Value");

The latter will call the constructor every time, generating a new object, low performance and memory overhead, and meaningless, because String objects cannot be changed, so for the same content string, as long as a String object to represent it. That is, multiple calls to the constructor above create multiple objects, all of which point to the same object with the String type attribute s.

The above conclusion is also based on the fact that for string constants, Java considers them to represent the same String object if they have the same content. Calling the constructor with the keyword new always creates a new object, regardless of whether the content is the same or not.

As for why the String class is designed to be immutable, it is determined by its purpose. In fact, not only String, but many classes in Java standard libraries are immutable. When developing a system, we sometimes need to design immutable classes to pass a set of related values, which is also an embodiment of object-oriented thinking. Immutable classes have some advantages, such as because their objects are read-only, there are no problems with multithreaded concurrent access. There are some drawbacks, such as requiring an object to represent each of the different states, which can cause performance problems. So the Java standard library also provides a variant, StringBuffer.

"Java basic interview questions what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report