Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Please could someone explain why the object type in the argument changes the behaviour?

 
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
Please could someone kindly explain why the object type of the equals argument changes the program behaviour? The difference between Object and CollegeStudent alters the program's behaviour to remove a Student from the arraylist.
Please note that the below questions were referenced from the course 'Java Certification : OCA (1Z0-808) Exam Simulation by Udayan Khattry'.

Prints
CollegeStudent[James, 25]
CollegeStudent[James, 27]
CollegeStudent[James, 25]
CollegeStudent[James, 25]



Prints
MastersStudent[James, 27]
MastersStudent[James, 25]
MastersStudent[James, 25]

Thanks for any help that you can give.
 
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you add some comments to the printed output where the output is not what you expected?  Also explain why?
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you add some comments to the printed output where the output is not what you expected?  Also explain why?


Hi, if you look carefully you will see that there is one less record between both data sets in the output
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

there is one less record between both data sets in the output


Why do you think the output should be the same?
What is the difference you are concerned about?
 
Marshal
Posts: 3398
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:

Norm Radder wrote:Can you add some comments to the printed output where the output is not what you expected?  Also explain why?


Hi, if you look carefully you will see that there is one less record between both data sets in the output


You have shown what was printed, but have not said what you were expecting.

In your code, you add 4 elements to the list, and remove 1 element, leaving 3 elements in the list.  The code later iterates through the list, and prints-out details for the 3 elements.
 
Ron McLeod
Marshal
Posts: 3398
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry - after reading your original post again, I see you are asking why the behaviour is different between the two examples, not why does the second example only print details for 3 list elements.
 
Ron McLeod
Marshal
Posts: 3398
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The element reference provided to the ArrayList#remove method is of type Object, so the Object#equals method will be used to determine equality.

If you want to implement your own overriding equals implementation, the method signature will need to be equals(Object obj), otherwise Object's implementation will invoked.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:The element reference provided to the ArrayList#remove method is of type Object, so the Object#equals method will be used to determine equality.

If you want to implement your own overriding equals implementation, the method signature will need to be equals(Object obj), otherwise Object's implementation will invoked.



Ok, but how does equals even get invoked as we are attempting to remove a new object from an arraylist? Please help me to understand, I am missing something important
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how does equals even get invoked  


Read the API doc for the remove() method to see that it uses the equals method to find the object to be removed from the list.
How else would the remove method determine what object to remove?

Uncomment the @Override statements to see which equals method is being used.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have tried the same example for List<File> and it will not remove the file object. Any ideas why please? My code is as follows:



Prints False


I have no toString() method like in the original posting. Is that necessary?
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print what is in the List and also print what you are trying to remove so you can compare them.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are the same i.e. somemadeuppath
 
Master Rancher
Posts: 3786
49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems unlikely that they are exactly the same - in the list, you are listing files contained in the carDocsDir.  But the thing you are trying to remove is the directory that contains the files, not a specific file.  So I think you have something like this:

List of files:

/x/y/x/fileA
/x/y/x/fileB
/x/y/x/fileC

File to remove:

/x/y/z

And the problem is that no file in the list is exactly equal to /x/y/z, so nothing gets removed.

I suggest , as Norm said, printing out what is in the list of files you want to remove, and also printing the thing you want to remove.  And then look very carefully - is the thing you want to remove in the list of files?  Exactly?  Or is there something very similar in the list?  Similar, but not exactly the same?
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am removing the absolute path of the parent directory that contains the files so it has to be the same. I would expect for the directory to be removed?
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The directory is /Users/myname/Documents/workspace/IntelliJ_Projects/MyProject/Cars/Results/Report

carDocsDir.getAbsolutePath()
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at what is in the list.  Is there a entry for the directory?
Print what is in the List so we can see if there is an entry for the directory.
 
Mike Simmons
Master Rancher
Posts: 3786
49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:I am removing the absolute path of the parent directory that contains the files so it has to be the same. I would expect for the directory to be removed?



Unless it was never in the list of files in the first place.  That was the point of my example.  The list of files contains the files in the directory, but not the directory itself.  Yes, you see the directory name printed over and over in the list... but always with some additional file name at the end.   So none of these lines:


/x/y/z/fileA
/x/y/z/fileB
/x/y/z/fileC

is the same as this:

/x/y/z
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Look at what is in the list.  Is there a entry for the directory?
Print what is in the List so we can see if there is an entry for the directory.



The Report folder is what the absolute path points to if its contents differs everytime the program is run will that make a difference e.g. if it has no files when the program is run or if it contains half a dozen on a different occasion?
 
Marshal
Posts: 72036
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:They are the same i.e. somemadeuppath

Do you mean that is what is displayed when you print your List? It isn't a path at all; if you read about Object#toString(), you will find out what it is.

 * * * * * * * * * * * *

Please don't quote the whole of the preceding post; that adds nothing to the discussion and is liable to removal.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies for the delay in response, I have been very busy. I will take another look and get back to you.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Clemonte Johnstone wrote:They are the same i.e. somemadeuppath

Do you mean that is what is displayed when you print your List? It isn't a path at all; if you read about Object#toString(), you will find out what it is.

 * * * * * * * * * * * *

Please don't quote the whole of the preceding post; that adds nothing to the discussion and is liable to removal.



Yes, you are right. It is working now.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:Apologies for the delay in response, I have been very busy. I will take another look and get back to you.



Please can we discuss overriding the toString() method. Usually, toString() prints either the object reference or a primitive as a String. In the above example, what are they doing?
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding the toString() method allows the class's author to return a String that describes what is to be shown for an instance of the class.  For example when printing.
 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Overriding the toString() method allows the class's author to return a String that describes what is to be shown for an instance of the class.  For example when printing.



If that's the case then why does the following program print empty ""?

 
Clemonte Johnstone
Ranch Hand
Posts: 91
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Overriding the toString() method allows the class's author to return a String that describes what is to be shown for an instance of the class.  For example when printing.



Oh an instance of a class only, that's interesting. Thanks a lot the above two points are quite important.
 
Norm Radder
Master Rancher
Posts: 4176
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why does the following program print empty ""?


That makes perfect sense to me.
What would you expect to print for the empty String: ""?
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If that's the case then why does the following program print empty ""?



You're calling toString() on a String. If you want to return your implementation of the toString() method call it on a class instance of _01_Overriding_ToString.
reply
    Bookmark Topic Watch Topic
  • New Topic