Dedicated to 2018 still struggling Java programmer, Java common face questions and answers!

Source: Internet
Author: User
Tags multiple inheritance in java

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

A 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 arbitrary platforms without requiring programmers to rewrite or recompile each platform individually.

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 with JRE, compiler and other tools (e.g. JavaDOc, Java Debugger)

The Jre:java runtime environment, which contains the core class libraries required for Java virtual machines and Java programs.

If you just want to run Java programs, then just install the JRE, if you want to write Java programs and run, then you need the JDK.

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

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

When the method of the parent class is private, it indicates that the method is privately owned by the parent class and is not visible to any other class, so if the subclass has a method that is the same as the parent class, this is equivalent to a new private method for the subclass, and if you want to make an upward transformation and then call the "Overwrite method", A compilation error is generated

Class Parent {private fun () {...}} Class Child extends the Parent {private fun () {...}} Class Test {public static void main (string[] args) {Parent c = new Child (); C.fun ();//Compile Error}}

Static methods are statically bound at compile time, belong to classes, and overrides are dynamically bound at run time (dynamically bound polymorphic) and therefore cannot be overwritten.

What are the basic data types supported by 4.Java? What is auto-unboxing?

Java supports the following 9 types of basic data: Byte,shot,int,long,float,double,char,boolean,void.

Auto-Unboxing is a reference to Java from jdk1.5, which is designed to automatically reload the original type to the corresponding object, or it can be reversed, that is, unpacking. This also embodies the purpose of all objects in Java.

The so-called auto-boxing is the automatic conversion of the original type to the corresponding object, and the unboxing is to convert the object type to the base type. Automatic unboxing in Java typically occurs during variable assignment, such as:

Integer object = 3; Auto-boxing int o = object; Unpacking

In Java, you should pay attention to the automatic disassembly box, because sometimes because of the Java automatic boxing mechanism, which causes a lot of objects created, the memory of the platform can cause pressure.

What are overrides and overloads?

Overrides are also called overrides, which occur between subclasses and the parent class, which means that the method in the subclass can be exactly the same as the name and parameters of a method in the parent class, and when 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 representation of the polymorphism of object-oriented programming.

Overloading means that in a class, you can have multiple methods of the same name, but they have different number or type of argument lists, and when the method is called, the method that corresponds to the argument list is called based on the type of argument passed. When the argument list is the same but the return value is not the same, a compilation error will occur, which is not an overload because the JVM cannot determine which method to call based on the return value type.

Does 5.Java support multiple inheritance? If not, how do you implement it?

Is single-inheritance in Java, meaning that a class can inherit only one parent class.

There are two ways to implement multiple inheritance in Java, one is an interface, but an inner class.

Implement multiple interfaces if the variables of the two interface are the same then the compile error occurs when the variable is called 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 PU Blic void Fun1 () {}}//inner class indirectly inherits class Child {class Father {private void Strong () {SYSTEM.OUT.PRINTLN ("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 passing and reference passing? Is there a value pass or a reference pass in Java, or are there?

The value is passed in the method call, the argument is a copy of their own copies of the parameters, in the method, the value of the parameter modification does not affect the original argument, the common example is just beginning to learn C language when the Exchange method Example.

A reference pass is a method call that arguments its own address to a formal parameter, and the change in the value of that parameter within the method is the actual operation of the argument.

There is only one way to pass in Java, and that is to pass the value. It may be confusing that when an object in Java is passed, the change to the parameter will still be intentional to the object's content.

The following example shows that value is passed 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 the reference pass, then of course, will change the content of StringBuffer. But if you remove the above comment, it will output: hello. At this point the value of SB does not become HA hello. If it is a reference to pass, then the formal parameter s is the address of SB, at this time in the method new StringBuffer (), and assign the object to S, That is, s now points to the newly created object. According to the reference, the change in S is the operation of SB, that is to say that SB should also point to the newly created object, then the output should be the HA World. But actually the output is only hello. This shows that SB points to the original object, while the formal parameter s points to the created object, which verifies that the object passing in Java is also a value pass.

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

The difference is:

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

A class can implement many interfaces, but only one abstract class is inherited

Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class can not implement all methods of an abstract class declaration, 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.

The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.

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

The interface is absolutely abstract and cannot be instantiated (Java 8 already supports implementing the default method in the interface). An abstract class can also not be instantiated, but it can be called if it contains the main method.

8. Can the constructor (constructor) be rewritten (override)?

Construction methods cannot be overridden by quilts, but construction methods can be overloaded, meaning that a class can have more than one construction method.

9.math.round (11.5) equals how much? How much does Math.Round (-11.5) equal?

Math.Round (11.5) ==12 Math.Round ( -11.5) ==-11 round method returns the longest integer closest to the parameter, and the parameter adds 1/2 to its floor.

String, StringBuffer the difference between StringBuilder.

The length of the tring is immutable;

The length of the StringBuffer is variable, and if you are frequently manipulating the contents of a string, especially if the content is to be modified, then use StringBuffer, if >string is required, then use the StringBuffer toString () method; thread safety;

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

Use strings with special care, if you want to change a string frequently, you must not use string, otherwise you will create a lot of useless objects out.

Take a look at the comparison

String s = "Hello" + "world" + "I love You"; StringBuffer Sb = new StringBuilder ("Hello"). Append ("World"). Append ("I love You");

This time S has more than one string to splice, it is supposed to be a number of objects generated, but the JVM will be an optimization, that is, only create an object, at this time it executes faster than the stringbuffer stitching. 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 are created, resulting in a waste of memory space.

Java common face questions and answers 21-30, please see the next article, welcome to forward and attention.

1, with 1-5 of working experience, in the face of the current popular technology do not know where to start, the need to break through the technical bottleneck can be into the group learning.

2, in the company for a long time, have a very comfortable, but job-hopping interview wall. Need to study in a short period of time, job-hopping to get a high salary can go to group learning.

3, if there is no work experience, but the foundation is very solid, on the Java work mechanism, common design ideas, Common Java Development Framework Master Proficiency, you can enter the group learning.

4, feel that they are very cow B, the general needs can be done. However, the knowledge points are not systematic, it is difficult to continue to break through in the technical field can be into the group learning.

5. Ali Java Senior Architect live to explain the knowledge points, share knowledge, many years work experience combing and summary, with everyone comprehensive, scientific to establish their own technical system and technical knowledge!

Group number: 685167672

Dedicated to 2018 still struggling Java programmer, Java common face questions and answers!

Related Article

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.