Course Hero Logo

4 Comparing Strings.pdf - Decisions in Java – Comparing...

This preview shows page 1 - 2 out of 2 pages.

Decisions in Java – Comparing StringsComparing StringsTherelational operatorsused with the primitive data types (i.e., less than, greater than, equal to, etc.)should not be used to compare strings.Recall that aStringvariable does not actually contain thestring, but only the location (address) of the string object in memory.Thus any comparison betweenstrings using relational operators would actually be comparing two addresses, rather than theStringvalues.Java provides a number ofmethodsfor comparing strings, but for now we will introduce only theequalsand thecompareTomethods.These areString methods, which means it can be called fromanyString variable.Theequalsmethod – testing for identical stringsTheequalsmethod tests whether or not two strings areidentical, returning true if they are identical,and false otherwise.Example 1 – given the declarationString s = "Same";then theequalsmethod would yield the following results:a)s.equals("Same")will return trueb)s.equals("same")will return false because 'S' is not equal to 's'c)s.equals("Same")will return false because of the spaces at the end of the word

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 2 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Equals sign, ASCII, relational operator

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture