Java basic questions (JVM), java basic questions jvm

Source: Internet
Author: User

Java basic questions (JVM), java basic questions jvm

1. What is a Java Virtual Machine? Why is Java called a platform-independent programming language "?

A Java virtual machine is a virtual machine process that can execute Java bytecode. Java source files are compiled into bytecode files executed by Java virtual machines.

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

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

2. What is the difference between JDK and JRE?

JDK: java development kit, including JRE, compiler, and other tools (such as javaDOc and java debugger)

JRE: java Runtime Environment, including the core class libraries required by java virtual machines and java programs.

If you only want to run the java program, you only need to install JRE. If you want to write and run the java program, you need JDK.

3. What does the keyword "static" mean? Can Java override a private or static method?

If the variable or method of a class has static modification before it, it indicates that this method or variable belongs to this class, that is, it can be directly used without creating an object.

When the method of the parent class is private, it indicates that this method is private to the parent class and is invisible to any other class. Therefore, if the child class has the same method as the parent class, this is equivalent to a new private method for the subclass. If you want to perform an upward transformation and then call this "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 during compilation and belong to classes, while overwriting is dynamically bound during runtime (dynamically bound polymorphism), so they cannot be overwritten.

4. What are the basic data types supported by Java? What is an Automatic Disassembly box?

Java supports nine basic data types: byte, shot, int, long, float, double, char, boolean, void.

The Automatic Disassembly box is referenced by java from jdk1.5. The purpose is to automatically replace the original type with the corresponding object. You can also reverse the disassembly box. This also reflects the purpose of java to make everything an object.

The so-called automatic packing is to automatically convert the original type to the corresponding object, and the unboxing is to convert the object type to the basic type. The Automatic Disassembly box in java usually occurs in the variable assignment process, for example:

Integer object = 3; // int o = object in automatic packing; // in java, note the automatic packing mechanism, as a result, many objects are created, which puts pressure on platforms with small memory.

What is overwriting and overloading?

Overwrite is also called overwrite. It occurs between the subclass and parent class, indicating that the method in the subclass can have the same name and parameter as a method in the parent class, when this method is called by the instance object created by the subclass, the definition method in the subclass is called, which is equivalent to overwriting the identical method defined in the parent class, this is also a manifestation of object-oriented programming polymorphism.

Overload means that a class can have multiple methods with the same name, but their parameter lists have different numbers or types. When this method is called, call the corresponding parameter list method based on the passed parameter type. If the parameter list is the same but the return value is different, a compilation error occurs. This is not a heavy load, because jvm cannot determine which method to call based on the return value type.

5. Does Java support multi-inheritance? If not, how can this problem be achieved?

In java, a class can only inherit one parent class.

There are two methods to implement multi-inheritance in java. One is interface, but internal class.

// Implement multiple interfaces. If the variables of the two interfaces are the same, an error occurred while calling the variable. 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 () {}} // The internal class indirectly inherits multiple class Child {class Father {private void strong () {System. out. p Rintln ("parent class") ;}} 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 transfer and reference transfer? In java, Is there value transfer or reference transfer?

Value Transfer means that when a method is called, the real parameter is a copy of itself and assigned to the form parameter. In the method, the modification to the value of this parameter does not affect the original real parameter, A common example is the example of the exchange method when I first learned the C language.

When a method is called, the real parameter passes its own address to the form parameter. The change in the value of this parameter in the method is the actual operation of this real parameter.

In java, there is only one transmission method, that is, value transfer. What is confusing is that when an object in java is passed, changes to the form parameter will still be intended to the content of the object.

The following example illustrates the value transfer 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. There is no problem with this. It may be the reference transfer that we usually understand. Of course, it will change the content of StringBuffer. However, if the above comment is removed, the output will be: hello. at this time, the sb value is not changed to ha hello. if it is passed by reference, the s parameter is the address of sb. In this case, the new StringBuffer () in the method is assigned to s, that is to say, s points to the newly created object. according to the argument passed by reference, the change to s is the operation on sb, that is, sb should also point to the newly created object, and the output result should be ha world. but in fact, the output is only hello. this indicates that sb points to the original object, and the form parameter s points to the created object, which verifies that object passing in java is also a value passing.

7. What is the difference between interfaces and abstract classes?

The difference is:

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

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

Class to implement an interface, it must implement all methods declared by the interface. However, classes do not implement all methods declared in abstract classes. In this case, classes must be declared abstract.

Abstract classes can implement interfaces without interface methods.

The variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.

The member functions in the Java interface are public by default. Abstract class member functions can be private, protected, or public.

Interfaces are absolutely abstract and cannot be instantiated (java 8 supports implementing the default method in interfaces ). Abstract classes cannot be instantiated, but can be called if they contain the main method.

8. Can constructor be overwritten )?

Constructor cannot be overwritten by the quilt class, but the constructor can be overloaded. That is to say, a class can have multiple constructor methods.

9. What is the value of Math. round (11.5? What is Math. round (-11.5) equal?

Math. round (11.5) = 12 Math. round (-11.5) =-11 the round method returns a long integer closest to the parameter. After the parameter is added to 1/2, ask for its floor.

10. Differences between String and StringBuffer StringBuilder.

The tring length is unchangeable;

The length of StringBuffer is variable. If you operate the content in the String frequently, especially when the content is to be modified, use StringBuffer. If you need to> String at last, use the toString () of StringBuffer () method; thread security;

StringBuilder is the equivalent class used by a single thread for the StringBuffer class starting from JDK 5. The StringBuilder class should be used first because> all the same operations are supported for it, but because it does not execute synchronization, It is faster.

Be careful when using strings. If a String needs to be changed frequently, do not use strings. Otherwise, many useless objects will be created.

Let's 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 for splicing. It is reasonable to say that multiple objects will be generated, but jvm will optimize this, that is to say, only one object is created, and its execution speed is faster than that of StringBuffer splicing. let's look at the following:

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.

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.