Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
143bb67c
Commit
143bb67c
authored
2 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
equals() vs. == for Strings
parent
c3581887
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/CoreClasses/coreClasses.xml
+61
-1
61 additions, 1 deletion
Doc/Sd1/CoreClasses/coreClasses.xml
with
61 additions
and
1 deletion
Doc/Sd1/CoreClasses/coreClasses.xml
+
61
−
1
View file @
143bb67c
...
...
@@ -202,11 +202,71 @@ equals: true</screen></td>
<listitem>
<para>
The
<methodname
xlink:href=
"https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)"
>
equals()
</methodname>
method
compares
two object
's value
s.
</para>
method
defines the equality
two objects.
</para>
</listitem>
</itemizedlist>
</figure>
<figure
xml:id=
"sd1_coreclasses_fig_operatorEqualVsMathImply"
>
<title>
Operator == and
<methodname>
equals()
</methodname>
implications
</title>
<itemizedlist>
<listitem>
<para>
Each object is equal by value to itself:
</para>
<para><code>
object1 == object2
</code>
⇒
<code>
object1.equals(object2)
</code></para>
</listitem>
<listitem>
<para>
The converse is not true. Two different objects may be of common
value:
</para>
<informaltable
border=
"0"
>
<colgroup
width=
"44%"
/>
<colgroup
width=
"56%"
/>
<tr>
<th>
Code
</th>
<th>
Result
</th>
</tr>
<tr>
<td
valign=
"top"
><programlisting
language=
"java"
>
String s = "Hello", copy = new String(s);
System.out.println("equals: " + s.equals(copy));
System.out.println(" ==: " + (s == copy));
</programlisting></td>
<td
valign=
"top"
><screen>
equals: true
==: false
</screen></td>
</tr>
</informaltable>
</listitem>
</itemizedlist>
</figure>
<figure
xml:id=
"sd1_coreclasses_fig_equalsByDefinition"
>
<title><methodname>
equals()
</methodname>
is being defined within
respective class!
</title>
<para>
Implementation at
<link
xlink:href=
"https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/String.java"
>
https://github.com/openjdk/
.../String.java
</link>
:
</para>
<programlisting
language=
"java"
>
public final class String ... {
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
return (anObject instanceof String aString)
&&
(!COMPACT_STRINGS || this.coder == aString.coder)
&&
StringLatin1.equals(value, aString.value);
}
</programlisting>
</figure>
<section
xml:id=
"sw1_sect_CoreClasses_hashing"
>
<title>
Objects, equals() and hash-values
</title>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment