Java vs TypeScript
When comparing Java vs TypeScript, the Slant community recommends TypeScript for most people. In the question“What is the best programming language to learn first?” TypeScript is ranked 6th while Java is ranked 23rd. The most important reason people chose TypeScript is:
Typescript has optional static typing with support for interfaces and generics, and intelligent type inference. It makes refactoring large codebases a breeze, and provides many more safeguards for creating stable code.
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 Optional static typing
Typescript has optional static typing with support for interfaces and generics, and intelligent type inference.
It makes refactoring large codebases a breeze, and provides many more safeguards for creating stable code.
Pro Strong typed language
Lot of benefits of it, you can read this.
Pro Strict superset of Javascript
Every existing Javascript program is already a valid TypeScript program giving it the best support for existing libraries, which is particularly useful if you need to integrate with an existing Javascript code base.
Pro First party Visual Studio support
As a Microsoft developed project, it has first party Visual Studio support that's on par with its C# support with features like syntax sensitive statement completion.
Pro Has a repository of high quality TypeScript type definitions for popular libraries
There are many ready to use and high quality TypeScript definitions for popular libraries including jquery, angular, bootstrap, d3, lodash and many-many more.
Pro Adds support for object-oriented programming
Typescript enables familiar object-oriented programming patterns: classes, inheritance, public/private methods and properties, et cetera.
Pro Polyfill for ES6 fat-arrow syntax
Typescript implements the fat arrow syntax, which always maintains the current context for this
and is a shorter/more convenient syntax than traditional function definition.
Pro Great support for React, integrated typed JSX parsing
Strongly typed react components, so UI "templating" automatically gains type safety.
Pro Great support for editors (Sublime, Code, Vim, IntelliJ...)
Pro Works well with existing Javascript code
Both can call Javascript code and be called by Javascript code. Making transitioning to the language very easy.
Pro Compiles to very native looking code
Compiles to simple looking Javascript making it easy to understand what is happening and learn the language (if you already know Javascript).
Pro Built and supported by Microsoft
Being built by Microsoft, TypeScript is much more likely than most other similar open-source projects to receive continued long-term support, good documentation, and a steady stream of development.
Pro Ability to do functional programming
Pro Clear roadmap
TypeScript has a clear and defined roadmap with rapid and constant releases.
Pro Low number of logical errors brought in by built-in type annotations
TypeScript's built-in type signatures allow developers to fully document interfaces and make sure that they will be correctly compiled. Therefore, cutting down on logical errors.
Pro Works well with Angular 2
Angular 2 is built using TypeScript and applications built using it can make use of that (or not).
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 Too similar to Javascript
Presents some advantages compared to Javascript, but because it is designed to be a superset of Javascript, it means all the bad parts of Javascript are still present.
Con Type checking not enforced by default
You have to use compiler flags to make sure it catches flaws like usage of implicit any, etc.
Con Type inference coverage is incomplete
The default type when declaring and using a variable is any
. For example, the following should break but does not:
function add(a:number) { return a + 1 }
function addAB(a, b) {return add(a) + b}
addAB("this should break but doesn't :(", 100)
In order to avoid this, you have to declare type signatures for every variable or parameter or set the flag --noImplicityAny
when running the compiler.
Con Requires "this" for field access
Even in cases were there is no ambiguity, you still have to use "this.fieldName" instead of just "fieldName".
Con Syntax is too verbose
Con No support for dead code elimination
Typescript compiler does not remove dead code from generated file(s), you have to use external tools to remove unused code after compilation. This is harder to achieve, because Typescript compiler eliminated all type information.
Con No support for conditional compilation
There is no clean way to have debug and release builds compiled from the same source, where the release version removes all debugging tools and outputs from the generated file(s).
Con Awful error messages
Comparing to Elm or Rust for example, TypeScript's error messages won't say you very much. For example if you change method of interface which your class implements it won't say your class have incorrect implementation. Instead it'll show error in usage of instances of class. In some cases it can spoil hours of your work trying to figure out why your parameters are incorrect.
Con Technical debt
As consequence of not enforcing type checking.
Con No Java-like package structure
If you prefer a Java-like approach of partitioning your code into different packages, the module system of typescript will confuse you.