/
String and Object Comparison String and Object Comparison

String and Object Comparison - PowerPoint Presentation

trish-goza
trish-goza . @trish-goza
Follow
372 views
Uploaded On 2018-02-06

String and Object Comparison - PPT Presentation

Comparison operator Equality Testing vs Assignment The operator tests for equality of numeric or other primitive data types not object data types if x 0 if x equals zero ID: 628478

nickname equals rectangle comparison equals nickname comparison rectangle cerealbox tests string object operator public stringexample class oatmealbox input rob

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "String and Object Comparison" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

String and Object Comparison

Comparison operatorSlide2

Equality Testing vs. Assignment

The = = operator tests for

equality of numeric or other primitive data types (not object data types).

if

(x = = 0) . . // if x equals zero

The = operator assigns a value to a

variable

:

x

= 0; // assign 0 to x

Don't confuse them. Slide3

String Comparison

Don't use = = for strings!

if (input = = "Y") // WRONG!!!

Use equals method:

if (

input.equals

("Y"))

= = tests identity, equals tests equal contents

Case insensitive test ("Y" or "y")

if (

input.equalsIgnoreCase

("Y"))

Slide4

public class StringExample

{

// instance variables - replace the example below with your own

private String name;

private String nickname;

/**

* Constructor for objects of class StringExample

*/

public StringExample()

{

// initialize instance variables

name = "Robert";

nickname = name.substring(0,3);

}

public void displayNames()

{

if (nickname == "Rob")

{

System.out.println("The nickname is Rob");

}

else

{

System.out.println("There is no nickname");

}

}Slide5

Object Comparison

= = tests for identity, equals for identical content

Rectangle

cerealBox

= new Rectangle(5, 10, 20, 30);

Rectangle

oatmealBox

= new Rectangle(5, 10, 20, 30);

Rectangle r =

cerealBox

;

cerealBox

!=

oatmealBox

,

but

cerealBox.equals

(

oatmealBox

)

cerealBox

== r

Note: equals must be defined for the classSlide6

Object Comparison