search
HomeJavajavaTutorialWhat does == mean in Java?

What does == mean in Java?

May 21, 2019 pm 04:56 PM
java

What does the relational operator "==" compare?

The following sentence is an excerpt from the book "Java Programming Thoughts": "Relational operators generate a boolean result, and they calculate the relationship between the values ​​of the operands. ".

This sentence seems simple, but it still needs to be understood carefully. To put it simply, == is used to compare whether values ​​are equal.

Let’s look at a few examples:

public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub 
        int n=3;
        int m=3;        
        System.out.println(n==m);        
        String str = new String("hello");
        String str1 = new String("hello");
        String str2 = new String("hello");        
        System.out.println(str1==str2);        
        str1 = str;
        str2 = str;
        System.out.println(str1==str2);
    }
}

The output result is:

true 
false
true

n==mThe result is true , this is easy to understand. The values ​​stored in variable n and variable m are both 3, which must be equal. And why are the results of the two comparisons of str1 and str2 different? To understand this, you only need to understand the difference between basic data type variables and non-basic data type variables.

For the variables of these 8 basic data types, the variables directly store the "value", so when the relational operator == is used for comparison, the "value" itself is compared. It should be noted that floating point types and integer types are both signed types, while char is an unsigned type (the value range of the char type is 0~2^16-1).

That is to say For example:

int n=3;
int m=3; 

Variable n and variable m both directly store the value "3", so when compared with ==, the result is true.

For variables of non-basic data types, they are called reference type variables in some books. For example, str1 above is a reference type variable. A reference type variable stores not the "value" itself, but the address of its associated object in memory.

For example, the following line of code:

String str1;

This sentence declares a reference type variable, which is not associated with any object at this time.

Use new String("hello") to generate an object (also called an instance of the String class) and bind this object to str1:

str1= new String("hello");

Then str1 points to an object (str1 is also called a reference to an object in many places). At this time, the variable str1 stores the storage address of the object it points to in memory, not the "value" itself, that is It says that the string "hello" is not stored directly. The references here are very similar to pointers in C/C.

So when you use == to compare str1 and str2 for the first time, the result is false. Therefore, they point to different objects respectively, which means that the memory addresses where they are actually stored are different.

In the second comparison, both str1 and str2 point to the object pointed to by str, so the result is undoubtedly true.

The above is the detailed content of What does == mean in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to convert names to numbers to implement sorting within groups?How to convert names to numbers to implement sorting within groups?Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

In Java remote debugging, how to correctly obtain constant values ​​on remote servers?In Java remote debugging, how to correctly obtain constant values ​​on remote servers?Apr 19, 2025 pm 01:54 PM

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer?In back-end development, how to distinguish the responsibilities of the service layer and the dao layer?Apr 19, 2025 pm 01:51 PM

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.