"Translated" Core Java Questions and Answers "1-33"

Source: Internet
Author: User

Objective

Link: 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, and this piece of content is very common in Java interviews. If you can answer this question clearly, it means that you aren't out and like to learn the latest technology. Java 8 is the biggest change following the annotations and generics of Java 5, and the main new features are as follows:

1. Interface supports static method and default method

2. Functional interfaces and lambda expressions

3. Stream API for the collection class

4. Java Date Time API

It is strongly recommended that you open the links above to gain a deeper understanding of this knowledge.

How do you understand the platform independence of Java

Platform independence means that you can run the same Java program on any operating system, you can write Java programs on the Windows operating system, and then run on Mac systems.

What is the JVM, and whether it is platform independent

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

The difference between JDK and JVM

Java Development Kit (JDK) is primarily for development purposes, and the JVM is part of the JDK for executing Java programs.

The JDK provides a complete set of tools, including compiling, debugging, and executing Java programs, where the execution is part of the JVM's responsibility, and the JVM guarantees the platform independence of Java.

Areas of the JVM and JRE

Java Runtime Environment (JRE) is the implementation of the JVM, which consists of the JVM (Java Virtual machine) and the Java Core Class library, which ensures that any Java program runs successfully.

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

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

Which class in Java is a superclass of all classes

Java.lang.Object is the root class of all classes, and we do not need to show the inheritance of it.

Why Java does not support multiple inheritance

Java does not support multiple inheritance because of the "Diamond Inheritance Problem" (masonry problem), such as: Assuming that both classes A and B implement a method in superclass, it will cause the compiler to compile Class C when the ambiguity, do not know which method to choose. For more information, read multiple inheritance in Java.

Although the Java class does not support multiple inheritance, the interface can be multiple inheritance, an interface can inherit more than one class because the interface is merely a declaration method, the implementation is defined by the implementation class, so there is no "diamond inheritance Problem" for the interface.

Why Java is not a fully object-oriented language

Java is considered not to be a fully object-oriented language because it supports basic data types such as int, short, long, and so on. These basic data types have facilitated the development of Java programs, and it is clear that Java is able to encapsulate these basic types as objects, but it does not bring too much benefit if it is used only for presentation. (It is said that the basic data types do not require garbage collection, so there is another performance advantage over the object type)

As we all know, for all the basic data types, Java provides the corresponding encapsulation classes, such as Integer, Long, and so on, they provide some additional methods.

The difference between path and CLASSPATH variable

Path is an environment variable used by the operating system to locate the executable program, so when we install Java or want any executable program to be found by the operating system, we need to add the path to the system path variable where the executable program is located.

Classpath is for Java, for Java programs to find dependent class files at run time, Classpath can be a directory, zip file, jar file, and so on.

The Main method in Java

The main () method is the entry for the Java program, and the syntax for 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 the method without instantiating the class, and the input parameter of the main method is an array of strings through which we can pass the runtime parameters to the Java program.

What are the overloads and overrides of Java

When we have multiple methods with duplicate names and the parameters of these methods are not the same, this is called method overloading;

Overrides are related to inheritance, when the parent class and subclass have an identical method (method name and parameter), called method overrides, typically add @override annotations before the subclass method, ensuring that when the parent class method is modified, the subclasses are also modified together.

Whether the main method can be overloaded

The answer is yes, a class can have multiple methods with the name "main", but when we run the class, the Java runtime only looks for the main method that conforms to the attribute syntax ( public static void main(String args[]) ).

Whether multiple public classes can be defined in a Java source file

The answer is no, there can be only one public class in the Java source file, but we can define multiple non-public classes.

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

Java packages are primarily used to organize and group Java classes and can be grouped by function or module. The full name of a Java class contains the package name and class name, such as java.lang.Object class, java.lang under the package, and the class name is object.

java.langis introduced by default, we do not need to explicitly introduce any class under the package.

What is an access control character

The access control characters in Java are public, private, protected, or default access if the above keyword decoration is not used.

The access control is primarily used for control of access rights.

A Java class can only be decorated with public or not decorated (default access rights). For more details, go to Java access Modifiers.

About the final keyword

Modified class: A class that is decorated with the final keyword cannot be inherited by another class, such as a final class, which 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 overridden by the quilt class.

Modifier variables: The final keyword can be used to modify variables to ensure that they can only be assigned once, but it is important to note that the state of the variable can be changed, for example, if the variable is an object, that is, the value of the member property of the object can be changed by using the final decoration.

Interface variables: interface Variables in Java are decorated by default with final and static.

Interface a{

String str= "Hello."; //final and Static

}

About the Static keyword

Modifier class variable: Static can be used to modify the member properties in the class, making it a global variable;

Modification method: Static can be used to modify the method in the class, that is, the statically method, static method can only access static variables, call static methods;

For more details, please visit the Java static keyword.

About the finally and Finalize keywords

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

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

Finalize () is a special method in the object class that we can override in our own class. This method is called by the garbage collector when the object is reclaimed.

The Finalize () method is often used to release system resources when the object is reclaimed.

Whether a static class can be declared

We cannot declare a top-level class as a static class, but an inner class can be decorated with static. The inner class that is modified by static is called a static inner class .

static inner classes are similar to other top-level classes, primarily for easy packaging.

For more in-house content, read the Java inner class.

What is static import

When we want to use any static variable or method, it is often the case that the class is piloted, and then its methods or variables are referenced according to the class name, as in the following example:

Import Java.lang.Math; // Inside Class double test = Math.PI * 5;

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

Import Static Java.lang.Math.PI; // no need to refer class now double test = PI * 5;

However, using static imports can be confusing and overuse can make programs difficult to read and maintain, so it's best to avoid them.

About Try-with-resources Statements

A new feature of Java 7 is the automatic management of resources using the Try-with-resources statement. Prior to Java 7, for resource shutdown, only explicit calls to close resource methods, usually closed in the finally statement block, often caused a memory leak when we forgot to close the resource.

Starting with Java 7, we can create a resource in a try block, and the resource shuts down automatically when the try code block is executed.

For more information on this, read the Java Automatic Resource Management.

About Multi-Catch code blocks

One of the improvements in Java 7 is the introduction of the Multi-Catch code block, which allows us to capture multiple exceptions using a catch block, which makes our code shorter and clearer when each catch block code is similar.

If a catch block handles multiple exceptions, you can use | To separate them, in which case the exception parameter is the final type and cannot be modified.

As an 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, please read Java Multi catch block.

About static code blocks

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

What is an interface

Interfaces are the core content of the Java programming language and are widely used in the JDK, as well as in design patterns, frameworks, and tools. The interface implements a method in Java for abstract purposes, which can be used to define some abstract conventions for subclasses to implement.

It is advantageous to design using an interface definition type as a top level interface. In addition, because a Java class can implement multiple interfaces, in most cases it is preferable to use an interface as a super-parent class.

For more information on interfaces, read the Java interface.

What is abstract class

Abstract classes are used to create default method implementations for subclasses, and an abstract class can contain an abstract method that does not have a body, or it can contain an implemented method.

The abstract keyword is used to create an abstract class that cannot be instantiated, and is typically used to allow subclasses to inherit, implement abstract methods defined in an abstract class, overwrite, or use an implemented method in an abstract class.

For more information on abstract classes, read the Java abstract class.

The difference between an abstract class and an interface

Abstract class is defined by the abstract keyword, and the interface is defined with the interface keyword;

Abstract classes can have implementation methods, interfaces do not;

A class can inherit only one abstract class, but may inherit multiple interfaces;

We can run an abstract class that contains the main method, but the interface is not;

For more information on the differences between interfaces and abstract classes, please read difference between abstract class and Interface.

Whether the interface can implement or inherit other interfaces

Interfaces cannot implement interfaces, but can inherit other interfaces.

Because the interface cannot contain the concrete implementation method, so they do not have "masonry problem", which is why the interface can be multiple inheritance reasons;

What is an Identity interface

An identity interface is an empty interface that does not contain any methods and is used only to identify certain features of the implementation class. A typical example is the serializable and cloneable interfaces, which identify can be serialized and cloned.

What is a wrapper class

The wrapper class for Java is a class that is encapsulated after 8 basic data types, and all wrapper classes are final decorated and immutable.

The automatic unboxing feature provided by Java 5 allows the basic data types and wrapper classes to be automatically converted.

For more information on packaging classes, read wrapper classes in Java.

About enumerations in Java

Enumeration is a new type introduced in Java 5, and its fields contain several fixed constants, for example, we can create a direction enumeration that contains four fixed fields east, WEST, North, south.

The enum keyword is used to create an enumeration type, which is a bit similar to class. In addition, enumeration constants are static and final.

For more information about enumerations, read Java enum.

About Java annotations

Java annotations provide code-related information that does not directly affect your code. Annotations are introduced in Java 5.

Annotations are metadata embedded in a program that can be parsed by annotation parsing tools or compilers.

We can also specify whether annotations are available at compile time or run time.

The annotations built into Java are @override, @Deprecated and @suppresswarnings

For more information on annotations, read the Java annotations.

About the Java reflection mechanism

The Java Reflection API helps us to observe and modify the runtime behavior of Java programs, and through the reflection mechanism, we can get a Java class, interface, or enumeration method and property information. The reflection API is a high-level feature of Java, which is generally not recommended in normal programming unless it is a special case. The use of the reflection API destroys some design patterns, such as the common singleton pattern, where private construction methods can be called through reflection, violating the rules of the access modifier.

Although we seldom use the reflection API in our usual programming, the existence of the reflection mechanism is very important. Now, basically any framework is inseparable from the reflection API, such as Common spring, Hibernate, or Tomcat.

For more details, read the Java Reflection Tutorial.

About the combinations in Java

Composition is a design technique for implementing has-a relationships between classes, and we can use object combinations to implement code reuse.

Java uses object reference variables for composition, and the advantage of using combinations is that we can control the visibility of other objects to the client class and only reuse the code that we need to reuse.

Read the Java composition for more information on Java composition.

Too many, to be continued ...

Translate Core Java Questions and Answers "1-33"

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.