java核心面试

Core Java interview questions help you in preparing for java based interviews. Whether you are a fresher or highly experienced professional, core java plays a vital role in any Java/JEE interview.

核心Java面试问题可帮助您准备基于Java的面试。 无论您是新手还是经验丰富的专业人员,核心Java在任何Java / JEE面试中都起着至关重要的作用。

Core Java is the favorite area in most of the interviews and plays a crucial role in deciding the outcome of your interview.

在大多数访谈中,Core Java是最喜欢的领域,并且在决定访谈结果时起着至关重要的作用。

This post comes directly from my 14+ years of Java programming and lots of interviewing experience. Java 14 has been released recently and I have updated the post to include some of the questions from the latest releases.

这篇文章直接来自我14年以上的Java编程经验以及许多采访经验。 Java 14最近已发布,我更新了该帖子,以包含最新版本中的一些问题。

Java核心面试问题 (Core Java Interview Questions)

I have already written a lot about java interview questions for specific topics such as String, Collections, and Multithreading.

我已经针对特定主题(例如字符串,集合和多线程)写了很多有关Java面试问题的文章。

  1. Java String Interview Questions
    Java字符串面试问题
  2. Java Thread Interview Questions
    Java线程面试问题
  3. Java Collections Interview Questions
    Java Collections面试问题
  4. Java Exception Interview Questions
    Java异常面试问题

Here I am providing some of the important core java interview questions with answers that you should know. You can bookmark this post to brush up your knowledge before heading for an interview.

在这里,我提供一些重要的Java核心访谈问题,并提供您应该知道的答案。 您可以在此帖子上加上书签,以在开始面试之前增加自己的知识。

Here is the link that opens in a new tab: Core Java Quiz

这是在新选项卡中打开的链接: 核心Java测验

  1. Name some important features of Java 14 release?
    列举Java 14版本的一些重要功能吗?
  2. Name some important features of Java 13 release?
    列举Java 13版本的一些重要功能吗?
  3. Name some important features of Java 12 release?
    列举Java 12版本的一些重要功能吗?
  4. What are the important features of Java 11 release?
    Java 11版本的重要功能是什么?
  5. What are the important features of Java 10 release?
    Java 10版本的重要功能是什么?
  6. What are the important features of Java 9 release?
    Java 9版本的重要功能是什么?
  7. What are the important features of Java 8 release?
    Java 8版本的重要功能是什么?
  8. Name some OOPS Concepts in Java?
    用Java命名一些OOPS概念?
  9. What do you mean by platform independence of Java?
    Java平台独立性是什么意思?
  10. What is JVM and is it platform independent?
    什么是JVM,它与平台无关吗?
  11. What is the difference between JDK and JVM?
    JDK和JVM有什么区别?
  12. What is the difference between JVM and JRE?
    JVM和JRE有什么区别?
  13. Which class is the superclass of all classes?
    哪个类是所有类的超类?
  14. Why Java doesn’t support multiple inheritance?
    为什么Java不支持多重继承?
  15. Why Java is not pure Object Oriented language?
    为什么Java不是纯粹的面向对象语言?
  16. What is difference between path and classpath variables?
    path和classpath变量之间有什么区别?
  17. What is the importance of main method in Java?
    Java中main方法的重要性是什么?
  18. What is overloading and overriding in java?
    什么是Java中的重载和重载?
  19. Can we overload main method?
    我们可以重载主要方法吗?
  20. Can we have multiple public classes in a java source file?
    我们可以在一个Java源文件中有多个公共类吗?
  21. What is Java Package and which package is imported by default?
    什么是Java包,默认情况下会导入哪个包?
  22. What are access modifiers?
    什么是访问修饰符?
  23. What is final keyword?
    什么是最终关键字?
  24. What is static keyword?
    什么是静态关键字?
  25. What is finally and finalize in java?
    Java中的final和finalize是什么?
  26. Can we declare a class as static?
    我们可以将一个类声明为静态的吗?
  27. What is static import?
    什么是静态导入?
  28. What is try-with-resources in java?
    什么是Java中的try-with-resources?
  29. What is multi-catch block in java?
    什么是Java中的多捕获块?
  30. What is static block?
    什么是静态块?
  31. What is an interface?
    什么是接口?
  32. What is an abstract class?
    什么是抽象类?
  33. What is the difference between abstract class and interface?
    抽象类和接口有什么区别?
  34. Can an interface implement or extend another interface?
    一个接口可以实现或扩展另一个接口吗?
  35. What is Marker interface?
    什么是标记界面?
  36. What are Wrapper classes?
    什么是包装器类?
  37. What is Enum in Java?
    Java中的Enum是什么?
  38. What is Java Annotations?
    什么是Java注释?
  39. What is Java Reflection API? Why it’s so important to have?
    什么是Java Reflection API? 为什么拥有这么重要?
  40. What is composition in java?
    Java中的成分是什么?
  41. What is the benefit of Composition over Inheritance?
    与继承相比,组合有什么好处?
  42. How to sort a collection of custom Objects in Java?
    如何在Java中对自定义对象的集合进行排序?
  43. What is inner class in java?
    Java中的内部类是什么?
  44. What is anonymous inner class?
    什么是匿名内部类?
  45. What is Classloader in Java?
    Java中的类加载器是什么?
  46. What are different types of classloaders?
    有哪些不同类型的类加载器?
  47. What is ternary operator in java?
    Java中的三元运算符是什么?
  48. What does super keyword do?
    super关键字有什么作用?
  49. What is break and continue statement?
    什么是中断并继续声明?
  50. What is this keyword?
    这个关键字是什么?
  51. What is default constructor?
    什么是默认构造函数?
  52. Can we have try without catch block?
    我们可以尝试没有挡块吗?
  53. What is Garbage Collection?
    什么是垃圾回收?
  54. What is Serialization and Deserialization?
    什么是序列化和反序列化?
  55. How to run a JAR file through command prompt?
    如何通过命令提示符运行JAR文件?
  56. What is the use of System class?
    System类的用途是什么?
  57. What is instanceof keyword?
    什么是instanceof关键字?
  58. Can we use String with switch case?
    我们可以在开关盒中使用String吗?
  59. Java is Pass by Value or Pass by Reference?
    Java是按值传递还是按引用传递?
  60. What is difference between Heap and Stack Memory?
    堆和堆栈内存有什么区别?
  61. Java Compiler is stored in JDK, JRE or JVM?
    Java编译器存储在JDK,JRE或JVM中吗?
  62. What will be the output of following programs?
    以下程序的输出是什么?

Java核心面试问答 (Core Java Interview Questions and Answers)

  1. 列举Java 14版本的一些重要功能吗? (Name some important features of Java 14 release?)

    Java 14 was released on March 17, 2020. It is a Non-LTS version. Some of the developer specific Java 14 Features are:

    1. Switch Expressions – JEP 361
    2. Pattern matching improvements of instanceof operator – Preview Feature, JEP 305
    3. Helpful NullPointerException Messages – JEP 358
    4. Text Blocks – Second Preview, JEP 368
    5. Records – data class, preview feature, JEP 359.

    Java 14于2020年3月17日发布。它是Non-LTS版本。 开发人员特定的Java 14功能包括:

    1. 开关表达式– JEP 361
    2. instanceof运算符的模式匹配改进–预览功能,JEP 305
    3. 有用的NullPointerException消息– JEP 358
    4. 文本块–第二次预览,JEP 368
    5. 记录–数据类,预览功能,JEP 359。
  2. 列举Java 13版本的一些重要功能吗? (Name some important features of Java 13 release?)

    Java 13 was launched on Sept 17, 2019. It is a Non-LTS version. Some of the developer specific Java 13 Features are:

    1. Text Blocks – Preview Feature, JEP 355
    2. Switch Expressions – Preview Feature, JEP 354
    3. Reimplementation of Java Sockets API – JEP 353
    4. Dynamic CDS Archive – JEP 350
    5. FileSystems.newFileSystem() Method
    6. DOM and SAX Factories with Namespace support
    7. Unicode 12.1 support
    8. ZGC improvements to return Unused Memory – JEP 351

    Java 13于2019年9月17日发布。它是Non-LTS版本。 开发人员特定的Java 13功能包括:

    1. 文本块–预览功能,JEP 355
    2. 开关表达式–预览功能,JEP 354
    3. 重新实现Java套接字API – JEP 353
    4. 动态CDS存档– JEP 350
    5. FileSystems.newFileSystem()方法
    6. 支持命名空间的DOM和SAX工厂
    7. Unicode 12.1支持
    8. ZGC改进以返回未使用的内存– JEP 351
  3. 列举Java 12版本的一些重要功能吗? (Name some important features of Java 12 release?)

    Java 12 was launched on March 19, 2019. It is a Non-LTS version. Some of the Java 12 Features are:

    1. JVM Changes – JEP 189, JEP 346, JEP 344, and JEP 230.
    2. Switch Expressions
    3. File mismatch() Method
    4. Compact Number Formatting
    5. Teeing Collectors in Stream API
    6. New Methods in String Class
    7. JEP 334: JVM Constants API
    8. JEP 305: Pattern Matching for instanceof
    9. Raw String Literals is Removed From JDK 12.

    Java 12于2019年3月19日发布。它是Non-LTS版本。 Java 12的一些功能包括:

    1. JVM更改– JEP 189,JEP 346,JEP 344和JEP 230。
    2. 切换表达式
    3. 文件不匹配()方法
    4. 紧凑数字格式
    5. 在Stream API中准备收集器
    6. 字符串类中的新方法
    7. JEP 334:JVM常量API
    8. JEP 305:用于instanceof的模式匹配
    9. 从JDK 12中删除了原始字符串文字。
  4. Java 11版本的重要功能是什么? (What are the important features of Java 11 release?)

    Java 11 is the second LTS release after Java 8. They’ve changed the licensing and support model which means if you download the Java 11 Oracle JDK, it will be paid for commercial use. If you want to use a free version, you can download it from the OpenJDK website.

    Some of the important features of Java 11 are:

    • We can run a java program directly through java command. The source file will be implicitly compiled and executed. It’s part of JEP 330 feature implementation.
    • There are 6 new methods added in String class – isBlank(), lines(), strip(), stripLeading(), stripTrailing(), and repeat(). You can read all about them at our article on Java String class.
    • Files class got two new methods to read/write string data – readString() and writeString().
    • We can use “var” with lambda expressions too. It’s part of JEP 323 implementation.
    • Epsilon: A No-Op Garbage Collector for test environments. It’s an experimental feature and part of JEP 318.
    • Java EE and CORBA Modules have been removed from the standard JDK build. Nashorn JavaScript Engine has also been deprecated.
    • New HTTP Client (JEP 321) and Flight Recorder (JEP 328)

    For a complete overview, please read Java 11 Features.

    Java 11是继Java 8之后的第二个LTS版本。它们已经更改了许可和支持模型,这意味着,如果您下载Java 11 Oracle JDK,则将以商业用途付费。 如果要使用免费版本,可以从OpenJDK网站下载。

    Java 11的一些重要功能包括:

    • 我们可以直接通过java命令运行Java程序。 源文件将被隐式编译和执行。 它是JEP 330功能实现的一部分。
    • String类中添加了6个新方法– isBlank(),lines(),strip(),stripLeading(),stripTrailing()和repeat()。 您可以在我们有关Java String类的文章中阅读有关它们的全部信息。
    • Files类有两种读取/写入字符串数据的新方法– readString()和writeString()。
    • 我们也可以在lambda表达式中使用“ var”。 它是JEP 323实施的一部分。
    • Epsilon:用于测试环境的无操作垃圾收集器。 这是一项实验性功能,是JEP 318的一部分。
    • Java EE和CORBA模块已从标准JDK构建中删除。 Nashorn JavaScript Engine也已弃用。
    • 新的HTTP客户端(JEP 321)和Flight Recorder(JEP 328)

    有关完整的概述,请阅读Java 11功能 。

  5. Java 10版本的重要功能是什么? (What are the important features of Java 10 release?)

    Java 10 is the first every-six-months from Oracle corporation, so it’s not a major release like earlier versions. However, some of the important features of Java 10 are:

    • Local-Variable Type Inference
    • Enhance java.util.Locale and related APIs to implement additional Unicode extensions of BCP 47 language tags.
    • Enable the HotSpot VM to allocate the Java object heap on an alternative memory device, such as an NV-DIMM, specified by the user.
    • Provide a default set of root Certification Authority (CA) certificates in the JDK.

    Java 10 is mostly a maintenance release, however I really liked the local variable type inference feature. For a detailed overview of Java 10, please go through Java 10 Features.

    Java 10是Oracle公司每六个月发布的软件,因此它不是早期版本的主要发行版。 但是,Java 10的一些重要功能是:

    • 局部变量类型推断
    • 增强java.util.Locale和相关API,以实现BCP 47语言标签的其他Unicode扩展。
    • 使HotSpot VM可以在用户指定的备用存储设备(例如NV-DIMM)上分配Java对象堆。
    • 在JDK中提供一组默认的根证书颁发机构(CA)证书。

    Java 10主要是维护版本,但是我真的很喜欢局部变量类型推断功能。 有关Java 10的详细概述,请浏览Java 10功能 。

  6. Java 9版本的重要功能是什么? (What are the important features of Java 9 release?)

    Java 9 was a major release and brought a lot of features. Some of the important features are:

    • Java 9 REPL (JShell)
    • Java 9 Module System
    • Factory Methods for Immutable List, Set, Map and Map.Entry
    • Private methods in Interfaces
    • Reactive Streams
    • GC (Garbage Collector) Improvements

    You will find more details about them at Java 9 Features.

    Java 9是主要版本,并带来了许多功能。 一些重要的功能是:

    • Java 9 REPL(JShell)
    • Java 9模块系统
    • 不可变列表,集合,映射和Map.Entry的工厂方法
    • 接口中的私有方法
    • React流
    • GC(垃圾收集器)改进

    您可以在Java 9 Features中找到有关它们的更多详细信息。

  7. Java 8版本的重要功能是什么? (What are the important features of Java 8 release?)

    Java 8 has been released in March 2014, so it’s one of the hot topics in java interview questions. If you answer this question clearly, it will show that you like to keep yourself up-to-date with the latest technologies.

    Java 8 has been one of the biggest releases after Java 5 annotations and generics. Some of the important features of Java 8 are:

    1. Interface changes with default and static methods
    2. Functional interfaces and Lambda Expressions
    3. Java Stream API for collection classes
    4. Java Date Time API

    I strongly recommend to go through above links to get proper understanding of each one of them, also read Java 8 Features.

    Java 8已于2014年3月发布,因此它是Java面试问题中的热门话题之一。 如果您清楚地回答了这个问题,它将表明您喜欢保持最新技术。

    Java 8是继Java 5注释和泛型之后的最大版本之一。 Java 8的一些重要功能包括:

    1. 使用默认和静态方法更改接口
    2. 功能接口和Lambda表达式
    3. 集合类的Java Stream API
    4. Java日期时间API

    我强烈建议您仔细阅读以上链接,以正确理解其中的每个链接,还请阅读Java 8 Features 。

  8. 用Java命名一些OOPS概念? (Name some OOPS Concepts in Java?)

    Java is based on Object Oriented Programming Concepts, following are some of the OOPS concepts implemented in java programming.

    • Abstraction
    • Encapsulation
    • Polymorphism
    • Inheritance
    • Association
    • Aggregation
    • Composition

    Read more about them at OOPS Concepts in Java.

    Java基于面向对象的编程概念,以下是在Java编程中实现的一些OOPS概念。

    • 抽象化
    • 封装形式
    • 多态性
    • 遗产
    • 协会
    • 聚合
    • 组成

    在Java的OOPS概念中了解有关它们的更多信息。

  9. Java平台独立性是什么意思? (What do you mean by platform independence of Java?)

    Platform independence means that you can run the same Java Program in any Operating System. For example, you can write java program in Windows and run it in Mac OS.

    平台独立性意味着您可以在任何操作系统中运行相同的Java程序。 例如,您可以在Windows中编写Java程序并在Mac OS中运行它。

  10. 什么是JVM,它与平台无关吗? (What is JVM and is it platform independent?)

    Java Virtual Machine (JVM) is the heart of java programming language. JVM is responsible for converting byte code into machine-readable code. JVM is not platform-independent, that’s why you have different JVM for different operating systems. We can customize JVM with Java Options, such as allocating minimum and maximum memory to JVM. It’s called virtual because it provides an interface that doesn’t depend on the underlying OS.

    Java虚拟机(JVM)是​​Java编程语言的核心。 JVM负责将字节码转换为机器可读的代码。 JVM不是独立于平台的,这就是为什么您为不同的操作系统使用不同的JVM的原因。 我们可以使用Java选项自定义JVM,例如为JVM分配最小和最大内存。 之所以称为虚拟的,是因为它提供了不依赖于底层操作系统的接口。

  11. JDK和JVM有什么区别? (What is the difference between JDK and JVM?)

    Java Development Kit (JDK) is for development purpose and JVM is a part of it to execute the java programs.

    JDK provides all the tools, executables and binaries required to compile, debug and execute a Java Program. The execution part is handled by JVM to provide machine independence.

    Java Development Kit(JDK)用于开发目的,JVM是其中的一部分,用于执行Java程序。

    JDK提供编译,调试和执行Java程序所需的所有工具,可执行文件和二进制文件。 执行部分由JVM处理,以提供机器独立性。

  12. JVM和JRE有什么区别? (What is the difference between JVM and JRE?)

    Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any development tools like java compiler, debugger, etc. If you want to execute any java program, you should have JRE installed.

    Java Runtime Environment(JRE)是JVM的实现。 JRE由JVM和Java二进制文件以及其他类组成,可以成功执行任何程序。 JRE不包含Java编译器,调试器等任何开发工具。如果要执行任何Java程序,则应安装JRE。

  13. 哪个类是所有类的超类? (Which class is the superclass of all classes?)

    java.lang.Object is the root class for all the java classes and we don’t need to extend it.

    java.lang.Object是所有java类的根类,我们不需要扩展它。

  14. 为什么Java不支持多重继承? (Why Java doesn’t support multiple inheritance?)

    Java doesn’t support multiple inheritance in classes because of “Diamond Problem”. To know more about diamond problem with example, read Multiple Inheritance in Java.

    However multiple inheritances are supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of the diamond problem with interfaces.

    由于“钻石问题”,Java不支持类中的多重继承。 要了解有关示例的钻石问题的更多信息,请阅读Java中的多重继承 。

    但是,接口中支持多个继承。 一个接口可以扩展多个接口,因为它们只声明方法,而实现将出现在实现类中。 因此,接口没有钻石问题。

  15. 为什么Java不是纯粹的面向对象语言? (Why Java is not pure Object Oriented language?)

    Java is not said to be pure object-oriented because it supports primitive types such as int, byte, short, long, etc. I believe it brings simplicity to the language while writing our code. Java could have wrapper objects for the primitive types but just for the representation, they would not have provided any benefit.

    As we know, for all the primitive types we have wrapper classes such as Integer, Long etc that provides some additional methods.

    不能说Java是纯粹的面向对象的,因为它支持基本类型,例如int,byte,short,long等。我相信它在编写代码时为语言带来了简化。 Java可以具有用于原始类型的包装对象,但是仅用于表示形式,它们不会提供任何好处。

    众所周知,对于所有基本类型,我们都有包装器类,例如Integer,Long等,它们提供了一些其他方法。

  16. path和classpath变量之间有什么区别? (What is difference between path and classpath variables?)

    PATH is an environment variable used by the operating system to locate the executables. That’s why when we install Java or want any executable to be found by OS, we need to add the directory location in the PATH variable. If you work on Windows OS, read this post to learn how to setup PATH variable on Windows.

    Classpath is specific to Java and used by java executables to locate class files. We can provide the classpath location while running java application and it can be a directory, ZIP files, JAR files, etc.

    PATH是操作系统用来查找可执行文件的环境变量。 这就是为什么当我们安装Java或希望OS找到任何可执行文件时,我们需要在PATH变量中添加目录位置。 如果您使用Windows操作系统,请阅读此文章以了解如何在Windows上设置PATH变量 。

    类路径特定于Java,并且Java可执行文件使用它来查找类文件。 我们可以在运行Java应用程序时提供classpath位置,它可以是目录,ZIP文件,JAR文件等。

  17. Java中main方法的重要性是什么? (What is the importance of main method in Java?)

    main() method is the entry point of any standalone java application. The syntax of main method is public static void main(String args[]).

    Java’s main method is public and static so that Java runtime can access it without initializing the class. The input parameter is an array of String through which we can pass runtime arguments to the java program. Check this post to learn how to compile and run java program.

    main()方法是任何独立的Java应用程序的入口。 main方法的语法为public static void main(String args[])

    Java的主要方法是public和static,因此Java运行时可以在不初始化类的情况下访问它。 输入参数是String数组,我们可以通过该数组将运行时参数传递给java程序。 检查这篇文章,了解如何编译和运行Java程序 。

  18. 什么是Java中的重载和重载? (What is overloading and overriding in java?)

    When we have more than one method with the same name in a single class but the arguments are different, then it is called method overloading.

    The overriding concept comes in picture with inheritance when we have two methods with the same signature, one in the parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if the parent class method is changed, so as child class.

    当我们在一个类中有多个具有相同名称的方法,但参数不同时,则称为方法重载。

    当我们有两个具有相同签名的方法时,继承的概念就带有继承性:一个在父类中,另一个在子类中。 我们可以在子类重写方法中使用@Override批注,以确保是否更改了父类方法(例如子类)。

  19. 我们可以重载主要方法吗? (Can we overload main method?)

    Yes, we can have multiple methods with name “main” in a single class. However if we run the class, java runtime environment will look for main method with syntax as public static void main(String args[]).

    是的,我们可以在一个类中使用多个名称为“ main”的方法。 但是,如果我们运行该类,则Java运行时环境将查找语法为public static void main(String args[]) main方法。

  20. 我们可以在一个Java源文件中有多个公共类吗? (Can we have multiple public classes in a java source file?)

    We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.

    一个Java源文件中不能有多个公共类。 单个源文件可以具有多个非公共类。

  21. 什么是Java包,默认情况下会导入哪个包? (What is Java Package and which package is imported by default?)

    Java package is the mechanism to organize the java classes by grouping them. The grouping logic can be based on functionality or modules based. A java class fully classified name contains package and class name. For example, java.lang.Object is the fully classified name of Object class that is part of java.lang package.

    java.lang package is imported by default and we don’t need to import any class from this package explicitly.

    Java包是通过对Java类进行分组来组织它们的机制。 分组逻辑可以基于功能或基于模块。 Java类的完全分类名称包含包和类名称。 例如, java.lang.ObjectObject类的完全分类名称,它是java.lang包的一部分。

    默认情况下, java.lang软件包是导入的,我们不需要从该软件包中显式导入任何类。

  22. 什么是访问修饰符? (What are access modifiers?)

    Java provides access control through public, private and protected access modifier keywords. When none of these are used, it’s called default access modifier.
    A java class can only have public or default access modifier. Read Java Access Modifiers to learn more about these in detail.

    Java通过公共,私有和受保护的访问修饰符关键字提供访问控制。 如果不使用这些,则称为默认访问修饰符。
    一个Java类只能具有public或default访问修饰符。 阅读Java Access修饰符以详细了解这些内容。

  23. 什么是最终关键字? (What is final keyword?)

    The final keyword is used with Class to make sure no other class can extend it. For example, the String class is final and we can’t extend it.

    We can use the final keyword with methods to make sure child classes can’t override it.

    Java’s final keyword can be used with variables to make sure that it can be assigned only once. However the state of the variable can be changed, for example, we can assign a final variable to an object only once but the object variables can change later on.

    Java interface variables are by default final and static.

    final关键字与Class一起使用,以确保没有其他类可以对其进行扩展。 例如,String类是最终的,我们不能扩展它。

    我们可以将final关键字与方法配合使用,以确保子类不能覆盖它。

    Java的final关键字可以与变量一起使用,以确保只能分配一次。 但是,变量的状态可以更改,例如,我们只能将一个最终变量分配给一个对象一次,但以后可以更改对象变量。

    Java接口变量默认为final和static。

  24. 什么是静态关键字? (What is static keyword?)

    static keyword can be used with class-level variables to make it global i.e all the objects will share the same variable.

    static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.

    Read more in detail at java static keyword.

    static关键字可以与类级变量一起使用,以使其具有全局性,即所有对象将共享同一变量。

    static关键字也可以与方法一起使用。 静态方法只能访问类的静态变量,并且只能调用类的静态方法。

    详细了解Java static关键字 。

  25. Java中的final和finalize是什么? (What is finally and finalize in java?)

    The finally block is used with try-catch to put the code that you want to get executed always, even if an exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.

    finalize() is a special method in Object class that we can override in our classes. This method gets called by the garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when the object is garbage collected.

    即使try-catch块引发了异常,finally块也会与try-catch一起使用,以始终放置您要执行的代码。 finally块主要用于释放在try块中创建的资源。

    finalize()是Object类中的一种特殊方法,可以在类中重写。 当对象正在收集垃圾时,垃圾收集器将调用此方法。 当对象被垃圾回收时,通常会重写此方法以释放系统资源。

  26. 我们可以将一个类声明为静态的吗? (Can we declare a class as static?)

    We can’t declare a top-level class as static however an inner class can be declared as static. If the inner class is declared as static, it’s called a static nested class.
    The static nested class is the same as any other top-level class and is nested for only packaging convenience.

    Read more about inner classes at java inner class.

    我们不能将顶级类声明为静态,但是可以将内部类声明为静态。 如果将内部类声明为静态,则称为静态嵌套类。
    静态嵌套类与任何其他顶级类相同,并且仅出于包装方便而嵌套。

    在java inner class中阅读有关内部类的更多信息。

  27. 什么是静态导入? (What is static import?)

    If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.

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

    We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.

    Use of static import can cause confusion, so it’s better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.

    如果必须使用其他类中的任何静态变量或方法,通常我们会导入该类,然后使用带有类名的方法/变量。

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

    我们可以通过仅导入静态方法或变量然后在类中使用它,就像它属于它一样,来做同样的事情。

    使用静态导入会引起混乱,因此最好避免这种情况。 过度使用静态导入可能会使您的程序无法读取和维护。

  28. 什么是Java中的try-with-resources? (What is try-with-resources in java?)

    One of the Java 7 features is the try-with-resources statement for automatic resource management. Before Java 7, there was no auto resource management and we should explicitly close the resource. Usually, it was done in the finally block of a try-catch statement. This approach used to cause memory leaks when we forgot to close the resource.

    From Java 7, we can create resources inside try block and use it. Java takes care of closing it as soon as try-catch block gets finished. Read more at Java Automatic Resource Management.

    Java 7的功能之一是用于自动资源管理的try-with-resources语句。 在Java 7之前,没有自动资源管理,我们应该显式关闭资源。 通常,它是在try-catch语句的finally块中完成的。 当我们忘记关闭资源时,这种方法曾经导致内存泄漏。

    从Java 7开始,我们可以在try块内创建资源并使用它。 Java会在try-catch块完成后立即关闭它。 在Java自动资源管理中了解更多信息。

  29. 什么是Java中的多捕获块? (What is multi-catch block in java?)

    Java 7 one of the improvement was a multi-catch block where we can catch multiple exceptions in a single catch block. This makes our code shorter and cleaner when every catch block has a similar code.

    If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, the exception parameter (ex) is final, so you can’t change it.

    Read more at Java multi catch block.

    Java 7的改进之一是多捕获块,我们可以在一个捕获块中捕获多个异常。 当每个catch块都有相似的代码时,这会使我们的代码更短,更清晰。

    如果catch块处理多个异常,则可以使用管道(|)分隔它们,在这种情况下,异常参数(ex)是最终的,因此无法更改。

    在Java多捕获块中内容。

  30. 什么是静态块? (What is static block?)

    Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when class is loaded.

    Java静态块是由Java ClassLoader将类加载到内存中时执行的语句组。 它用于初始化类的静态变量。 通常,它用于在加载类时创建静态资源。

  31. 什么是接口? (What is an interface?)

    Interfaces are core concepts of java programming language and used a lot not only in JDK but also java design patterns, most of the frameworks and tools. Interfaces provide a way to achieve abstraction in java and used to define the contract for the subclasses to implement.

    Interfaces are good for starting point to define Type and create top level hierarchy in our code. Since a java class can implements multiple interfaces, it’s better to use interfaces as super class in most of the cases. Read more at java interface.

    接口是Java编程语言的核心概念,不仅在JDK中使用了很多,还在Java设计模式,大多数框架和工具中使用了很多。 接口提供了一种在Java中实现抽象的方法,并用于定义要实现的子类的协定。

    接口非常适合作为在我们的代码中定义Type和创建顶层层次结构的起点。 由于Java类可以实现多个接口,因此在大多数情况下,最好将接口用作超类。 在java interface上内容。

  32. 什么是抽象类? (What is an abstract class?)

    Abstract classes are used in java to create a class with some default method implementation for subclasses. An abstract class can have an abstract method without the body and it can have methods with implementation also.

    abstract keyword is used to create a abstract class. Abstract classes can’t be instantiated and mostly used to provide base for sub-classes to extend and implement the abstract methods and override or use the implemented methods in abstract class. Read important points about abstract classes at java abstract class.

    在Java中,抽象类用于创建具有某些默认方法实现子类的类。 抽象类可以具有没有主体的抽象方法,并且也可以具有带有实现的方法。

    abstract关键字用于创建抽象类。 抽象类无法实例化,主要用于为子类提供基础,以扩展和实现抽象方法,并覆盖或使用抽象类中的已实现方法。 在java abstract class中阅读有关抽象类的要点。

  33. 抽象类和接口有什么区别? (What is the difference between abstract class and interface?)

    abstract keyword is used to create abstract class whereas interface is the keyword for interfaces.

    Abstract classes can have method implementations whereas interfaces can’t.

    A class can extend only one abstract class but it can implement multiple interfaces.

    We can run an abstract class if it has a main() method whereas we can’t run an interface.

    Some more differences in detail are at Difference between Abstract Class and Interface.

    abstract关键字用于创建抽象类,而interface是用于接口的关键字。

    抽象类可以具有方法实现,而接口则不能。

    一个类只能扩展一个抽象类,但可以实现多个接口。

    如果抽象类具有main()方法,则可以运行它,而不能运行接口。

    更多详细的区别在于抽象类和接口之间的区别 。

  34. 一个接口可以实现或扩展另一个接口吗? (Can an interface implement or extend another interface?)

    Interfaces don’t implement another interface, they extend it. Since interfaces can’t have method implementations, there is no issue of diamond problem. That’s why we have multiple inheritances in interfaces i.e an interface can extend multiple interfaces.

    From Java 8 onwards, interfaces can have default method implementations. So to handle diamond problem when a common default method is present in multiple interfaces, it’s mandatory to provide implementation of the method in the class implementing them. For more details with examples, read Java 8 interface changes.

    接口没有实现另一个接口,而是对其进行了扩展。 由于接口不能具有方法实现,因此不会出现菱形问题。 这就是为什么我们在接口中具有多个继承,即一个接口可以扩展多个接口。

    从Java 8开始,接口可以具有默认方法实现。 因此,当多个接口中存在通用默认方法时,要处理钻石问题,必须在实现它们的类中提供该方法的实现。 有关示例的更多详细信息,请阅读Java 8接口更改 。

  35. 什么是标记界面? (What is Marker interface?)

    A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.

    标记接口是一个空接口,没有任何方法,但用于强制Java实现类中的某些功能。 一些众所周知的标记器接口是可序列化的和可克隆的。

  36. 什么是包装器类? (What are Wrapper classes?)

    Java wrapper classes are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes.

    Read more at Wrapper classes in Java.

    Java包装器类是Java中八种原始类型的对象表示形式。 Java中的所有包装器类都是不可变的且是最终的。 Java 5自动装箱和拆箱允许在原始类型及其对应的包装器类之间轻松进行转换。

    在Java的Wrapper类中内容。

  37. Java中的Enum是什么? (What is Enum in Java?)

    Enum was introduced in Java 1.5 as a new type whose fields consist of a fixed set of constants. For example, in Java, we can create Direction as an enum with fixed fields as EAST, WEST, NORTH, SOUTH.

    enum is the keyword to create an enum type and similar to the class. Enum constants are implicitly static and final. Read more in detail at java enum.

    Enum在Java 1.5中作为一种新类型引入,其字段由一组固定的常量组成。 例如,在Java中,我们可以将Direction创建为带有固定字段(例如EAST,WEST,NORTH,SOUTH)的枚举。

    enum是用于创建枚举类型的关键字,与该类相似。 枚举常量是隐式静态的和最终的。 在Java枚举中详细了解更多。

  38. 什么是Java注释? (What is Java Annotations?)

    Java Annotations provide information about the code and they have no direct effect on the code they annotate. Annotations are introduced in Java 5. Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or by the compiler. We can also specify annotation availability to either compile-time only or till runtime. Java Built-in annotations are @Override, @Deprecated and @SuppressWarnings. Read more at java annotations.

    Java批注提供有关代码的信息,并且它们对其批注的代码没有直接影响。 注释是在Java 5中引入的。注释是有关嵌入在程序本身中的程序的元数据。 可以通过注释解析工具或编译器进行解析。 我们还可以将注释可用性指定为仅在编译时或直到运行时。 Java内置注释是@ Override,@ Deprecated和@SuppressWarnings。 在Java注解中了解更多信息。

  39. 什么是Java Reflection API? 为什么拥有这么重要? (What is Java Reflection API? Why it’s so important to have?)

    Java Reflection API provides the ability to inspect and modify the runtime behavior of java application. We can inspect a java class, interface, enum and get their methods and field details. Reflection API is an advanced topic and we should avoid it in normal programming. Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.

    Even though we don’t use Reflection API in normal programming, it’s very important to have. We can’t have any frameworks such as Spring, Hibernate or servers such as Tomcat, JBoss without Reflection API. They invoke the appropriate methods and instantiate classes through reflection API and use it a lot for other processing.

    Read Java Reflection Tutorial to get in-depth knowledge of reflection api.

    Java Reflection API提供了检查和修改Java应用程序运行时行为的功能。 我们可以检查Java类,接口,枚举并获取其方法和字段详细信息。 Reflection API是高级主题,在常规编程中应避免使用它。 反射API的使用可以通过调用私有构造函数(即违反访问修饰符的规则)来破坏设计模式(例如Singleton模式)。

    即使我们在常规编程中不使用Reflection API,拥有它也很重要。 没有反射API,我们将没有任何诸如Spring,Hibernate之类的框架或诸如Tomcat,JBoss之类的服务器。 他们通过反射API调用适当的方法并实例化类,并将其大量用于其他处理。

    阅读Java Reflection Tutorial以获取反射API的深入知识。

  40. Java中的成分是什么? (What is composition in java?)

    Composition is the design technique to implement has-a relationship in classes. We can use Object composition for code reuse.

    Java composition is achieved by using instance variables that refer to other objects. The benefit of using composition is that we can control the visibility of other objects to client classes and reuse only what we need. Read more with example at Java Composition example.

    组合是在类中实现具有关系的设计技术。 我们可以使用对象组合进行代码重用。

    Java组合是通过使用引用其他对象的实例变量来实现的。 使用组合的好处是我们可以控制其他对象对客户端类的可见性,并且仅重用我们需要的对象。 在Java Composition示例中阅读有关示例的更多信息。

  41. 与继承相比,组合有什么好处? (What is the benefit of Composition over Inheritance?)

    One of the best practices of Java programming is to “favor composition over inheritance”. Some of the possible reasons are:

    • Any change in the superclass might affect subclass even though we might not be using the superclass methods. For example, if we have a method test() in the subclass and suddenly somebody introduces a method test() in the superclass, we will get compilation errors in the subclass. The composition will never face this issue because we are using only what methods we need.
    • Inheritance exposes all the superclass methods and variables to the client and if we have no control in designing superclass, it can lead to security holes. Composition allows us to provide restricted access to the methods and hence more secure.
    • We can get runtime binding in composition where inheritance binds the classes at compile time. So composition provides flexibility in the invocation of methods.

    You can read more about above benefits of composition over inheritance at java composition vs inheritance.

    Java编程的最佳实践之一是“偏向于继承而不是继承”。 一些可能的原因是:

    • 即使我们可能未使用超类方法,超类中的任何更改也可能会影响子类。 例如,如果我们在子类中有一个方法test(),而突然有人在超类中引入了方法test(),我们将在子类中得到编译错误。 合成永远不会遇到这个问题,因为我们仅使用所需的方法。
    • 继承向客户端公开了所有超类方法和变量,如果我们无法控制超类的设计,则可能导致安全漏洞。 合成使我们能够提供对方法的受限访问,因此更加安全。
    • 我们可以在合成中获得运行时绑定,其中继承在编译时绑定了类。 因此,组合为方法的调用提供了灵活性。

    您可以在Java组合与继承上阅读有关组合对继承的上述好处的更多信息。

  42. 如何在Java中对自定义对象的集合进行排序? (How to sort a collection of custom Objects in Java?)

    We need to implement Comparable interface to support sorting of custom objects in a collection. The Comparable interface has compareTo(T obj) method which is used by sorting methods and by providing this method implementation, we can provide default way to sort custom objects collection.

    However, if you want to sort based on different criteria, such as sorting an Employees collection based on salary or age, then we can create Comparator instances and pass it as sorting methodology. For more details read Java Comparable and Comparator.

    我们需要实现Comparable接口以支持对集合中的自定义对象进行排序。 Comparable接口具有compareTo(T obj)方法,该方法由排序方法使用,并通过提供此方法实现,我们可以提供对自定义对象集合进行排序的默认方法。

    但是,如果您想根据不同的标准进行排序,例如根据薪水或年龄对Employees集合进行排序,则可以创建Comparator实例并将其作为排序方法传递。 有关更多详细信息,请阅读Java Comparable和Comparator 。

  43. Java中的内部类是什么? (What is inner class in java?)

    We can define a class inside a class and they are called nested classes. Any non-static nested class is known as an inner class. Inner classes are associated with the object of the class and they can access all the variables and methods of the outer class. Since inner classes are associated with the instance, we can’t have any static variables in them.

    We can have local inner class or anonymous inner class inside a class. For more details read java inner class.

    我们可以在一个类中定义一个类,它们称为嵌套类。 任何非静态嵌套类都称为内部类。 内部类与该类的对象相关联,它们可以访问外部类的所有变量和方法。 由于内部类与实例相关联,因此我们不能在其中包含任何静态变量。

    我们可以在一个类中包含本地内部类或匿名内部类。 有关更多详细信息,请阅读Java内部类 。

  44. 什么是匿名内部类? (What is anonymous inner class?)

    A local inner class without a name is known as an anonymous inner class. An anonymous class is defined and instantiated in a single statement. Anonymous inner class always extend a class or implement an interface.

    Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class. Anonymous inner classes are accessible only at the point where it is defined.

    没有名称的本地内部类称为匿名内部类。 匿名类在单个语句中定义和实例化。 匿名内部类总是扩展一个类或实现一个接口。

    由于匿名类没有名称,因此无法为匿名类定义构造函数。 匿名内部类仅可在定义点访问。

  45. Java中的类加载器是什么? (What is Classloader in Java?)

    Java Classloader is the program that loads byte code program into memory when we want to access any class. We can create our own classloader by extending ClassLoader class and overriding loadClass(String name) method. Learn more at java classloader.

    Java Classloader是当我们要访问任何类时将字节代码程序加载到内存中的程序。 我们可以通过扩展ClassLoader类并覆盖loadClass(String name)方法来创建自己的类加载器。 在java classloader中了解更多信息。

  46. 有哪些不同类型的类加载器? (What are different types of classloaders?)

    There are three types of built-in Class Loaders in Java:

    1. Bootstrap Class Loader – It loads JDK internal classes, typically loads rt.jar and other core classes.
    2. Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
    3. System Class Loader – It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options.

    Java中内置了三种类型的内置类加载器:

    1. Bootstrap类加载器–它加载JDK内部类,通常加载rt.jar和其他核心类。
    2. 扩展类加载器–它从JDK扩展目录(通常为$ JAVA_HOME / lib / ext目录)中加载类。
    3. 系统类加载器–它从当前类路径加载类,这些类可以在使用-cp或-classpath命令行选项调用程序时进行设置。
  47. Java中的三元运算符是什么? (What is ternary operator in java?)

    Java ternary operator is the only conditional operator that takes three operands. It’s a one liner replacement for if-then-else statement and used a lot in java programming. We can use ternary operator if-else conditions or even switch conditions using nested ternary operators. An example can be found at java ternary operator.

    Java三元运算符是唯一采用三个操作数的条件运算符。 它是if-then-else语句的一个衬里替换,并且在Java编程中使用了很多。 我们可以使用三元运算符if-else条件,甚至可以使用嵌套三元运算符切换条件。 可以在java三元运算符中找到一个示例。

  48. super关键字有什么作用? (What does super keyword do?)

    The super keyword can be used to access the superclass method when you have overridden the method in the child class.

    We can use the super keyword to invoke superclass constructors in child class constructor but in this case, it should be the first statement in the constructor method.

    package com.journaldev.access;public class SuperClass {public SuperClass(){}public SuperClass(int i){}public void test(){System.out.println("super class test method");}
    }

    Use of super keyword can be seen in below child class implementation.

    当您在子类中重写该方法时,可以使用super关键字访问该方法。

    我们可以使用super关键字在子类构造函数中调用超类构造函数,但是在这种情况下,它应该是构造方法中的第一条语句。

    package com.journaldev.access;public class SuperClass {public SuperClass(){}public SuperClass(int i){}public void test(){System.out.println("super class test method");}
    }

    在下面的子类实现中可以看到super关键字的使用。

  49. 什么是中断并继续声明? (What is break and continue statement?)

    We can use break statement to terminate for, while, or do-while loop. We can use a break statement in the switch statement to exit the switch case. You can see the example of break statement at java break. We can use a break with the label to terminate the nested loops.

    The continue statement skips the current iteration of a for, while or do-while loop. We can use the continue statement with the label to skip the current iteration of the outermost loop.

    我们可以使用break语句终止for,while或do-while循环。 我们可以在switch语句中使用break语句退出switch案例。 您可以在java break上查看break语句的示例。 我们可以在标签上使用break来终止嵌套循环。

    Continue语句跳过for,while或do-while循环的当前迭代。 我们可以使用带有标签的continue语句来跳过最外层循环的当前迭代。

  50. 这个关键字是什么? (What is this keyword?)

    this keyword provides the reference to the current object and it’s mostly used to make sure that object variables are used, not the local variables having the same name.

    //constructor
    public Point(int x, int y) {this.x = x;this.y = y;
    }

    We can also use this keyword to invoke other constructors from a constructor.

    此关键字提供对当前对象的引用,主要用于确保使用了对象变量,而不是具有相同名称的局部变量。

    //constructor
    public Point(int x, int y) {this.x = x;this.y = y;
    }

    我们还可以使用此关键字从构造函数中调用其他构造函数。

  51. 什么是默认构造函数? (What is default constructor?)

    No argument constructor of a class is known as default constructor. When we don’t define any constructor for the class, java compiler automatically creates the default no-args constructor for the class. If there are other constructors defined, then compiler won’t create default constructor for us.

    没有一个类的参数构造函数称为默认构造函数。 当我们没有为该类定义任何构造函数时,java编译器会自动为该类创建默认的no-args构造函数。 如果定义了其他构造函数,则编译器不会为我们创建默认构造函数。

  52. 我们可以尝试没有挡块吗? (Can we have try without catch block?)

    Yes, we can have try-finally statement and hence avoiding catch block.

    是的,我们可以使用try-finally语句,从而避免catch阻塞。

  53. 什么是垃圾回收? (What is Garbage Collection?)

    Garbage Collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. In Java, the process of deallocating memory is handled automatically by the garbage collector.

    We can run the garbage collector with code Runtime.getRuntime().gc() or use utility method System.gc(). For a detailed analysis of Heap Memory and Garbage Collection, please read Java Garbage Collection.

    垃圾收集是查看堆内存,识别正在使用和未使用的对象以及删除未使用的对象的过程。 在Java中,垃圾回收器会自动处理释放内存的过程。

    我们可以使用代码Runtime.getRuntime().gc()或使用实用程序方法System.gc()来运行垃圾收集器。 有关Heap Memory and Garbage Collection的详细分析,请阅读Java Garbage Collection 。

  54. 什么是序列化和反序列化? (What is Serialization and Deserialization?)

    We can convert a Java object to a Stream that is called Serialization. Once an object is converted to Stream, it can be saved to file or send over the network or used in socket connections.

    The object should implement a Serializable interface and we can use java.io.ObjectOutputStream to write objects to file or to any OutputStream object. Read more at Java Serialization.

    The process of converting stream data created through serialization to Object is called deserialization. Read more at Java Deserialization.

    我们可以将Java对象转换为称为序列化的Stream。 将对象转换为Stream后,可以将其保存到文件或通过网络发送,或用于套接字连接。

    该对象应实现一个Serializable接口,我们可以使用java.io.ObjectOutputStream将对象写入文件或任何OutputStream对象。 在Java Serialization中内容。

    将通过序列化创建的流数据转换为Object的过程称为反序列化。 在Java反序列化中了解更多信息。

  55. 如何通过命令提示符运行JAR文件? (How to run a JAR file through command prompt?)

    We can run a jar file using java command but it requires Main-Class entry in jar manifest file. Main-Class is the entry point of the jar and used by java command to execute the class. Learn more at java jar file.

    我们可以使用java命令运行jar文件,但需要jar清单文件中的Main-Class条目。 Main-Class是jar的入口点,由java命令用于执行该类。 在java jar文件中了解更多信息。

  56. System类的用途是什么? (What is the use of System class?)

    Java System Class is one of the core classes. One of the easiest ways to log information for debugging is System.out.print() method.

    System class is final so that we can’t subclass and override its behavior through inheritance. System class doesn’t provide any public constructors, so we can’t instantiate this class and that’s why all of its methods are static.

    Some of the utility methods of System class are for array copy, get the current time, reading environment variables. Read more at Java System Class.

    Java系统类是核心类之一。 记录调试信息最简单的方法之一是System.out.print()方法。

    系统类是最终的,因此我们不能通过继承来继承和覆盖其行为。 系统类未提供任何公共构造函数,因此我们无法实例化此类,这就是其所有方法都是静态的原因。

    System类的一些实用程序方法用于数组复制,获取当前时间,读取环境变量。 在Java System Class中内容。

  57. 什么是instanceof关键字? (What is instanceof keyword?)

    We can use the instanceof keyword to check if an object belongs to a class or not. We should avoid it’s usage as much as possible. Sample usage is:

    public static void main(String args[]){Object str = new String("abc");if(str instanceof String){System.out.println("String value:"+str);}if(str instanceof Integer){System.out.println("Integer value:"+str);}
    }

    Since str is of type String at runtime, first if statement evaluates to the true and second one to false.

    我们可以使用instanceof关键字检查对象是否属于一个类。 我们应该尽可能避免使用它。 样本用法为:

    由于在运行时str的类型为String,因此第一个if语句的值为true,第二个为false。

  58. 我们可以在开关盒中使用String吗? (Can we use String with switch case?)

    One of the Java 7 feature was improvement of switch case of allow Strings. So if you are using Java 7 or higher version, you can use String in switch-case statements. Read more at Java switch-case String example.

    Java 7的功能之一是改进了允许字符串的切换大小写。 因此,如果您使用的是Java 7或更高版本,则可以在switch-case语句中使用String。 在Java switch-case String example上内容。

  59. Java是按值传递还是按引用传递? (Java is Pass by Value or Pass by Reference?)

    This is a very confusing question, we know that object variables contain the reference to the Objects in heap space. When we invoke any method, a copy of these variables is passed and gets stored in the stack memory of the method. We can test any language whether it’s pass by reference or pass by value through a simple generic swap method, to learn more read Java is Pass by Value and Not Pass by Reference.

    这是一个非常令人困惑的问题,我们知道对象变量包含对堆空间中对象的引用。 当我们调用任何方法时,这些变量的副本将被传递并存储在该方法的堆栈存储器中。 我们可以测试任何语言,无论是通过引用传递还是通过简单的通用交换方法传递值,以了解更多信息, Java是按值传递而不是按引用传递 。

  60. 堆和堆栈内存有什么区别? (What is difference between Heap and Stack Memory?)

    Major difference between Heap and Stack memory are as follows:

    • Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution.
    • Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
    • Memory management in the stack is done in a LIFO manner whereas it’s more complex in Heap memory because it’s used globally.

    For a detailed explanation with a sample program, read Java Heap vs Stack Memory.

    堆和堆栈内存之间的主要区别如下:

    • 堆内存由应用程序的所有部分使用,而堆栈内存仅由一个执行线程使用。
    • 每当创建对象时,它始终存储在堆空间中,并且堆栈存储器包含对该对象的引用。 Stack memory only contains local primitive variables and reference variables to objects in heap space.
    • Memory management in the stack is done in a LIFO manner whereas it's more complex in Heap memory because it's used globally.

    For a detailed explanation with a sample program, read Java Heap vs Stack Memory .

  61. Java Compiler is stored in JDK, JRE or JVM? (Java Compiler is stored in JDK, JRE or JVM?)

    The task of java compiler is to convert java program into bytecode, we have javac executable for that. So it must be stored in JDK, we don’t need it in JRE and JVM is just the specs.

    The task of java compiler is to convert java program into bytecode, we have javac executable for that. So it must be stored in JDK, we don't need it in JRE and JVM is just the specs.

  62. What will be the output of following programs? (What will be the output of following programs?)

    1. static method in class

      package com.journaldev.util;public class Test {public static String toString(){System.out.println("Test toString called");return "";}public static void main(String args[]){System.out.println(toString());}
      }

      Answer: The code won’t compile because we can’t have an Object class method with static keyword. Note that Object class has toString() method. You will get a compile-time error as “This static method cannot hide the instance method from Object”. The reason is that static method belongs to the class and since every class base is Object, we can’t have the same method in the instance as well as in class. You won’t get this error if you change the method name from toString() to something else that is not present in superclass Object.

    2. static method invocation

      Answer: Well this is a strange situation. We all have seen NullPointerException when we invoke a method on the object that is NULL. But here this program will work and prints “Test foo called”.

      The reason for this is the java compiler code optimization. When the java code is compiled to produced byte code, it figures out that foo() is a static method and should be called using class. So it changes the method call obj.foo() to Test.foo() and hence no NullPointerException.

      I must admit that it’s a very tricky question and if you are interviewing someone, this will blow his mind off.

      java核心面试_前100多个核心Java面试问题相关推荐

      1. java培训机构排名前十,持续更新大厂面试笔试题

        四面阿里 面试岗位是研发工程师,直接找蚂蚁金服的大佬进行内推,参与了阿里巴巴中间件部门的提前批面试,一共经历了四次面试,拿到了口头offer. 一面: 自我介绍 项目中做了什么,难点呢. Java的线 ...

      2. java跳槽原因_跳槽求职必看:Java程序猿面试失败的5大原因!

        四月,又到了应聘跳槽高峰期了,而应聘程序员的要求更是越来越多了.如今的企业不仅要程序员做的了专职码农,还要你有一定的工作经验和过硬的技术,不仅找的到bug还能搞的定产品,不仅逻辑思维细密还要能抗压力, ...

      3. 易鑫集团Java面试题_史上最全!2020面试阿里,字节跳动90%被问到的JVM面试题(附答案)...

        前言:最近老是收到小伙伴的私信问我能不能帮忙整理出一份JVM相关的面试题出来,说自己在大厂去面试的时候这一块问的是特别多的,每次自己学的时候每次都学不到重点去.这不他来了,一份详细的JVM面试真题给大 ...

      4. java servlet_Java Servlet的前100个问题

        java servlet 1)是" servlets"目录还是" servlet"目录? 回答: 对于Java Web Server: 在文件系统上,它是&qu ...

      5. 优酷土豆java面试_优酷土豆笔试之Java开发类题目

        一.选择题 选择题非常简单,都是基础题,什么死锁发生的条件.HashMap和HashSet查找插入删除的时间复杂度.Thread类和Runnable接口.排序复杂度比较.建堆调整堆等等,具体的也记不得 ...

      6. 优酷土豆java面试_优酷土豆校园招聘Java开发类笔试题目

        先总体说下题型,共有20道选择题,4道简答题,3道编程题和1道扩展题,题目都比较简单,限时一小时完成, 一.选择题 选择题非常简单,都是基础题,什么死锁发生的条件.HashMap和HashSet查找插 ...

      7. 平安银行java爬虫面试_【中国平安银行信用卡中心java工程师面试】校招进来的,刷人还是蛮多的。-看准网...

        参加2017年春招--信贷审批岗:先面试后笔试,在东华大学松江校区面试,当天一面结束马上就会有结果出来,通过的人接着二面,二面结束回去等通知,大概2天后就要笔试.本人外地非211学校小硕一枚,进入二面 ...

      8. 华为OD面试【前】经验分享,包含面试准备的各个细节

        文章目录 核心的华为 OD 机试 了解华为 OD 职位和要求 什么是华为 OD 以及 OD 岗位分类 华为 OD 相关岗位都需要哪些技能和经验 为什么要申请 OD 职位 准备简历和面试材料 简历的重点 ...

      9. java 底层运行_从表面到底层丨Java和JVM的运行原理,现在带给你

        Java,编程语言,被创造于90年代初,在经历了这么多年的风风雨雨,Java已经成长为世界第一的编程语言,根据往期以及目前的数据来看,Java的使用频率为全球第一,即使偶尔会有第二第三的情况,但是这依 ...

      最新文章

      1. 如何在 jetbrick-template 中使用 debug函数?
      2. 自定义语言的实现——解释器模式
      3. jpa 查询 列表_终极JPA查询和技巧列表–第3部分
      4. C#编写不安全代码初探
      5. 快递官网漏洞泄露 1400 万用户信息
      6. 【bzoj 3669】[Noi2014]魔法森林
      7. express怎么读取html,Express 配置HTML页面访问的实现
      8. 惠普电脑u盘重装系统步骤_惠普电脑优盘装系统步骤
      9. 越狱后如何添加cydia源及cydia源大全
      10. 为什么要选择平台+独立站运营的双轨模式
      11. 数商云跨境电商平台解决方案 1
      12. #1_《经典控制理论复习》摘录
      13. 企业微信可以自动上班打卡吗?
      14. SuperMap 许可简介
      15. 器件选型 贴片还是直插好?
      16. 大数据学习之hadoop——07阿里云服务器搭建集群以及遇到的问题解决办法
      17. 自动超频_电脑超频有什么用?一键超频原来还有着功效
      18. 让人工智能有情感的秘诀!清华权威报告看透情感计算【附下载】
      19. Vue结合天地图(天地图API)Gis开发
      20. java8 stream 首字母分组排序

      热门文章

      1. HTML+CSS 整站 步骤
      2. 谷歌技术quot;三宝quot;之MapReduce
      3. [转载] Python机器学习库scikit-learn使用小结(二)
      4. [转载] python笔记
      5. struts2与spring集成时,关于class属性及成员bean自动注入的问题
      6. 微信H5页面内实现一键关注公众号
      7. indexOf(String.indexOf 方法)
      8. javax.crypto.BadPaddingException: Blocktype ...
      9. word文档生成目录
      10. 设计模式练习:Decorate模式