Here are numerous ways to test for equality


Q - There are numerous ways to test for equality. == works fine for primitive types, but not for objects such as strings, etc. Why is this so? Discuss why == doesn't work when utilizing Strings but does for integers and doubles.

A - The == operator does not work for Strings because they are reference objects. A String variable holds a reference to an object as opposed to a primitive variable (int, double, float, etc) which would hold the value itself . Since == only compares the value of the two variables, it compares the references of two String variables. For true equality in a String object, each individual element of the String must be equal to the corresponding character elements of the String it is being compared to. The == operator cannot handle this comparison so we must rely on equality methods that know the components of the objects being compared. Since primitives hold the values assigned to them, as opposed to references, the == operator is able to compare them successfully.

Criteria - Write one or two paragraphs. Provide valuable and thoughtful feedback for the above answer with constructive criticism and critiques. Present original thoughts and ideas in postings, (avoid simple agreement or disagreement with, or restatement of other's postings) You may want to provide additional information from your outside readings or other outside resources. I encourage you to share links to other excellent resources during this course.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Here are numerous ways to test for equality
Reference No:- TGS0123898

Expected delivery within 24 Hours