Java to python converter
This is simple basic alpha version of Java to Python code converter. The main goal of this tool is to provide functionality to deal with converting custom Java code into equivalent Python code.

Example of java to python
Here mentioned few examples which is based on arrays, strings and some useful packages which is generally used in java programming language.
Java array to python list
Java Code
class MyArray
{
public static void main(String[] args)
{
// Simple array of integer elements
int[] a = new int[]{1,2,3,4,5};
int[][] b = new int[][]{{1,2,3},{4,5,6}};
int[] c = new int[10];
System.out.println(a.length);
System.out.println(b.length);
System.out.println(c.length);
}
}
Converted Python
class MyArray :
@staticmethod
def main( args) :
# Simple array of integer elements
a = [1, 2, 3, 4, 5]
b = [[1, 2, 3], [4, 5, 6]]
c = [0] * (10)
print(len(a))
print(len(b))
print(len(c))
if __name__=="__main__":
MyArray.main([])
Java String to python string method
Java Code
class MyString
{
public static void main(String[] args)
{
String a = new String("Abc");
String b = "ABC" + "dec";
// Abc
System.out.println(a);
// ABCdec
System.out.println(b);
// ABC
System.out.println(a.toUpperCase());
// abcdec
System.out.println(b.toLowerCase());
}
}
Converted Python
class MyString :
@staticmethod
def main( args) :
a = "Abc"
b = "ABCdec"
# Abc
print(a)
# ABCdec
print(b)
# ABC
print(a.upper())
# abcdec
print(b.lower())
if __name__=="__main__":
MyString.main([])
Java Hashmap to python dictionary
Java Code
import java.util.HashMap;
class MyHashMap
{
public static void main(String[] args)
{
HashMap < String, Integer > hm = new HashMap < String, Integer > ();
hm.put("One", 1);
hm.put("Ten", 10);
hm.put("Year", 2022);
// {Year=2022, One=1, Ten=10}
System.out.println(hm);
// 3
System.out.println(hm.size());
}
}
Converted Python
class MyHashMap :
@staticmethod
def main( args) :
hm = dict()
hm["One"] = 1
hm["Ten"] = 10
hm["Year"] = 2022
# {Year=2022, One=1, Ten=10}
print(hm)
# 3
print(len(hm))
if __name__=="__main__":
MyHashMap.main([])
How to convert Python to java ?
We were on our way to convert Python to Java. In which we faced some difficulty. That's because, in Python, everyone acts like an object. But Java has its limitations. We can say that the task of converting from Python to Java is not easy, for this you have to keep the following things in mind.
AST Implementation : There are many ways to implement AST (abstract syntax tree), You can use python ast package to implement AST.
import ast code = "print('I am good')" result = ast.parse(code) print(ast.dump(result))
Module(body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Str(s='I am good')], keywords=[]))])
It does not check unnecessary variable methods and packages. Means it doesn't do error detection which is possible in your code.
You can also use ANTLR v4 to generate the AST. This too will lack the same type detection. When you use antlr you will feel that it works slowly. But it has many benefits that it also provides your visitors, grammar and visualization.
Parse Tree Visitor : This is the critical step in which you can control how you represent your code.
Type Detection
Package detection
Syntax Transformation
Class Decoration
Loop transformation
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