Show
Comparing objects is a little different than comparing primitive typed values like numbers. Objects can be very complex and have many attribute values or instance variables inside them. For example, the turtle objects have many instance variables like name, width, height, xPos, yPos, etc. When comparing two turtle objects, we need a specially written equals method to compare all of these values. In this lesson, we will take a look at String objects and how they are compared with == vs. the equals method. 3.7.1. String Equality¶The equals method for Strings compares two strings letter by letter. When the
operator Figure 1: String aliases¶ Coding ExerciseIf you run the following, what will be printed? The following video traces
through the code above and shows how Here’s the representation of memory where s2 and s3 refer to the same String object. Figure 2: s2 and s3 are aliases referring to the same String object¶ 3.7.2. Equality with New Strings¶If you use the What will the following print? Watch the video below to see how this code works in memory. Since we used the Here is the representation of these String objects in memory. Figure 3: Two strings that are equal with equals but not with ==.¶ Note that you can also create Strings using string literals instead of new, like Note Only use 3-7-5: Which of the following is true after the code executes? String s1 = new String("hi"); String s2 = new String("bye"); String s3 = new String("hi"); s2 = s1; 3-7-6: Which of the following is true after the code executes? String s1 = new String("hi"); String s2 = new String("bye"); String s3 = new String("hi"); 3-7-7: Which of the following is true after the code executes? String s1 = new String("hi"); String s2 = new String("bye"); String s3 = new String("hi"); 3.7.3. Comparing with null¶One common place to use == or != with objects is to compare them to null to see if they really exist. Sometimes short-circuit evaluation is used to avoid an error if the object doesn’t exist. Remember that short-circuit evaluation is used with && in Java meaning that if the first part of the if condition is false, it doesn’t even have to check the second condition and it knows the whole && test is false. Coding ExerciseTry the following code to see a NullPointer error (if you don’t see the error because of the autograding, you can copy it into the pencil icon scratch area to run it without the grader). Since s is null, indexOf throws an NullPointer error for s. Comment out the first if statement and run the program again. The second if statement avoids the error with shortcircuit evaluation. Because s != null is false, the rest of the boolean expression is not evaluated. Now, change s to set it to “apple” instead of null in the first line and run the code again to see that the if statements can print out that “apple contains an a”. The following video shows how the null string reference works in memory. 3.7.4. Programming Challenge : Tracing Code¶What will the following code print out? Trace through the code by drawing diagrams of what is going on in memory like the figures above, and then show the values of s1, s2, s3, s4 and the output after each line of code. Remember that you can use trace tables to track the values of variables as they change throughout a program. To trace through code, write down a variable in each column in a table and keep track of its value throughout the program as you go through it line by line. String s1 = null; String s2 = new String("hi"); String s3 = new String("hi"); String s4 = new String("bye"); if (s1 == null) s1 = s2; if (s1 == s2) System.out.println("s1 and s2 refer to the same object"); if (s2 == s3) System.out.println("s2 and s3 refer to the same object"); if (s3 == s4) System.out.println("s3 and s4 refer to the same object"); if (s1.equals(s2) && s2.equals(s3)) System.out.println("s1, s2, s3 are equal");
3-7-10: Write your tracing table here that keeps track of s1, s2, s3, s4 and the output. 3.7.5. Summary¶
3.7.6. AP Practice¶3-7-11: Consider the following code segment. String message = new String("AP Practice"); String note = new String("AP Practice"); String memo = new String("memo"); int i = 5; if (message.equals(note) && !message.equals("memo")) { message = note; if (message == note && message.length() > i) { i = 3; memo = message.substring(i); } } Which of the following expressions evaluate to message == note && message == memo Message does not refer to the same object as memo. message.equals(note) && message.equals(memo) Message is not the same string as in memo. message == note && memo.equals(“Practice”) Yes, both if statements in the code above execute changing message to equal note and memo to equal “Practice”. message != note || message == memo Both of these are false. message.equals(memo) || memo.equals(note) Both of these are false. You have attempted of activities on this page Which of these methods of class string is used to compare two string objects?Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true.
Which of these method of class string is used to compare two string objects for their equality a equals () b equals () C Isequal ()?Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.
Which of these method of class string is used to compare two string objects for their equality immersive reader 1 point equals () equals () Isequal () Isequal ()?Explanation: equals() is method of class String, it is used to check equality of two String objects, if they are equal, true is retuned else false.
Which method can be used to compare two strings for equality?1) equals() and equalsIgnoreCase()
The equals() method is used to compare the equality of both the strings. This comparison is case sensitive, but if we want to compare two strings irrespective of their case differences, then we use the equalsIgnoreCase() method.
|