Core Java Questions and Answers (1-33), answers1-33

Source: Internet
Author: User
Tags define abstract stream api multiple inheritance in java

Core Java Questions and Answers (1-33), answers1-33
Preface

Http://www.journaldev.com/2366/core-java-interview-questions-and-answers.

What are the important features of Java 8?

Java 8 was released in March 2014, which is very common in Java interviews. If you can answer this question clearly, you are not out and would like to learn the latest technologies. Java 8 is the biggest change made after Java 5 annotations and generics. The main new features are as follows:

1. The interface supports static and default methods.

2. functional interfaces and Lambda expressions

3. Stream API provided for the Collection class

4. Java date and time API

We strongly recommend that you open the above link to gain a deeper understanding of this knowledge.

How do you understand Java's platform independence?

Platform independence means that you can run the same Java program on any operating system. You can write a Java program on a Windows operating system and then run it on a Mac system.

What is JVM? Is it platform independent?

Java Virtual Machine (JVM) is the core of the Java programming language. JVM is responsible for converting bytecode into machine-readable code. JVM does not have platform independence, so different operating systems have different JVMs. We can customize some JVM parameters, such as configuring the maximum memory and minimum memory for JVM. JVM is called a virtual machine because it provides an interface that does not depend on the underlying operating system.

Differences between JDK and JVM

Java Development Kit (JDK) is mainly for Development purposes, while JVM is a part of JDK and is used to execute Java programs.

JDK provides a full set of tools, including compiling, debugging, and executing Java programs. The execution part is the responsibility of JVM, and JVM ensures the independence of Java platform.

Regions of JVM and JRE

Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM (Java Virtual Machine) and Java core class library, which can ensure the successful running of any Java program.

However, JRE does not contain any development tools, such as Java compilation and debugging.

If you only want to run the Java program, install JRE.

In Java, which class is the superclass of all classes?

Java. lang. Object is the root class of all classes, and we do not need to inherit it from the display.

Why does Java not support multiple inheritance?

Java does not support multiple inheritance because of the "diamond Inheritance Problem" (brick-and-stone problem). For example, assume that classes A and B both implement A method in SuperClass, this will cause the compiler to produce ambiguity when compiling Class C, and it does not know which method to choose. For more information, see Multiple Inheritance in Java.

Although Java classes do not support multi-inheritance, interfaces can be inherited in multiple ways. An interface can inherit multiple classes because interfaces only declare methods. The specific implementation is defined by implementation classes, therefore, there is no "diamond Inheritance Problem" for interfaces.

Why is Java not a fully object-oriented language?

Java is not considered a fully object-oriented language because it supports basic data types, such as int, short, and long. These basic data types facilitate Java program development. Obviously, Java can encapsulate these basic types into objects, but if they are used only for representation, they do not bring too many benefits. (It is said that the basic data type does not require garbage collection, so it has another performance advantage over the object type)

As we all know, Java provides corresponding encapsulation classes for all basic data types, such as Integer and Long. They provide additional methods.

Differences between the path and classpath Variables

PATH is an environment variable used by the operating system to locate the executable program. Therefore, when we install Java or want to locate any executable program by the operating system, you must add the PATH where the executable program is located to the system PATH variable.

Classpath is for Java and is used to search for dependent class files during runtime. classpath can be a directory, zip file, and jar file.

Main method in Java

The main () method is the entry of the java program. The syntax of the main () method is as follows:

public static void main(String args[]).

The main method is a public static method, so java can directly access this method without instantiating this class. The input parameter of the main method is a string array, with this array, we can pass in runtime parameters to the java program.

What is Java's overload and coverage?

When we have multiple cognominal methods with different input parameters, this is called method overload;

Override is related to inheritance. When the parent class and subclass have the same method (method name and parameter) called method rewriting, the @ Override annotation is usually added before the subclass method, make sure that the subclass is also modified when the parent class method is modified.

Can I reload the main method?

The answer is yes. A class can have multiple methods named "main". However, when we run this class, the java Runtime Environment will only find the methods that conform to the feature syntax (public static void main(String args[]).

Can multiple public classes be defined in a Java source file?

The answer is no. There can only be one public class in the java source file, but multiple non-public classes can be defined.

What is a Java package and which package is introduced by default

Java packages are mainly used to organize and group Java classes by function or module. The complete name of a Java class contains the package name and class name, as shown in figurejava.lang.ObjectClass, located injava.langUnder the package, the class name is Object.

java.langIs introduced by default. We do not need to explicitly introduce any classes under this package.

What is an access controller?

The access control operators in Java include public, private, and protected. If the preceding keyword is not used, the default access permission is granted.

Access controllers are mainly used to control access permissions.

A Java class can only be modified using public or not (default access permission ). For more information, visit Java Access Modifiers.

Keyword about final

Modifier class: the class modified by the final keyword cannot be inherited by other classes. For example, the String class is a final class and cannot be inherited by any class.

Modification Method: We can use the final keyword to modify the method to ensure that the method cannot be overwritten by the quilt class.

Modifier variable: The final keyword can be used to modify a variable to ensure that it can be assigned only once. However, you must note that the state of the variable can be changed. For example, if the variable is an object that uses final modification, the value of the member attribute of the object can be changed.

Interface variable: The interface variables in Java are modified using final and static by default.

Interface {

String str = "hello."; // final and static

}

Static keywords

Modify class variables: static can be used to modify the member attributes of a class and make it a global variable;

Modification Method: static can be used to modify the methods in the class, that is, static methods. static methods can only access static variables and call static methods;

For more details, visit java static keyword.

Keyword about finally and finalize

The finally keyword is used with try-catch to ensure that the finally statement block is always executed, even if an exception occurs within the try-catch statement block.

Finally statement blocks are often used to release resources created in try statement blocks.

Finalize () is a special method in the Object class. We can rewrite this method in our own class. This method will be called by the garbage collector when the object is recycled.

The finalize () method is often used to release system resources when the object is recycled.

Can I declare a static class?

We cannot declare a top-level class as a static class, but internal classes can be modified using static. Static internal classes are called static internal classes.

Static internal classes are similar to other top-level classes, mainly for convenient packaging.

For more information about internal classes, see java inner class.

What is static import?

When we want to use any static variables or methods, we usually pilot the class and reference its methods or variables based on the class name, as shown in the following example:

import java.lang.Math;//inside classdouble test = Math.PI * 5;

In fact, there is another way to use static import, that is, directly import the static variable:

import static java.lang.Math.PI;//no need to refer class nowdouble test = PI * 5;

However, the use of static imports may cause some confusion. Excessive use will make the program difficult to read and maintain, so it is best to avoid using it.

Try-with-resources statements

A new feature of Java 7 is to use the try-with-resources statement to automatically manage resources. Before Java 7, you can only explicitly call the method to close the resource, usually in the finally statement block. When we forget to close the resource, memory leakage is often caused.

From Java 7, we can create resources in the try code block. When the try code block is executed, the resources are automatically disabled.

For more information, see Java Automatic Resource Management.

Multi-catch code block

One improvement of Java 7 is the introduction of multi-catch code blocks. We can use a catch Block to catch multiple exceptions. When the code of each catch block is similar, this will make our code simpler and clearer.

If a catch Block processes multiple exceptions, you can use | to separate them. In this case, the exception parameters are of the final type, so they cannot be modified.

For example:

Before Java 7:

catch (IOException ex) {     logger.error(ex);     throw new MyException(ex.getMessage());catch (SQLException ex) {     logger.error(ex);     throw new MyException(ex.getMessage());}catch (Exception ex) {     logger.error(ex);     throw new MyException(ex.getMessage());}

After Java 7:

catch(IOException | SQLException | Exception ex){     logger.error(ex);     throw new MyException(ex.getMessage());}

For more information, see Java multi catch block.

Static code blocks

A Java static code block is a set of code statements that are executed when the loader loads these classes into the memory. It can be used to initialize static variables and is often used to create static resources (when the class is loaded );

What is an interface?

Interfaces are the core content of Java programming language. They are widely used in JDK and also in design patterns and various frameworks and tools. The interface implements a method to abstract in Java, which can be used to define abstract conventions for subclass implementation.

It is very beneficial to use the interface definition type as a top-level interface. In addition, since a Java class can implement multiple interfaces, it is best to use interfaces as super parent classes in most cases.

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.