r/javahelp • • Oct 31 '25

Homework How are numbers compared as a String?

I'm working on this project, and I'm checking whether something occurs before a specific time. I'm doing this by converting the times to Strings, then comparing them against each other (yes I'm aware it's not ideal, bear with me).
The issue is that it says that '10:00 < 09:00'. Why is that?

0 Upvotes

24 comments sorted by

View all comments

Show parent comment

1

u/GolfballDM Nov 01 '25

At least in ASCII and Unicode, '0' (0x30) is < '1' (0x31).  

1

u/jmat83 Nov 01 '25

I stand corrected. The fact that I’m wrong about the ordering of these values in the character set is, however, a great illustration of why using an appropriate data type and the built in methods of that data type to do comparisons is a better way to do it than converting to a string and then using string comparisons. I don’t have to remember the semantics of the character set. I can just trust that ZonedDateTime works like time and not deal with conversion at all.