Collection of classic Java interview questions (1)
##1. The difference between & and &&
(Recommended study:java interview questions )
& operator has two uses: (1) bitwise AND; (2) logical AND. The&& operator is a short-circuit AND (or concise AND) operation. The difference between logical AND and short-circuit AND is very huge. Although both require the Boolean values on the left and right sides of the operator to be true, the value of the entire expression is true. && is called a short-circuit operation because if the value of the expression on the left of && is false, the expression on the right will be directly short-circuited and no operation will be performed. Many times we may need to use && instead of &. For example, when verifying user login, it is determined that the username is not null and not an empty string. It should be written as username != null &&!username.equals(""). The order of the two cannot be exchanged, let alone used. & operator, because if the first condition is not true, the equals comparison of strings cannot be performed at all, otherwise a NullPointerException exception will be generated.2. When using the final keyword to modify a variable, does it mean that the reference cannot be changed or the referenced object cannot be changed?
When using the final keyword to modify a variable, it means that the reference variable cannot be changed, but the content of the object pointed to by the reference variable can still be changed. Example:public class Test10 { // final修饰基本类型的变量 public static final char CHAR = '中'; // final修饰引用类型的变量 public static final StringBuffer a = new StringBuffer("StringBuffer"); public static void main(String[] args) { // 编译报错,引用不能变 // a = new StringBuffer("hehe"); // 引用变量所指向的对象中的内容还是可以改变的 a.append("xxx"); } public static int method1(final int i) { // i = i + 1;// 编译报错,因为final修饰的是基本类型的变量 return i; } // 有人在定义方法的参数(引用变量)时,可能想采用如下的形式来阻止方法内部修改传进来的参数对象, // 实际上,这是办不到的,在该方法内部任然可以增加如下代码来修改参数对象 public static void method2(final StringBuffer buffer) { buffer.append("buffer");// 编译通过,因为final修饰的是引用类型的变量 } }
3. What is the difference between static variables and instance variables?
Syntax difference: static variables need to be modified with the static keyword, but instance variables do not. The difference when the program is running: static variables are subordinate to classes, and instance variables are subordinate to objects. Instance variables must create an instance object, and the instance variables in it will be allocated space before this instance variable can be used; Static variables are category quantities, as long as the program loads the bytes of the class code, the static variable will be allocated space and can be used. To sum up, instance variables must be used through this object after creating an object, and static variables can be referenced directly using the class name. Note: The use of (static) static variables also has limitations. Non-static methods and variables in the class cannot be called in a static method. There is only one variable in the memory after the class is loaded. A memory space that can be shared by all instance objects of a class.4. Is it possible to issue a call to a non-static method from within a static method?
Can't. Because non-static methods are associated with objects, an object must be created before method calls can be made on the object. When calling static methods, there is no need to create an object and can be called directly. In other words, when a static method is called, no instance object may have been created. If a call to a non-static method is issued from a static method, which object is the non-static method associated with? What about? This logic cannot be established, so a static method internally issues a call to a non-static method.5. What is the difference between "==" and equals methods? The
== operator is specifically used to compare whether the values of two variables are the same, that is, to compare whether the values stored in the memory corresponding to the variables are the same. If you want to compare two basic types of data or two reference variables for equality, you can only use the == operator. The equals method is used to compare whether the contents of two independent objects are the same, just like comparing whether two books are the same. The two objects it compares are independent. Code example:String a = new String("AA"); String b = new String("AA"); System.out.println(a==b); System.out.println(a.equals(b));
boolean equals(Object o) { return this==o; }
The above is the detailed content of Collection of classic Java interview questions (1). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
