6 ms·
"Undefined behaviour" has a specific technical meaning. In Go, data races can result in UB. In Java, they can't. Java may be non-deterministic, or exhibit unin
by Diggsey 5y ago
"Undefined behaviour" has a specific technical meaning. In Go, data races can result in UB. In Java, they can't.
Java may be non-deterministic, or exhibit unintended behaviour in response to a race condition. UB is strictly worse because it could do either of those things... or indeed anything else, segfaulting being the least of your concerns.
- ngrilly 5y agoThis is the "technical meaning" of "undefined behaviour" according to Wikipedia: > Undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres. Can we predict the behavior of a Java program with a data race?
- anderskaseorg 5y agoA section of Java code with a data race can’t break anything that the same section of code couldn’t have been rewritten to intentionally break without using a data race. For example: it might throw an exception, loop infinitely, or return valid values that the programmer didn’t expect; but it cannot corrupt the internal state of the JVM, violate type-safety, fiddle with private fields, or access data that isn’t reachable from its own variables. Data races cannot be used to violate a security barrier between mutually untrusting sections of Java code.
- ngrilly 5y agoYou're right. That's a big advantage of the JVM. It's memory safe even in presence of data races. That post from Russ Cox (again) explains quite well why Go is not memory safe in presence of data races, and what should be changed to fix this: https://research.swtch.com/gorace https://research.swtch.com/gorace
- UncleMeat 5y agoThis depends on what "unpredictable" means. Do I know precisely what this java program will compute? No. But I do know some things that it won't do. Its behavior is not completely unbounded.
- Dylan16807 5y agoThat's a really bad standalone definition. It would apply to randomly generating numbers, or most attempts at allocating memory and then sorting by address.
- Diggsey 5y agoThat's less a definition, and more a description of what UB looks like. Here's a better definition: https://en.cppreference.com/w/cpp/language/ub https://en.cppreference.com/w/cpp/language/ub