So I’m learning Android Dalvik Java. I’ve got an xml parser that records element tags and does different things depending on the tag.
All is good, however when I tried to do an if statement based on a tag I ran into trouble. Here’s an example.
When a button tag is encountered it returns a string of that tag. So <button> results in a return string of “button”. However I try to do a Boolean comparison of the return to “button” it returns false. “button” does not equal “button”.
Thinking maybe some extra character was added by the parser I check the length. Both are 6 characters long. Thinking maybe it’s some weird text encoding thing I split both strings into a char array, and compare the characters at each individual position. All six characters are equal. However all the same characters, in the same order, as a string are not equal? Have I lost my marbles?
Scratching my head I check various string methods and come across String.equals(differentString). string.equals does in fact say they’re equal.
so surmise according to java:
[MethodThatReturnsStringThatSaysButton() == “button”] = false
but
MethodThatReturnsStringThatSaysButton().equals(“button”) = true
So I guess what I’m asking is why did the equals operator (==) fail, but the equals method work? Further does the equals method pose any risk to similar failures?
Also on the flip side this worked good but I don’t see why.
See I don’t know much about opening network connections. What I settled on for my project, is a loop that runs as long as the connection is active. If there’s something to send down the TCP pipe, it does, if there’s incoming it sends it to the message handler. Each loop it checks for incoming or outgoing and sends it on it’s way.
It works great, which worries me. See I didn’t put any code to control it’s speed. Other languages if you let a loop run unchecked it’ll run at full speed and DoS the CPU to hell.
However in java the CPU sets idle unless there’s something to do, and then the loop does it. It works, but I don’t really understand why, and that worries me because what ever is making it work could change, and if I don’t know what’s making it work I can’t do much if stops working, and I can’t put in anything to help the code recover in such a failure.
So I guess what I’m asking is how does java know to throttle the loop unless there’s something to do, and what’s the risk of it not throttling the loop?