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.
TypeScript | Java |
---|---|
|
|
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.
TypeScript | Java |
---|---|
|
|
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:
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
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.
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.
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"));
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.
You can run the generated JavaScript file using Node.js:
node hello.js
This should output:
Hello, World!
- Install Node.js and npm from the Node.js website.
- Install TypeScript globally using the command:
npm install -g typescript
. - Verify the installation with the command:
tsc --version
. - Create a TypeScript file (e.g.,
hello.ts
). - Compile the TypeScript file using the command:
tsc hello.ts
. - 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:
- Visit the Official Oracle Website: Go to the Oracle JDK download page.
- 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.
- Download the Installer: Choose the appropriate installer for your operating system (Windows, macOS, or Linux) and download it.
For Windows:
- Run the Installer: Double-click the downloaded
.exe
file to run the installer. - Follow the Installation Wizard: Follow the prompts in the installation wizard. You can choose the default settings or customize the installation directory.
- 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:
- Run the Installer: Double-click the downloaded
.dmg
file and follow the instructions to install the JDK. - 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:
-
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
-
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
-
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.
- Download the JDK:
- Visit the Oracle JDK download page.
- Choose the appropriate installer for your operating system and download it.
- Install the JDK:
- Run the installer and follow the prompts.
- Set the
JAVA_HOME
environment variable.
- Verify the Installation:
- Open a terminal or command prompt and run
java -version
.
- Open a terminal or command prompt and run
By following these steps, you should have Java installed and be able to run Java applications on your machine.