When comparing Java vs Nim, the Slant community recommends Nim for most people. In the question“What is the best programming language to learn first?” Nim is ranked 22nd while Java is ranked 23rd. The most important reason people chose Nim is:
There are generics, templates, macros in Nim. They can allow you to write new DSL for your application, or avoid all boilerplate stuff.
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 Great metaprogramming features
There are generics, templates, macros in Nim. They can allow you to write new DSL for your application, or avoid all boilerplate stuff.
Pro Strict typing
Checks your code at compile time.
Pro Has built-in unittest module
With built-in "unittest" module you can create test with a very readable code.
Pro Compile-time execution
Nim has a built-in VM, which executes macros and some other code at compile time. For example, you can check if you're on Windows, and Nim will generate code only for it.
Pro Has built-in async support
Nim has "asyncdispatch" module, which allows you to write async applications.
Pro Really cross-platform
The same code can be used for web, server, desktop and mobile.
Pro Easy to read
Nim has a lot of common with Python in terms of syntax. Indentation-based syntax, for/while loops.
Pro Multi paradigm
Imperative, OOP, functional programming in one language.
Pro Easy to integrate with another languages
You can use Nim with any language that can be interfaced with C. There's a tool which helps you to create new C and C++ bindings for Nim - c2nim.
Also, you can use Nim with Objective C or even JavaScript (if you're compiling for these backends).
Pro Garbage-collected
You don't need to deal with all those manual memory allocations, Nim can take care of it. But also you can use another GC, or tweak it for your real-time application or a game.
Pro Type interferencing
You only need to specify types in your procedures and objects - you don't need to specify type when you're creating a new variable (unless you're creating it without initialization).
Pro Built-in Unicode support
You can use unicode names for variables, there is "unicode" module for operations with unicode.
Pro Supports UFCS (Unified Function Call Syntax)
writeLine(stdout, "hello") can be written as stdout.writeLine("hello")
proc add(a: int): int = a + 5 can be used like 6.add.echo or 6.add().echo()
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.