Home > Java > JavaBase > body text

How to use == in java

angryTom
Release: 2019-11-14 13:23:20
Original
4092 people have browsed it

How to use == in java

How to use == in java

== in java is often used in if statements or ternary operators, For example:

if (1 == 2) {
}

1 == 2 ? "true" : "false";
Copy after login

In Java, if it is a basic data type, == compares the value; if it is an object type, == compares the address of the object .

Among them, == can also be used to judge strings.

Assign a value to String through direct assignment instead of new. If the object exists in the string constant pool, it will not be created again. At this time, it is judged by == and true is returned

For example:

String str="wo";
String str1="wo";
str == str1
Copy after login

is true. In JDK1.6 and earlier versions, the string constant pool is in the method area; in JDK1.7 and later, the string constant pool is in the heap. The reference of the object is stored in the stack

php Chinese website, a large number of freeJava introductory tutorials, welcome to learn online!

The above is the detailed content of How to use == in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!