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 questions and answers that Java often meets?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "Java frequent meeting test questions and answers". In daily operation, I believe many people have doubts about Java frequent meeting test questions and answers. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "Java frequent meeting test questions and answers". Next, please follow the editor to study!

1. What is a Java virtual machine? Why is Java called a "platform-independent programming language"?

The Java virtual machine is a virtual machine process that can execute Java bytecode. The Java source file is compiled into a bytecode file that can be executed by the Java virtual machine.

Java is designed to allow applications to run on any platform without requiring programmers to rewrite or recompile separately for each platform.

The Java virtual machine makes this possible because it knows the instruction length and other features of the underlying hardware platform.

What is the difference between 2.JDK and JRE?

JDK: java development kit that includes JRE, compilers, and other tools (such as javaDOc, java debugger)

JRE: java runtime environment that contains the core class libraries required by the java virtual machine and java programs.

If you just want to run java programs, you only need to install JRE, and if you want to write java programs and run them, you need JDK.

3. What does the keyword "static" mean? Is it possible to override a private or static method in Java?

If a class's variable or method is preceded by a static modifier, it indicates that the method or variable belongs to the class, that is, it can be used directly without creating an object

When the method of the parent class is modified by private, it indicates that the method is private to the parent class and is invisible to any other class, so if the subclass sets the same method as the parent class, it is equivalent to a new private method for the subclass, and if you want to transform upward and then call the "override method", a compilation error will occur.

Class Parent {private fun () {...}} class Child extends Parent {private fun () {...}} class Test {public static void main (String [] args) {Parent c = new Child (); c.fun (); / / compilation error}

Static methods are statically bound at compile time and belong to classes, while overrides are dynamically bound at run time (polymorphisms of dynamic binding), so they cannot be overridden.

What are the basic data types supported by 4.Java? What is automatic unpacking?

There are 9 basic data types supported by java: byte,shot,int,long,float,double,char,boolean,void.

Automatic unpacking is referenced by java from jdk1.5, the purpose is to automatically replace the original type with the corresponding object, or in reverse, that is, unpacking. This also reflects the tenet that everything in java is an object.

The so-called automatic boxing is to automatically convert the original type into the corresponding object, while unboxing is to convert the object type to the basic type. Automatic unpacking in java usually occurs during variable assignment, such as:

Integer object = 3; / / Auto-boxing int o = object; / / unpacking

In java, you should pay attention to automatic unboxing, because sometimes many objects may be created because of the java autoboxing mechanism, which can put pressure on platforms with low memory.

What are overrides and overloads?

Overwriting, also known as rewriting, occurs between the subclass and the parent class, indicating that the method in the 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 definition 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.

Overloading means that there can be multiple methods with the same name in a class, but the number or type of their parameter lists is different. when the method is called, the method of the corresponding parameter list is called according to the parameter type passed. A compilation error occurs when the parameter list is the same but the return values are different, which is not an overload, because jvm cannot determine which method should be called based on the return value type.

Does 5.Java support multiple inheritance? If it is not supported, how can it be implemented?

In java, it is single inherited, which means that a class can inherit only one parent class.

There are two ways to implement multi-inheritance in java, one is the interface, but the inner class.

/ / implement multiple interfaces if the variables of the two interfaces are the same, interface interface1 {static String field = "dd"; public void fun1 ();} interface interface2 {static String field = "dddd"; public void fun2 ();} class child implements interface1,interface2 {static String field = "dddd" @ Override public void fun2 () {} @ Override public void fun1 () {}} / / Inner class indirect multi-inheritance class Child {class Father {private void strong () {System.out.println ("parent");}} class Mother {public void getCute () {System.out.println ("mother");}} public void getStrong () {Father f = new Father () F.strong ();} public void getCute () {Mother m = new Mother (); m.getCute ();}} 6. What is value passing and reference passing? Is it value passing or reference passing in java, or both?

Value passing is that when a method is called, the argument assigns a copy of itself to the parameter. In the method, the modification of the parameter value does not affect the original argument. A common example is the example of the exchange method at the beginning of learning the c language.

Reference passing is when the argument passes its address to the parameter when the method is called, and the change to the parameter value within the method is the actual operation on the argument.

There is only one way of passing in java, and that is value passing. What may be confusing is that when an object in java is passed, changes to the parameter will still mean the content of the object.

The following example illustrates the value passing in java.

Public class Test {public static void main (String [] args) {StringBuffer sb = new StringBuffer ("hello"); getString (sb); System.out.println (sb);} public static void getString (StringBuffer s) {/ / s = new StringBuffer ("ha"); s.append ("world");}}

In the above example, the current output is: hello world. This is not a problem, it may be the usual understanding of reference passing, then of course it will change the content of StringBuffer. But if you remove the comments above, you will output: hello. At this point, the value of sb does not become ha hello. If the reference is passed, then the s of the formal parameter is the address of the sb, at this time in the method new StringBuffer (), and assign the object to s, that is to say, s now points to the newly created object. According to the reference pass, the change to s at this time is the operation on sb, that is, the sb should also point to the newly created object, so the output should be ha world. But in fact, the output is only hello. This shows that the sb points to the original object, while the parameter s points to the created object, which verifies that the object passing in java is also value passing.

7. What is the difference between an interface and an abstract class?

The differences are as follows:

All the methods in the interface are abstract. Abstract classes can contain both abstract and non-abstract methods.

Class can implement many interfaces, but can only inherit one abstract class

If a class is to implement an interface, it must implement all the methods declared by the interface. However, the class may not implement all the methods declared by the abstract class, and of course, in this case, the class must also be declared abstract.

An abstract class can implement an interface without providing an interface method implementation.

Variables declared in the Java interface are all final by default. Abstract classes can contain variables that are not final.

The member function in the Java interface is public by default. The member function of an abstract class can be private,protected or public.

Interfaces are absolutely abstract and cannot be instantiated (java 8 already supports the implementation of default methods in interfaces). An abstract class cannot be instantiated either, but it can be called if it contains a main method.

8. Can the constructor be rewritten (override)?

Constructors cannot be overridden by subclasses, but constructors can be overloaded, that is, a class can have multiple constructors.

How much is 9.Math.round (11.5)? How much is Math.round (- 11.5)?

Math.round (11.5) = = 12 Math.round (- 11.5) =-11 the round method returns the long integer closest to the parameter, and the parameter is added with 1 Math.round 2 to find its floor.

10. The difference between String and StringBuffer StringBuilder.

The length of tring is immutable.

The length of StringBuffer is variable. If you often operate on the contents of a string, especially when the content is to be modified, use StringBuffer, and if you finally need to > String, use the toString () method of StringBuffer; thread-safe

StringBuilder, starting with JDK 5, complements the StringBuffer class with an equivalent class used by a single thread; usually the StringBuilder class should be preferred because > supports all the same operations for it, but because it does not perform synchronization, it is faster.

Be very careful when using strings. If you want to change a string frequently, you must not use String, otherwise you will create a lot of useless objects.

Take a look at the comparison.

String s = "hello" + "world" + "i love you"; StringBuffer Sb = new StringBuilder ("hello"). Append ("world"). Append ("i love you")

At this time, s has multiple strings to concatenate, and in theory, multiple objects will be generated, but jvm will optimize this, that is, only one object will be created, and its execution speed is faster than StringBuffer stitching. And take a look at this:

String S2 = "hello"; String S3 = "world"; String S4 = "i love you"; String S1 = S2 + S3 + S4

In this case, three more objects will be created, resulting in a waste of memory space.

At this point, the study on "what are the Java test questions and answers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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