How does .equals Java String Class method work?
In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?
String word = "hello"
word.equals("hello");
output is true
java class methods
add a comment |
In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?
String word = "hello"
word.equals("hello");
output is true
java class methods
3
What do you mean by "a random variable of type string"?
– Henry
Nov 12 '18 at 13:23
In Java you can instantiate objects without assigning it to a variable. Here this is the case for"hello"
:word.equals("hello");
– davidxxx
Nov 12 '18 at 13:27
add a comment |
In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?
String word = "hello"
word.equals("hello");
output is true
java class methods
In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?
String word = "hello"
word.equals("hello");
output is true
java class methods
java class methods
asked Nov 12 '18 at 13:19
The Great VisionaryThe Great Visionary
15117
15117
3
What do you mean by "a random variable of type string"?
– Henry
Nov 12 '18 at 13:23
In Java you can instantiate objects without assigning it to a variable. Here this is the case for"hello"
:word.equals("hello");
– davidxxx
Nov 12 '18 at 13:27
add a comment |
3
What do you mean by "a random variable of type string"?
– Henry
Nov 12 '18 at 13:23
In Java you can instantiate objects without assigning it to a variable. Here this is the case for"hello"
:word.equals("hello");
– davidxxx
Nov 12 '18 at 13:27
3
3
What do you mean by "a random variable of type string"?
– Henry
Nov 12 '18 at 13:23
What do you mean by "a random variable of type string"?
– Henry
Nov 12 '18 at 13:23
In Java you can instantiate objects without assigning it to a variable. Here this is the case for
"hello"
: word.equals("hello");
– davidxxx
Nov 12 '18 at 13:27
In Java you can instantiate objects without assigning it to a variable. Here this is the case for
"hello"
: word.equals("hello");
– davidxxx
Nov 12 '18 at 13:27
add a comment |
4 Answers
4
active
oldest
votes
The equals()
method is used to verify if the state of the instances of two Java classes is the same. Because equals()
is from the Object class, every Java class inherits it. But the equals()
method has to be overridden to make it work properly. Of course, String overrides equals()
.
Take a look:
public boolean equals(Object anObject)
if (this == anObject)
return true;
if (anObject instanceof String)
String aString = (String)anObject;
if (coder() == aString.coder())
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
return false;
As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared
I have taken it from here. Better if you have an idea about String pool also. So please refer the link
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
add a comment |
String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.
We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here
Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java
add a comment |
If you are comparing String
with primitives like int,long
.
The answer is
String is not a primitive. It is a class. There are several ways to create an object from this class,
like
String word=new String("hello");
String word="hello"; //special case for String class
now word
becomes an object and you can call the method equals
(because every object you create is inherited from java.lang.Object) class.
If you look at primitives like int
.
you cannot do something like below, beacause number
is not an object.
int number=12;
number.equals(12)
add a comment |
equals method compares the string on which the method is called with the string which is passed as a parameter.
Take a look at these links:
String Comparison
Equals on String
Overriding equals in String class
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263058%2fhow-does-equals-java-string-class-method-work%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The equals()
method is used to verify if the state of the instances of two Java classes is the same. Because equals()
is from the Object class, every Java class inherits it. But the equals()
method has to be overridden to make it work properly. Of course, String overrides equals()
.
Take a look:
public boolean equals(Object anObject)
if (this == anObject)
return true;
if (anObject instanceof String)
String aString = (String)anObject;
if (coder() == aString.coder())
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
return false;
As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared
I have taken it from here. Better if you have an idea about String pool also. So please refer the link
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
add a comment |
The equals()
method is used to verify if the state of the instances of two Java classes is the same. Because equals()
is from the Object class, every Java class inherits it. But the equals()
method has to be overridden to make it work properly. Of course, String overrides equals()
.
Take a look:
public boolean equals(Object anObject)
if (this == anObject)
return true;
if (anObject instanceof String)
String aString = (String)anObject;
if (coder() == aString.coder())
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
return false;
As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared
I have taken it from here. Better if you have an idea about String pool also. So please refer the link
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
add a comment |
The equals()
method is used to verify if the state of the instances of two Java classes is the same. Because equals()
is from the Object class, every Java class inherits it. But the equals()
method has to be overridden to make it work properly. Of course, String overrides equals()
.
Take a look:
public boolean equals(Object anObject)
if (this == anObject)
return true;
if (anObject instanceof String)
String aString = (String)anObject;
if (coder() == aString.coder())
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
return false;
As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared
I have taken it from here. Better if you have an idea about String pool also. So please refer the link
The equals()
method is used to verify if the state of the instances of two Java classes is the same. Because equals()
is from the Object class, every Java class inherits it. But the equals()
method has to be overridden to make it work properly. Of course, String overrides equals()
.
Take a look:
public boolean equals(Object anObject)
if (this == anObject)
return true;
if (anObject instanceof String)
String aString = (String)anObject;
if (coder() == aString.coder())
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
return false;
As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared
I have taken it from here. Better if you have an idea about String pool also. So please refer the link
edited Nov 12 '18 at 14:11
suvojit_007
1,3251517
1,3251517
answered Nov 12 '18 at 13:25
SandSand
1,417319
1,417319
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
add a comment |
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
@Pshemo True. Edited it. Thanks
– Sand
Nov 12 '18 at 13:47
add a comment |
String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.
We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here
Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java
add a comment |
String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.
We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here
Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java
add a comment |
String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.
We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here
Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java
String class (String in Java is Object) inherited equals method from Object and override it as is correctly mentioned in answers by Sand and Armine.
We can call methods on String (even on constant String, statement: "Hello World".length() returns 11) because String is an Object and not a primitive type. Primitive types are byte, short, int,... You could read more about them here
Nice reading about differences between primitives types and Objects: Difference between Primitive and Reference variable in Java
edited Nov 12 '18 at 14:07
answered Nov 12 '18 at 14:01
user2851729user2851729
336
336
add a comment |
add a comment |
If you are comparing String
with primitives like int,long
.
The answer is
String is not a primitive. It is a class. There are several ways to create an object from this class,
like
String word=new String("hello");
String word="hello"; //special case for String class
now word
becomes an object and you can call the method equals
(because every object you create is inherited from java.lang.Object) class.
If you look at primitives like int
.
you cannot do something like below, beacause number
is not an object.
int number=12;
number.equals(12)
add a comment |
If you are comparing String
with primitives like int,long
.
The answer is
String is not a primitive. It is a class. There are several ways to create an object from this class,
like
String word=new String("hello");
String word="hello"; //special case for String class
now word
becomes an object and you can call the method equals
(because every object you create is inherited from java.lang.Object) class.
If you look at primitives like int
.
you cannot do something like below, beacause number
is not an object.
int number=12;
number.equals(12)
add a comment |
If you are comparing String
with primitives like int,long
.
The answer is
String is not a primitive. It is a class. There are several ways to create an object from this class,
like
String word=new String("hello");
String word="hello"; //special case for String class
now word
becomes an object and you can call the method equals
(because every object you create is inherited from java.lang.Object) class.
If you look at primitives like int
.
you cannot do something like below, beacause number
is not an object.
int number=12;
number.equals(12)
If you are comparing String
with primitives like int,long
.
The answer is
String is not a primitive. It is a class. There are several ways to create an object from this class,
like
String word=new String("hello");
String word="hello"; //special case for String class
now word
becomes an object and you can call the method equals
(because every object you create is inherited from java.lang.Object) class.
If you look at primitives like int
.
you cannot do something like below, beacause number
is not an object.
int number=12;
number.equals(12)
answered Nov 12 '18 at 13:53
DushDush
713312
713312
add a comment |
add a comment |
equals method compares the string on which the method is called with the string which is passed as a parameter.
Take a look at these links:
String Comparison
Equals on String
Overriding equals in String class
add a comment |
equals method compares the string on which the method is called with the string which is passed as a parameter.
Take a look at these links:
String Comparison
Equals on String
Overriding equals in String class
add a comment |
equals method compares the string on which the method is called with the string which is passed as a parameter.
Take a look at these links:
String Comparison
Equals on String
Overriding equals in String class
equals method compares the string on which the method is called with the string which is passed as a parameter.
Take a look at these links:
String Comparison
Equals on String
Overriding equals in String class
edited 2 days ago
answered Nov 12 '18 at 13:33
ArmineArmine
6622826
6622826
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263058%2fhow-does-equals-java-string-class-method-work%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What do you mean by "a random variable of type string"?
– Henry
Nov 12 '18 at 13:23
In Java you can instantiate objects without assigning it to a variable. Here this is the case for
"hello"
:word.equals("hello");
– davidxxx
Nov 12 '18 at 13:27