I have it working now thank you all for the replies, i obviously still have a lot of work to do practicing the fundamentals.
DevFred i like the data-driven approach it seems a lot tidier than my list of if statements and i think i will do another version with this approach to try and help my understanding further.
TicTacToe array problem
Quote:Original post by nobodynews
edit: I don't know Java didn't let you compare strings like that mentioned by rip-off so that would need to be fixed as well.
Java string literals actually end up as String objects, so == compares their addresses just like it does for other objects. This is not a problem if both strings being compared were created as a string literal, because all string literals in Java are automatically interned - two string literals with the same text will always point to the same String object.
Quote:Original post by lightbringerQuote:Original post by nobodynews
edit: I don't know Java didn't let you compare strings like that mentioned by rip-off so that would need to be fixed as well.
Java string literals actually end up as String objects, so == compares their addresses just like it does for other objects. This is not a problem if both strings being compared were created as a string literal, because all string literals in Java are automatically interned - two string literals with the same text will always point to the same String object.
Even still, you shouldn't use == to compare strings unless you can establish they are both interned as an invariant. In a situation like this where the data may end up coming from a file or somewhere more bizarre I would side with caution.
An enum would be the superior solution even to an interned string, they are by design always interned, as well as type safe.
Quote:Original post by rip-off
Even still, you shouldn't use == to compare strings unless you can establish they are both interned as an invariant. In a situation like this where the data may end up coming from a file or somewhere more bizarre I would side with caution.
Wasn't trying to recommend it, just explaining what's going on. Interned Strings are great to use with IdentityHashMap for constant-time lookups for instance. I agree that enum makes more sense here, especially since the domain is well-defined (x, o, null, that's it). For situations where you need a lot of flexibility (for example you want to let the end user pass in his own message types in script without modifying the system), strings of course are a better choice.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement