Getty Images/iStockphoto

Tip

The differences between Java and TypeScript devs must know

Do you know Java? Are you trying to learn TypeScript? Here are five differences between TypeScript and Java that will make the learning transition easier.

TypeScript is growing in popularity as the programming language of choice for both front-end and back-end developers.

With TypeScript, developers can fully wield the power of the principles and practices of object-oriented programming (OOP). Meanwhile, they can build on their experience with JavaScript, the language from which TypeScript is derived.

Typescript is a good fit for Java programmers who want to branch out into another language but still leverage their expertise with OOP.

Here are the five things every Java developer who's learning TypeScript needs to know:

  1. TypeScript is just as OOP as Java.
  2. There are some differences in syntax between TypeScript and Java.
  3. TypeScript compiles differently from Java.
  4. The TypeScript component library for NodeJS is NPM.
  5. You'll need to find a good TypeScript IDE.

1. TypeScript is just as OOP as Java

TypeScript was intended to be an OOP language from the start. All the standard features of OOP available in Java are in TypeScript.

First and foremost, TypeScript is a strict type language. As with Java, TypeScript establishes the type of a variable at design time, when the variable is declared. The following is an example of strict type declaration in TypeScript, which declares the variable firstName as a variable of type string:

let firstName: string

The main features of Java
Among Java's main selling points are that it's object-oriented, with a similar syntax to C++, and its code is comprehensive yet flexible.

As with Java, under TypeScript classes and interfaces are first-class building blocks of the language. You can declare the scope of class variables and methods as public, protected and private. Java has supported this style of scoping since its inception.

Finally, TypeScript lets you apply decorators to classes, methods, accessors, properties and parameters. A decorator can impose behavior on a class, method, accessor, property or parameter in a declarative manner by using the given decorator prefixed with the @ symbol, as shown in the following example that uses a decorator name @sealed:

@sealed
class BugReport {
 type = "report";
 title: string;
 
 constructor(t: string) {
  this.title = t;
 }

}

The Java equivalent of a TypeScript decorator is an annotation.

2. There are some differences in syntax between TypeScript and Java

Both TypeScript and Java are OOP languages, but there are differences in their syntax. The following example declares a variable named firstName in Java:

String firstName = "John";

The next example declares firstName in TypeScript:

const firstName: string = 'John';

As you can see, the examples above have the same intent: declare a variable named firstName of type String and then assign the value "John'' to the variable. The difference is in the syntax of each expression.

Java programmers coming to TypeScript must take some time to learn the TypeScript syntax. TypeScript syntax isn't that hard to learn; it's just a bit different from Java syntax.

3. TypeScript compilation is different than Java compilation

Both Java and TypeScript are compiled languages. This means that a compiler inspects the text of source code to make sure the code is syntactically correct and then converts the source code text into a format that a computer at runtime can process.

In Java, source code is compiled into bytecode that is run by the Java Virtual Machine installed on a given computer. In TypeScript, source code is compiled into JavaScript code that is run by the JavaScript runtime. For front-end developers, the JavaScript runtime is a component that's part of the web browser. For back-end developers, the JavaScript runtime is installed on the server that runs the back-end application.

The physical file transformation in Java converts a text file that has the .java file extension into bytecode that has the .class file extension.

Typescript, by comparison, compiles source code text files with the .ts extension into JavaScript code that has the .js file extension.

Compilation steps
Compilation is the process that turns source code into runnable code.

4. The TypeScript component library for NodeJS is NPM

Java programming has matured over the years to the point where there are comprehensive package and build systems to deploy components and libraries that programmers can easily use in their code. Two popular package management and build systems among Java developers are Maven and Gradle. Various repositories that host components to download and install support these package management systems.

TypeScript developers use the NPM package management and build system to create applications intended to run under NodeJS. Publicly available NPM packages are hosted on npmjs.com.

5. Just as in Java, use a good IDE for TypeScript

Any Java or TypeScript developer can write an application using nothing more than a simple text editor. However, most developers use an integrated developer environment (IDE) such as IntelliJ, WebStorm, Eclipse or Visual Studio Code.

The benefit of using an IDE is that developers can write better code, faster. IDEs can detect syntax errors in code at design time, even before the code is compiled. Many IDEs also detect inefficient code and suggest better ways to write the code.

Many IDEs offer a feature called code completion, which offers suggestions for developers to complete a programming statement as the developer writes. Auto-completion can also automatically import dependency statements when you declare a class or interface that's part of an external dependency.

This code completion capability has different names in different IDEs. In Microsoft Visual Studio Code, it's branded as IntelliSense and IntelliCode. Eclipse has a setting called Content Assist, while IntelliJ and Webstorm call their setting code completion.

Java and TypeScript together

TypeScript has a lot to offer Java developers. Coders who understand the intricacies of object-oriented programming can apply what they know to TypeScript immediately. Also, TypeScript offers a way for Java developers to transition into the world of browser-based software development.

There is a learning curve for developers to move from Java to TypeScript. For most, getting the hang of TypeScript is a matter of understanding the syntax. The five tips in this article should make the move easier.

Dig Deeper on Core Java APIs and programming techniques