a couple of java weirdness questions, including when "button" != "button"

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?

This is a wild guess since I haven’t coded in Java for at least 3 years. But when you get this kind of result it usually is because you have two objects that have the same value. When you compare them they are not equal, because they are not the same object. But they happen to contain the same value. IIRC a String is an object in Java so you to use a method to compare the values, not ==. I didn’t take the time to look this up to confirm but it may give you another avenue to check.

I checked and I was correct. A Java String is not a built-in data type, it is a class. When you use == on two Strings, you are checking to see if they are the same object. To compare the values of two Strings, you have to use the method “equals” as in s1.equals(s2).

Ahh I see!This java thing has been a huge learning experience. Thanks for solving that mystery! It was hitting surreal levels of weirdness.

You might want to check out the Object.equals() documentation for more on == vs equals.

For the networking question, I think you need to provide more information – specifically, which classes/methods you’re using. (I assume you’ll be able to locate them in the Android API; you can also find the Object.equals() there.) I’ve only taken a cursory look at android; they may very well have some combined wait and available functionality built into the send/receive methods.

Well thianks for the reply. I feel like a bit of a heel. I was putting in comments to post the network code to show you what I’m on about, when low and behold buried in the bottom of the function was a Thread.sleep. Apparently past me did put code in to throttle it and I forgot.

Yeah, that happens. Way more often than it should. :slight_smile:

If you’re interested in “doing things the right way”, you might want to take a closer look at the java.nio and java.nio.channels packages and use them rather than Thread.sleep. However, IMHO, this is one area where the criticism “java seems to do things in the most convoluted way possible” holds true – the minimum required number of and interplay among classes/methods was always more than I wanted to deal with. So, like you, I just used Thread.sleep in a loop, didn’t experience substantial performance issues, and was happy. :slight_smile:

Just be aware that a platform has a minimum time slice*; sleeping less than that doesn’t have an effect, sleeping more may degrade responsiveness and/or performance.

*E.g., IIRC, minimum time slice was 100ms on the Intel PIII I was using when I realized it was platform dependent and might affect what I was doing. Other hardware may have nanosecond-level capability (evidenced by the Thread.sleep(millis, nanos) method), and I have no idea how coarse- or fine-grained timers are on android devices.