Last Input Code : Java

File Name (Without .java).

Last Output Code :


Online java to javascript converter

This is useful tool which are convert java code to similar javascript code with limited functionality. This is like a transpiler which is convert source to source converter.

How to use this?

That is similar to compile java code in your local system. First put your java code inside input box and mention the file name (Which class is public and define main method).

The file name is important, because we first check whether your code is valid. If it is valid then the process of converting to Java -> JavaScript works.

How its works

This tool are designed to convert OOPS based javascript code. There are following concepts which are used internally to convert code.

Class Syntax : In javascript, class keyword is used to define a custom class. That is similar to java programming. By javascript are no class modifiers such as public private and abstract. For example.

// File : Faster.java
// Abstract class
abstract class Code
{}
// Run class
class Run
{}
// Demo class 
public class Faster
{}

This tool convert above class as normal class. Such us.

// File : Faster.java
// Abstract class
class Code
{}
// Run class
class Run
{}
// Demo class 
class Faster
{}

It is possible to convert class into a function in javascript but we are follow class syntax rule. Note that class modifier is important in java programming language because they mange accessibility of class within the package and outside the package.

Instance variable : In java language are four types of modifiers are used to declare instance variable such as public,static,private and protected. Otherwise By default instance variable is under the package.

Let's seen an example, how to declare and accessing those variables in java and javascript code.

// File : Demo.java
// My class
public class Demo
{
	// Instance variable
	// variable without modifier
	int age;
	// public variable
	public String name;
	// private variable
	private String password;
	// protected variable
	protected String address;
	// Static variables 
	static double pi = 3.145;
	private static int rectangle = 10;
	public static int pincode = 10001;

	public static void main(String[] args)
	{
      	// Access static variable
      	System.out.println(pi);
    	System.out.println(rectangle);
        System.out.println(pincode);
    }
}
3.145
10
10001

This tool is convert public private and static variables into the javascript language, But unfortunately there are no direct way to convert protected variable. Converted code is.

// File : Demo.java
// My class
class Demo
{
	// Instance variable
	// variable without modifier
	age = 0;
	// public variable
	name = null;
	// private variable
	#password = null;
	// protected variable
	address = null;
	// Static variables 
	static pi = 3.145;
	static #rectangle = 10;
	static pincode = 10001;
	static main(args)
	{
		// Access static variable
		console.log(Demo.pi);
		console.log(Demo.#rectangle);
		console.log(Demo.pincode);
	}
}

Demo.main([]);

Put this code into <script type="text/javascript"> // Above code here </script> And test this code into your web browser and view the result on console panel.

Note that, We not provide initial value of variable this tool are assign default value to variable (which is based on its datatype).

1) Note that the tool is capable of custom comment handling. But sometimes at times it doesn't appear because the code is transformed.

2) static variables are defined by static and private variable is prefixed by # symbol. Those variable can used by class name.

More information about class. Above code are work in chrome browser, But some of firefox browser are allowed to declare private variables. (We will providing the better solution of this problem in upcoming version)

Method conversion : This is custom method conversion mechanism. Basically java are use to define a method they are need 3 point.

Such as return type, method name and method paramter. lets see an example of how to converting method.

// File : Demo.java
// My class
public class Demo
{
	public int age;
	public String name;
	public Demo(int age, String name)
	{
		this.age = age;
		this.name = name;
	}
	public void display()
	{
		System.out.println(this.age);
		System.out.println(this.name);
	}
	public static void main(String[] args)
	{
		Demo obj = new Demo(10, "foo");
		obj.display();
	}
}

Converted code.


// My class
class Demo
{
	age = 0;
	name = null;
	constructor(age, name)
	{
		this.age = age;
		this.name = name;
	}
	display()
	{
		console.log(this.age);
		console.log(this.name);
	}
	static main(args)
	{
		var obj = new Demo(10, "foo");
		obj.display();
	}
}
Demo.main([]);

Array Example

// Array example
public class Demo
{
	public static void main(String[] args)
	{
		int[][] a = new int[2][4];
		float[] b = new float[4];
		int[] d = new int[2];
		System.out.println(a[0].length);
	}
}

Converted code.

// Array example
class Demo
{
	static
	main(args)
	{
		var a = Array(2).fill(0).map(() => new Array(4).fill(0));
		var b = Array(4).fill(0.0);
		var d = Array(2).fill(0);
		console.log(a[0].length);
	}
}
Demo.main([]);

Map Example

// HashMap example
import java.util.*;
public class Demo
{
	public static void main(String[] args)
	{
		HashMap < Integer, String > s = new HashMap < Integer, String > ();
		s.put(1, "One");
		s.put(2, "Two");
		s.put(3, "Three");
		for (Map.Entry < Integer, String > entry: s.entrySet()) System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
	}
}

Converted code.

/* This class are provide by kalkicode.com */
class Settlement
{
	static mapEntrySet(m)
	{
		var record = [];
		for (let data of m.entries())
		{
			record.push(
			{
				k: data[0],v: data[1],getKey: function()
				{
					return this.k;
				},getValue: function()
				{
					return this.v;
				}
			});
		}
		return record;
	}
}
class Demo
{
	static
	main(args)
	{
		var s = new Map();
		s.set(1, "One");
		s.set(2, "Two");
		s.set(3, "Three");
		for (const entry of Settlement.mapEntrySet(s))
		{
			console.log("Key = " + entry.getKey() + ", Value = " + entry.getValue());
		}
	}
}
Demo.main([]);

Limitation This tool are design to convert your custom code to javascript code. But it is not sufficient because java support thousands of package and there is no direct way to translate into javascript, So this tool are provide functionality of few package which is basically used in java.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment