Skip to content

Tabares/from-typescript-to-java

Repository files navigation

🖥️ Introduction

The current guide was desined to understand the sightly differences between JavaScript and Java, recall each implementation as study notes and quick document to migrate to JavaScript or Java.

However to increase compability and provide smooth learning and memory retantion, TypeScript is required; to matching types implementation in both sides. Also Object Oriented Paradigmn basic kwnlodege is required.

This guide is using a table to compare code, to verify the diferences in each language, this samples are provinding the final output; that would be equal for each case.

TypeScriptJava

for (let i = 0; i < 5; i++) {
  console.log(i);
}
/*
0
1
2
3
4
*/

for (int i = 0; i < 5; i++) {
   System.out.println(i);
}
/*
0
1
2
3
4
*/

To run Java code in a proper way please create and use a "main" class (you can rename this class with a proper name) this is the entry point of any Java application. It contains the `main` method (this method name is required in order to execute), which is the method that the Java Virtual Machine (JVM) calls to start the execution of the program. The `main` method must be declared as `public`, `static`, and `void`, and it must accept a single argument: an array of `String` objects.

Here is a simple example of a main class in Java:

samples/
├── Car.java
public class Car {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
          System.out.println(i);
        }
    }
}

Each sample of this guide is verifiable by running the code in their respective bash instructions.

TypeScriptJava

PS C:\samples> tsc Car.ts
PS C:\samples> node Car.js
Model: Unknown, Year: 0
Model: Honda, Year: 2025
Model: Toyota, Year: 2022

PS C:\samples> javac Car.java
PS C:\samples> java Car
Model: Unknown, Year: 0
Model: Toyota, Year: 2022
Model: Honda, Year: 2025

INSTALLATIONS

Typescript

To install TypeScript, you need to have Node.js and npm (Node Package Manager) installed on your machine. Here are the steps to install TypeScript:

1. Install Node.js and npm

If you don't have Node.js and npm installed, you can download and install them from the official Node.js website: Node.js Download

2. Install TypeScript

Once you have Node.js and npm installed, you can install TypeScript globally using npm. Open your terminal or command prompt and run the following command:

npm install -g typescript

The -g flag installs TypeScript globally, which means you can use the tsc command from any directory on your machine.

3. Verify the Installation

To verify that TypeScript has been installed correctly, you can check the version of TypeScript by running the following command:

tsc --version

This command should display the version of TypeScript that was installed.

4. Create a TypeScript File

You can create a TypeScript file with the .ts extension. For example, create a file named hello.ts with the following content:

function greet(name: string) {
    return `Hello, ${name}!`;
}

console.log(greet("World"));

5. Compile the TypeScript File

To compile the TypeScript file into JavaScript, use the tsc command followed by the filename:

tsc hello.ts

This will generate a hello.js file in the same directory.

6. Run the JavaScript File

You can run the generated JavaScript file using Node.js:

node hello.js

This should output:

Hello, World!

Summary

  1. Install Node.js and npm from the Node.js website.
  2. Install TypeScript globally using the command: npm install -g typescript.
  3. Verify the installation with the command: tsc --version.
  4. Create a TypeScript file (e.g., hello.ts).
  5. Compile the TypeScript file using the command: tsc hello.ts.
  6. Run the generated JavaScript file using Node.js: node hello.js.

By following these steps, you should have TypeScript installed and be able to compile and run TypeScript code.

Java

o install Java on your machine, follow these steps:

1. Download the Java Development Kit (JDK)

  1. Visit the Official Oracle Website: Go to the Oracle JDK download page.
  2. Choose the JDK Version: Select the version of the JDK you want to download. The latest version is recommended, but you can choose an older version if needed.
  3. Download the Installer: Choose the appropriate installer for your operating system (Windows, macOS, or Linux) and download it.

2. Install the JDK

For Windows:

  1. Run the Installer: Double-click the downloaded .exe file to run the installer.
  2. Follow the Installation Wizard: Follow the prompts in the installation wizard. You can choose the default settings or customize the installation directory.
  3. Set the JAVA_HOME Environment Variable:
    • Open the Start menu and search for "Environment Variables."
    • Click on "Edit the system environment variables."
    • In the System Properties window, click on the "Environment Variables" button.
    • Click "New" under System variables and add a new variable named JAVA_HOME with the path to your JDK installation directory (e.g., C:\Program Files\Java\jdk-17).
    • Find the Path variable in the System variables list, select it, and click "Edit."
    • Add a new entry with the path to the bin directory of your JDK installation (e.g., C:\Program Files\Java\jdk-17\bin).

For macOS:

  1. Run the Installer: Double-click the downloaded .dmg file and follow the instructions to install the JDK.
  2. Set the JAVA_HOME Environment Variable:
    • Open a terminal window.

    • Add the following line to your shell profile file (e.g., .bash_profile, .zshrc, etc.):

      export JAVA_HOME=$(/usr/libexec/java_home)
    • Save the file and run the following command to apply the changes:

      source ~/.bash_profile

For Linux:

  1. Install the JDK: Open a terminal window and use the package manager for your Linux distribution to install the JDK. For example, on Ubuntu, you can run:

    sudo apt update
    sudo apt install openjdk-17-jdk
  2. Set the JAVA_HOME Environment Variable:

    • Open a terminal window.

    • Add the following lines to your shell profile file (e.g., .bashrc, .zshrc, etc.):

      export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
      export PATH=$PATH:$JAVA_HOME/bin
    • Save the file and run the following command to apply the changes:

      source ~/.bashrc

3. Verify the Installation

To verify that Java has been installed correctly, open a terminal or command prompt and run the following command:

java -version

This command should display the version of Java that was installed.

Summary

  1. Download the JDK:
  2. Install the JDK:
    • Run the installer and follow the prompts.
    • Set the JAVA_HOME environment variable.
  3. Verify the Installation:
    • Open a terminal or command prompt and run java -version.

By following these steps, you should have Java installed and be able to run Java applications on your machine.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •