When comparing Java vs Rust, the Slant community recommends Rust for most people. In the question“What is the best programming language to learn first?” Rust is ranked 21st while Java is ranked 23rd. The most important reason people chose Rust is:
Since Rust is statically typed, you can catch multiple errors during compile time. This is extremely helpful with debugging, especially compared with dynamically typed languages that may fail silently during runtime.
Specs
Ranked in these QuestionsQuestion Ranking
Pros
Pro Consistent programming standards
Most Java code follows very standardized coding styles. This means that when you're starting out, there are fewer questions about how you should implement something as the programming styles and patterns are well established and consistent. This consistent style means that it's often easier to follow others' example code, and that it's more likely to meet at least a certain minimum standard of quality. This discipline with consistent stylistic standards also becomes useful later, when collaborating on projects with larger teams.
Pro Massive amount of libraries and APIs
Java has been around for such a long time that there have been tens of thousands of APIs and libraries written for almost anything you want to do.
Pro Most commonly used language in industry
Java is one of the most popular languages in industry, consistently ranking either first, or occasionally second (behind C or Javascript) in terms of usage. Polls (see sources below) show it to be consistently in high demand, particularly as measured by job board postings. This makes Java a great time investment, as you will be easily able to get a job utilizing your skills, particularly as those Java applications in production now will continue to need maintenance in the future. It also results in great support for tools and plenty of computer science books, example projects and online tutorials.
Pro Fantastic IDEs
Because Java is statically typed, integrated development environments (IDEs) for Java can provide a lot more feedback on errors you will encounter. Java IDEs can give you specific errors in the location where they occur without having to run the code every time. This makes is faster to debug and learn from your mistakes.
IDEs also have extensive auto complete capabilities that can help you learn the programming libraries you are using faster and tell you what functions are available.
Pro Introduces you to object oriented languages
Object Oriented Programming (OOP) is a paradigm that teaches you to split your problem into simpler modules with few connections between them; it's the most common paradigm used in industry. Java is the best choice as an introduction to object oriented languages because, as a statically-typed OOP-only language, it very clearly highlights core OOP principles such as encapsulation, access control, data abstraction, and inheritance.
While a scripting language provides more flexibility and terseness, learning a scripting language first would not instill these fundamental concepts as well, as they tend to obscure details such as how types work, and are less encouraging of an object oriented style.
Pro Best introduction to "C style" languages
The Java syntax is very similar to other C style languages. Learning the fundamentals of Java will port over well to other languages so you can apply what you've leaned to other languages afterwards.
Pro Platform Independent
Because of the Java Virtual Machine, the Java programming language is supported wherever a JVM is installed.
Pro You know what you do, and still simple
Because everything is typed and there is no silent cast or fail, you exactly know what you are manipulating, and there's no magic.
Have you ever watched a beginner struggling with a "couldn't call method x on y" in python or Javascript? These languages don't teach you what proper types are.
Java is one of the simplest languages to learn these basic concepts you find in every programming language.
Pro Catch errors at compile-time
Since Rust is statically typed, you can catch multiple errors during compile time. This is extremely helpful with debugging, especially compared with dynamically typed languages that may fail silently during runtime.
Pro Compiles to machine code allowing for extra efficiency
Rust uses LLVM as a backend, among other things this allows Rust code to compile down to machine languages. This allows developers to write programs that run as efficiently as possible.
Pro Threads without data races
Unique ownership system guarantees a mutable data to be owned and mutated by only one thread at a time, so there's no data race, and this guarantee is checked at compile time statically. Which means easy multi-threading.
Of course, immutable data can be shared among multiple threads freely.
Pro Generics support
You don't have to write same array and dictionary classes hundreds and thousands times for strong type check by compiler.
Pro Built-in concurrency
Rust has built-in support for concurrency.
Pro Supports cross compilation for different architectures
Since Rust 1.8 you can install additional versions of the standard library for different targets using rustup/multirust
.
For example:
$ rustup target add x86_64-unknown-linux-musl
Which then allows for:
$ cargo build --target x86_64-unknown-linux-musl
Pro Makes developers write optimal code
Rust is a modern programming language written around systems. It was designed from the ground up this way. It's language design makes developers write optimal code almost all the time, meaning you don't have to fully know and understand the compiler's source code in order to optimize your program.
Furthermore, Rust does not copy from memory unnecessarily, to give an example: all types move by default and not copy. Even references to types do not copy by default. In other words, setting a reference to another reference destroys the original one unless it's stated otherwise.
Pro Support for macros
When you identify a part of your code which gets repeated often, which you cannot abstract using functions or classes, you can use Rust's built-in Macros.
Pro Official package manager and build tool
Cargo is the official package manager for Rust. It comes with the language and downloads dependencies, compiles packages, and makes and uploads distributable packages
Pro Easy to write understandable code
While not as verbose as Java, it still is much more verbose than languages like Go and Python. This means that the code is very explicit and easy to understand.
Pro Big community
The biggest community contributing to language.
Pro Functional programming
Very easy to create functional with some additional from structure application.
Pro Excellent syntax
Pro Safety
Pro No GC
Pro Low memory usage
Pro Static compiled & statically linked single file output
Pro Extremely fast code execution
Pro Zero-cost futures or Async IO
Cons
Con Too verbose
- A Hello world needs package, class, static method and the actual
printf
. - Reading a line from input requires instatiating 5 objects in the right order.
- Exceptions are everywhere, particularly since all values are nullable.
- Java has a getter/setter culture, but without native syntax support.
- portable Java code lacks anonymous functions, and continues to lack good support for partial application, compensating instead with verbose design patterns, kludges like anonymous inner classes, or just inline code.
- It is statically typed without type inference, with a culture that promotes long class names.
- Poor support for sum-types and pattern matching leads to overuse of inheritance for dynamic dispatch and chains of nested conditionals
Especially for beginners, this can make reading Java code feel overwhelming; most Java courses tell students to simply copy, paste, and ignore a significant percentage of the code until they've learned enough to understand what it means.
For experienced programmers, this makes Java feel tedious, especially without an IDE, and actively discourages some solutions and some forms of abstraction.
Con Confusing mis-features
Some features in Java can be quite confusing for beginners.
Encapsulation is needlessly obfuscated with a confusing access control model. As an example, the "protected" keyword not only grants access to child classes, but to the entire package. Since small programs are written as one package, it becomes functionally equivalent to "public".
In OOP, everything is supposed to be an object, but, in Java, primitive types such as integers, booleans and characters are not, and must be handled as special cases.
Java continues to lack many high-level features, and, particularly prior to Java 7, compensated by adding confusing Java-only features, such as anonymous subclasses. Some example code is unreadable without knowing a special-case feature, libraries differ in style based on when they were released or what platform they target(e.g., Android vs. Desktop), and some solutions just aren't available on some platforms.
Con Locks you into the static OOP mindset
Overly focuses on class-based OOP to the detriment of programmer freedom or alternative paradigms that are better for various problems. Traps programmers into an always use class-based OOP mindset.
Con Half-baked generics
Type erasure means it doesn't even exist at runtime. The whole generics system is confusing for beginners.
Con Slow and heavy
Too far from the machine hardware.
Con Too much hype and useless complication
I've been developing in Java for 20 years and I love this language, I've never used a better designed one over the last 30+ years. However, starting with version 6, the number of really useful features in each release has steadily decreased. There is now too much marketing in Java's evolution and too few concern about developers' needs (the functionalities of the applications we develop haven't changed and enterprise application architecture is much simpler than 20 years ago). I'm deeply sad to say that today, learning Python is a much wiser choice.
Con Garbage collection may teach bad habits
Java is a garbage collected language and it does not force programmers to think about memory allocation and management for their programs. This is fine most of the time. However, it may cause some difficulties in adjusting to a non-GC language (such as C for example), where memory management needs to be done manually. But if good coding practices and habits are followed, this shouldn't be much of a problem.
Con Long learning curve
Con Worst-of-both-worlds static type system
It's just barely good enough to make decent IDEs, but it's not at the level of Idris or even Haskell. For large enterprise projects, the IDE support is important, but the static typing in Java just gets in the way for the smaller projects beginners would start with.
Python is duck typed and this makes small programs easy to develop quickly, but the price is that you have to write unit tests to avoid breaking larger programs. In contrast, you can be reasonably certain that a program that actually compiles in Idris does what you want, because assertions are built into the powerful type system. Java can't make that claim and still requires unit tests. Java has the worst of both worlds because of its poor static type system.
Con Static typing but no type inference
The type system gets in your way more than it helps. Heavy IDE support is absolutely required for reasonable productivity. This means beginners have to learn not just the language, but eventually a complex, heavyweight IDE too.
Con Lacks modern features
Java evolves very slowly - lambda expressions weren't available until Java 8 (which is not available on Android), and despite getters/setters being a long-time convention, the language still doesn't have native accessor syntax (a la C#'s properties). It's unlikely newer, popular features like list comprehensions or disjoint union types will be available anytime soon. While not strictly required for novice programmers, these make problems more complicated and tedious than they need to be - for example, when a simple local function would do, (portable) Java demands anonymous inner classes, an interface and a class, or worse, no abstraction at all.
Con Anything java can do, C# does better and more elegant.
Since C# came later, it could avoid the blatant mistakes made in Java.
Con Enforces some misguided principles
Java utilizes principles that primarily organize code into "classes" as the central concept, instead of more familiar organizational methods.
Con Checked exceptions
Checked exceptions add significantly to the cognitive load of the beginner. The more rules unrelated to the actual task that you pile onto the beginner, the slower he gets. Exponentially. And for what? Sure, they look good on paper, but checked exceptions are useless in the real world. They don't scale.
When you're calling APIs five levels deep and your method's throws clause balloons to 80 exceptions you might see, it gets kind of ridiculous. So you just say throws Exception
, which defeats the whole feature, but is more pointless boilerplate in an already tediously verbose language.
Or worse, while developing you can't be bothered and just catch{}
and silently swallow them all. Then you forget about it. Now you don't even have exceptions. The cure is worse than the disease!
So the wiser Java programmers will wrap all checked exceptions into a runtime exception, effectively making exceptions unchecked. That's at the cost of more boilerplate, but it's the lesser of evils. Unfortunately, Java doesn't even have macros to do this part for you. Maybe your IDE can write code templates for you, but that doesn't make it any easier to read.
Con Long compile times
Way longer to compile than other languages.
Con Low productivity
The compiler is strict, such as ownership and borrowing checkers.
Con Low readability
Harder to read and understand language.
Con Very ugly and verbose syntax
Compared to many languages. One tool needed to be productive is the language to be pleasing and effective, yet Rust friction is as high as its safety.
Con Steep learning curve
Pretty strict and hard to adapt for beginners.
Con Not as useful or productive as advertised.
If really out here wanting to use something besides C/C++, in 98% of use cases, would do just as well or be better off using something other than Rust. Will complete the project faster, would be easier, will be happier, and nearly as safe (use newer language with optional GC).
Con Rust not as safe as it pretends to be
Rust problems:
1) Massive undisclosed usage of unsafe.
2) Massive amounts of memory safety CVEs - see details.
3) Large bloated Rust binaries increase attack surface for exploits.
4) Many corporate types claiming Rust is safe, don't actually program or use it (have some quiet financial interest to push it).
Con Significant time required to understand the language fully
There's the infamous borrow checker for example.
Con Low-level programming language
This means that it encourages the programmer to be very careful in terms of how memory is allocated, etc.
Most applications can run without exceeding the capacity of the server, even with an inefficient dynamic scripting language.