• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Inheritance - OCA Java SE 8 Programmer I Exam Guide - Mock Test 2 question  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
doing a mock test (TEST 2), there is the question "What is the result" for the following code:


The is one missing '}', but the mock test considers as valid the following reply: "Equals! false".

The test 2 should be corrected



 
Marshal
Posts: 64475
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the Boyarsky and Selikoff book? Have you looked here whether that has been reported before?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[color=darkred][color=#444444]public class Equality {

[color=#444444]private String word;
   Equality(String word) {this.word = word;}


}[/color] [/color] [/color]

When I runs the program it returns equals. You just need to add the false return at the end .
 
Fred Masen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public boolean equals(Object o) {
       if(((Equality) o).getWord() == word){
         System.out.println("Equals! ");



This part of the code imply it return true.
else will return false.
 
Campbell Ritchie
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That equals() method isn't correctly implemented; I shall leave it to you to find what the errors are.
 
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Masen wrote:
This part of the code imply it return true.
else will return false.


There is no implied return value in Java. To return something, there must be an explicit return statement. System.out.println() is a void method. The only time you can have an implied return is if a method is declared as void.
 
Fred Masen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree about the language specifications your described. But in the example proposed and the changes I made " It implies" true or return false. The program works properly , if you change the Howdy it will return the appropriate answer and it is working fine. The program knows that it is true at the starts and else false. The reason I am saying that is that the code I changed does not have a "TRUE s" statement so it it indirectly "implies" true at the beginning.
 
Campbell Ritchie
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . . The only time you can have an implied return is if a method is declared as void.

Do you mean an explicit empty return; statement?
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . . The only time you can have an implied return is if a method is declared as void.

Do you mean an explicit empty return; statement?


No, I meant an implicit/implied return when you just fall out of a method at the end.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Masen wrote:I agree about the language specifications your described. But in the example proposed and the changes I made " It implies" true or return false. The program works properly , if you change the Howdy it will return the appropriate answer and it is working fine. The program knows that it is true at the starts and else false. The reason I am saying that is that the code I changed does not have a "TRUE s" statement so it it indirectly "implies" true at the beginning.



We can avoid misunderstandings if you post your code that you think works. From you've said so far I'm not convinced you've actually fixed anything.
 
Campbell Ritchie
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . an implicit/implied return when you just fall out of a method at the end.

Is there such a thing? Yes, I think there probably is; I shall let you go through some bytecode, but I think a return is added to the bytecode as the last line of a void method.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

$ javap -c Foo
Compiled from "Foo.java"
public class Foo {
 public Foo();
   Code:
      0: aload_0
      1: invokespecial #1                  // Method java/lang/Object."<init>":()V
      4: return

 public static void main(java.lang.String[]);
   Code:
      0: invokestatic  #2                  // Method foo:()V
      3: return

 public static void foo();
   Code:
      0: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      3: ldc           #4                  // String Foo
      5: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      8: return
}


The bytecode is exactly the same if you put explicit return statements at the end of main() and foo()
 
Campbell Ritchie
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . The bytecode is exactly the same . . .

Which shows you were right.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Which shows you were right.


So were you

I think a return is added to the bytecode as the last line of a void method

 
Fred Masen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Lacar : Of course I fixed it otherwise I would not have posted it the changed program. I am far from being good in Java but one thing I do the mos nowt is trying the code the most I can using my Eclipse IDE before posting the program. Try yourself and you will see it works fine now
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Masen wrote:one thing I do the mos nowt is trying the code the most I can using my Eclipse IDE before posting the program. Try yourself and you will see it works fine now


I took the code you posted in your previous reply and reformatted it for clarity. Is this the code you're referring to?

If so, sorry to say that this code does not actually "work" in the sense that it is implemented correctly and does the right thing. The equals() method always returns false. It NEVER returns true as you seem to think.  Sure, the output will be "Equals!" but that does not mean the equals() method actually returns true because again, it only ever returns false.  I don't believe you understand this yet.
 
Fred Masen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Lacar : Well try to change howdy howdy to : how howdy and it returns "equal not". Basically this very very very simple program does what it is supposed to do it, comparing 2 Strings and telling you if they are the same or not. Now what happens under the wood of Java I am not aware of it and you are correct. And you are right I am not good with Boolean and the more complicated for me to learn is Arrays. I understand them but it does not come to me naturally , I keep forgetting about them. The book I have covers close to 100 pages about Arrays and I am still confused when it comes to write them.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is what that code was supposed to be:

And the expected output is:

Equals!
false


There's a subtle nuance that might escape you about line 20 above. Using == to compare two Strings is not advisable and in most circumstances, is probably a bug. Here on line 20, however, using == will be true only because of the way the objects e1 and e2 were created. They both were created with the string literal "howdy" as the parameter to their respective constructors. Java puts string literals it finds in your program into what's called the "String pool". The "howdy" String literal on line 10 and on line 11 are literally the same String object in the String pool. That's why the use of == on line 20 will seem like it works in this one particular case.

However, given different inputs, the program will not behave the way an unwary reader might expect it to behave.

Try this change to main() that circumvents Java's String pooling mechanism:

This still essentially runs with the same String values as before except now it will give you a different output.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Masen wrote:Well try to change howdy howdy to : how howdy and it returns "equal not". Basically this very very very simple program does what it is supposed to do it, comparing 2 Strings and telling you if they are the same or not.


Yeah, that's where you're not getting the point of this code. As I showed in my last example, if you change the values you're passing to the two constructors to "howdy" and "HOWDY".toLowerCase(), you probably would expect to get the same output. You won't. You'll get "Equals... not!" even though the two words are still "howdy" and "howdy", respectively.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Masen wrote:Well try to change howdy howdy to : how howdy and it returns "equal not".


Another thing you seem to be confused about: The program displays "Equals... not!", it doesn't return it. There's a huge difference.  I think this example code has too many things wrong with it that makes it likely to confuse and/or mislead beginners. However, it's a good piece of code to study if you want to see how tricky using == vs. equals() can be when you're dealing with String objects.
 
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, try to see the difference between:

1 - Code provided (this method always returns a boolean value FALSE, regardless from what it prints on screen):


2 - This method returns TRUE or FALSE, depending on the result of comparison provided in the IF statement:


3 - Same result of 2, but in a simplified way:
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ricky Bee wrote:
3 - Same result of 2, but in a simplified way:


Make sure you understand the difference between using == instead of equals() here -- code like this should immediately raise a red flag as it is likely to be a bug.

Assuming appropriate checks to guard against NullPointerException had already been made, this would be more correct:
 
Fred Masen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Equality {


private String word;

   Equality2(String word) {
       this.word = word;
   }

   public static void main(String[] args) {
       Equality2 e1 = new Equality2("HOWDY");
       Equality2 e2 = new Equality2("HOWDY".toLowerCase());
       if (e1.equals(e2)) System.out.println(e1 == e2);
   }

   public String getWord() {
       return word;
   }

   public boolean equals(Object o) {
       if (((Equality2) o).getWord() == word) {
           System.out.println("Equals! ");
           
       } else {
           System.out.print("Equals... not! ");
       }
       return false;
   }
}

   
@ Lacan : No in my example the (return true is removed) only the return false remains.  And you can change the Strings and it will work properly and give you each time the correct answer. Now if you are converting one of them as lowest case the compiler does not behave properly and gives equals not. But the goal is to compare simple strings not trying to convert them while comparing them on the same time.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more try. The point of the code example is to show the difference between == and equals()

The code I asked you to try was this:

It was NOT this, which is what you apparently tried:

There is a HUGE difference in what I asked you to try and this code that you gave.

Again, this code is a poor example of how you would write an equals() method so you should not look at it as "an example that has a goal of comparing simple strings" -- it simply is the WRONG way to compare Strings in general.
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Fred: I'm just trying to help you out, buddy. You seem to think you're right but trust me, you're mistaken and there is something you're not quite understanding about the code.

Here's why I can confidently say this:

You wrote:No in my example the (return true is removed) only the return false remains.  And you can change the Strings and it will work properly and give you each time the correct answer



I suggest you re-read the thread and really try to understand what I've been trying to tell you. Good luck.
 
Ricky Bee
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Ricky Bee wrote:
3 - Same result of 2, but in a simplified way:


Make sure you understand the difference between using == instead of equals() here -- code like this should immediately raise a red flag as it is likely to be a bug.

Assuming appropriate checks to guard against NullPointerException had already been made, this would be more correct:



Hmm... right.

So, kind of as a rule of thumb: never use == to campare the value of two String objects.

In some situations, something like that it can return FALSE even if the value of those Strings looks the same. That's the case you are showing here:

 
Fred Masen
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys but this was not my program , it was introduced by the member Parent. All I did was to suggest him  to remove the return true statement in order to make it work with simple strings. Actually this was a poorly written test program and Parent had to figure out why the program was not working properly. So as I said just remove"the return true statement" and it will gives appropriated results as long as you are working with simple strings. But I would not have created such program lol . I passed that == and equals chapters a long time ago and understands the nuances of them. Parent asked how can I make work ?? Remove the return true statement and you got a poorly designed working program
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ricky Bee wrote:
In some situations, something like that it can return FALSE even if the value of those Strings looks the same. That's the case you are showing here:


Well, at least you got the point I was trying to make. However, given the implementation of equals() we've been talking about, the code in that main() method would display this:

false
Equals... not!
false

But if you added this:

you'd see that both objects look like they have the same word:

e1.word: howdy
e2.word: howdy

Because the given equals() implementation uses == to compare the strings, it will unexpectedly display "Equals... not!" and equals() will return false in this case.

On the other hand, if you had an equals() that was implemented as this:

then you'd get this displayed:

false
true
e1.word: howdy
e2.word: howdy


Try this code and see for yourself:

 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Masen wrote:Remove the return true statement and you got a poorly designed working program


It seems you and I have very different ideas of what constitutes a "working program".

This is what I see your "fixed working program" as: https://imgflip.com/i/2gjqa5
 
Campbell Ritchie
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . This is what I see your "fixed working program" as: https://imgflip.com/i/2gjqa5

Hahahahahahahahahahaha!

No, the car in that photo is better. It will either go from A to B or it will stop altogether.
 
Campbell Ritchie
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you get that equals() method to identify two Strings the same, it will still fail to override Object#equals() correctly; it will not accept nulls, and it will not consistently return the same result.
 
Mark Parent
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is that the Boyarsky and Selikoff book? Have you looked here whether that has been reported before?


It is not reported, so I have left a comment

PS Jeanne Boyarsky has tried to reply me:

Technically the exam says to assume code snippets have surrounding code that is ok. So while this wasn’t intentional, you can assume the closing }



But this is firstly opinable because even the book suggests to start checking curly braces, and so on (Tips on Taking the Exam):

“Be very careful on the code examples. Check for syntax errors first: count curly braces, semicolons, and parentheses, and then make sure there are as many left ones as right ones. Look for capitalization errors and other such syntax problems before trying to figure out what the code does.”



And then we can see that it is definitely wrong because, from the book (Multiple-Choice Questions):

On the real exam, when a code listing starts with line 1, it means that you’re looking at an entire source file.


And that exercise starts with line 1, so we are looking at the entire source file.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!