• Post Reply Bookmark Topic Watch Topic
  • New Topic

line of code giving me trouble  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I couldn't come up with a better title but there isn't really a topic this falls into maybe references but more so logic I think

anyway I am following Tim's masterclass of Java on Udemy and there is code I can't understand for the life of me here is the code and I'll post a diagram and explanation after the code please tell me where I am going wrong







this is the part of the code which is giving me troubles




lets just say for example we have two stockItems a game and a shirt,pretend we pass the game object in to addStock ofcourse we will be passing a copy of the reference of that object ok so now item refers to the game object,instock equals getOrDefault(item.getName(),item); lets pretend that game is in the map so stockItem now also points to the game object because stockItem = list.getOrDefault(item.name,item), in this case we return the game object and set stockItem = to it



so item and inStock will point to the same object in memory so this (inStock != item) will not get executed

now lets say getOrDefault can't find the item and returns the item inStock equals the item because it was return

so item and instock will still point to the same object?



thanks

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I drew it out on a piece of paper I understand where I went wrong I will post the solution and why I was wrong soon
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleease don't call it  "list" when it's a Map.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I did figure out the problem but never got a chance to come back to it and post the solution a few days pass and the solution is gone from my mind,anyway I'm still not too sure what is happening

is getOrDefault creating a new object if it can't find that object in the Map?

if if not how is this possible because this line of code should never be executed



thanks
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick reply Goran means a lot I'm sorry I'm so confused on this topic and you have explained it so many times but I'll ask a final question

so we will say we have a map of stockItems and this list has a game(items["game"]) and a shirt​(items["shirt]") the map holds references to game and shirt,so lets so we want to add a new stockItem car car does not exist in the map so

StockItem inStock = items.getOrDefault(item.getName(),item);​



so inStock will equal item,item is a reference and points to car so since inStock equals item and item == car this line of code





will not get executed,

but if car is already in the map inStock will still equal item because item == car (item is a reference to car) and item.GetName() will retrieve a reference items["car"] from the list and since both items["car"] and now instock still points to car?

so again





will not get executed?

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:thanks for the quick reply Goran means a lot I'm sorry I'm so confused on this topic and you have explained it so many times but I'll ask a final question


Not sure who you're talking to?


Anyway, in the first case (item does not exist in the map, mapped by its name), yes, you get the item reference returned, and so inStock and item will hold the same reference.

In the second case, where there is something in the Map that has a key of item.getName(), it is quite likely that the references will not be the same.
This is because, when using '==' in Java you are comparing the reference value and not the contents, so they will only be equal if they refer to the exact same object.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry I asked someone on Udemy for help with the question but he didn't understand what I was saying I shouldn't have included that

I probably sounded crazy haha,

anyway much appreciated thanks for the reply,so in the second case stockitem will not equal item?

thanks
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



so in the first case lets pretend car is in the list

item points to car,instock will also point to car


so this line of code won't get executed




in the second case item still points to car,car is not in map so instock = item but item = car so instock == car  because they refer to the same object

so the above line of code still does not get executed


so confused =(
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys I found this a pretty confusing topic but I won't make the same mistake and not put the answer here once it was solved,so just in case somebody else is doing this udemy course and has a problem with this block of code or are in a similar situation they can refer to this page,

ok so I solved thIS problem,assuming we have CORRECTLY implemented the equals and hashcode methods of an object we can only have one object with a particular key,so in this example we use String ,String's equals and hashcode methods have been implemented correctly by the java developers so we don't have to worry about a duplicate entry been added,now since we can't have two string values which are equal ("hello".equals("hello")) in the same hashMap only one will be added so when we already have an object in the hashMap such as car,we set inStock equal to list.getOrDefault(item.getName(),item),sorry if I'm repeating myself so when car is already in the map this getorDefault method will return car from our map and inStock will be equalled to that car object,therefore since item refers to the car object we passed in as the argument instock does not equal item so this line of code 



gets executed,the other case is a lot more simpler to understand if the object we passed in is not in the HashMap we will set inStock equal to that item

so


does NOT get executed we then add this item to the map if it was not already in it or we then update the map if the object was in it,

hope the explanation was ok,hope this helps anyone in the same situation.

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not convinced that you shouldn't use
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Knute

it would be better to use



thanks
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I didn't format that very well

I also forgot to add a question mark
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use
if (! inStock.equals(item))


a == b
Compares references

a.equals( b )
Compares the contents stored at those references.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah that would make more sense

thanks guys =)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!