I don't know why after using .toString() and having our new String, we need only to use .equals instead of == when we want to compare. But other strings can be compared with ==. Why is that and what are the cases when we need == or equals?
difference between .equals() and ==
49 Views Asked by Gargouri Nourallah At
2
There are 2 best solutions below
0
Leafxe
On
In Java, == compares addresses and .equals() compares contents. Objects, like strings are reference types so even if they contain the same content, using == while comparing two strings may not always return the same outcome.
.equals() is a method that accepts the type Object and casts the value to the class and gets the instance's private members and compare the values.
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in TYPES
- Need clarification on VHDL expressions involving std_logic_vector, unsigned and literals, unsure about compiler interpretation
- Adding a different string to a table fails
- The type of B is displayed as A when `type B = A` is used. Why is it displayed as `any` when `type B = A | A` is used instead?
- why we got same data type in two versions like "int" and "integer" in php?
- Handling NaN entries in a dataframe created from CSV
- Cannot find type definition file for 'node' in react project
- Correct way to count types in whole corpus
- Typescript: how to get possible keys from const with limited values?
- Having two Image types in React TypeScript one for upload, one for display
- MOOC.fi Java Programming course 1 - Exercise 13 "Exercises" Part 6 - Compilation error
- Is is a mistake to use type keyword after curly braces in TS when importing constants and files fro one file?
- type annotations needed, try using a fully qualified path to specify the expected types
- Need a simple example how to catch a data type error en C++
- Pyspark reads data as string but on Mongo they are double
- Extract a Maybe from a heterogeneous collection
Related Questions in COMPARISON
- I get very different comparison results for the same texts. [sentence-transformers/all-mpnet-base-v2]
- How can I compare the similarity between multiple sets?
- Adding numeric values in one CSV to those in another CSV based on matching another value in the same row
- Problem in printing the files which are different in 2 folders
- i want to compare and obtain new data of 2 files, but the place of the content changes lines
- Java string comparison issues
- Comparing Multiple Integers in C Workaround
- difference between .equals() and ==
- How to compare two tuple list, return the highest value and the variable name from which the highest value is found?
- Conjoint analysis in R: within model comparison of two sets of regerssion coefficients (AMCEs)
- Why aren't container comparison operators allocator agnostic?
- Compare two lists elementwise with OR
- Looker Studio Scorecard Not Showing Correct Date Range Comparison Percentage
- How to facet-wrap comparative density plots in R with ggplot
- Comparison between two identical tables in Quicksight
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
They can't, actually. Not reliably.
The cases when you need
==do not exist. The cases when you use.equalsare 100% of the time.(There are some cases where people may say that with string interning or other use cases,
==suffices. This is not actually a good reason, and you should never use==.)