Skip to main content

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];

Converted Python

class MyArray :
    def main( args) :
        # Simple array of integer elements
        a = [1, 2, 3, 4, 5]
        b = [[1, 2, 3], [4, 5, 6]]
        c = [0] * (10)

if __name__=="__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
      	// ABCdec
  		// ABC
        // abcdec

Converted Python

class MyString :
    def main( args) :
        a =  "Abc"
        b = "ABCdec"
        # Abc
        # ABCdec
        # ABC
        # abcdec

if __name__=="__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}
		// 3

Converted Python

class MyHashMap :
    def main( args) :
        hm =  dict()
        hm["One"] = 1
        hm["Ten"] = 10
        hm["Year"] = 2022
        # {Year=2022, One=1, Ten=10}
        # 3

if __name__=="__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)
    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

Hhdh     117 Day ago
Doesn't work well with functionals and array instantiation.

import java.util.*;
class Test
    public static void main(String[] args) {
        List<Long> a = new ArrayList<Long>(Arrays.asList(new Long[] {1L,2L}));

MANTOSH KUMAR     171 Day ago
Please convert the following JAVA code to PYTHON code the description of the files has been provided below as a link

>:(     207 Day ago
this program is dumb andruins your javascript please dont use this 
instead use this convert that can actual transfer in between files, 
(only under the "(..)" rule anything other would be invalid.
Kalkiteam     244 Day ago
It is capable, but it is designed to work with a single file to reduce the load on the server. If you use more than one file it will take more time. 
John     245 Day ago
Seriously? This can't handle dependence between files?
John     245 Day ago
Seriously? This can't handle dependence between files?
Kalkiteam     247 Day ago
It is very important to test for syntax errors, which detect valid code, before starting to convert the code. This is done so that your code can be changed without interruption.
By :
Nag     248 Day ago
Why do you compile the java class when converting in to python. How can you expect all import packages are available to you.
Pp021     301 Day ago
// file : XORCrypt
public class XORCrypt {
	private static String key = "o23sSendevia";

	public static String encrypt(String str) {
		int[] output = new int[str.length()];
		for (int i = 0; i < str.length(); i++) {
			int o = (Integer.valueOf(str.charAt(i)) ^ Integer.valueOf(key.charAt(i % (key.length() - 1)))) + '0';
			output[i] = o;
		String out = "";
		for (int enc : output) {
			out += enc + "'";
		return out.substring(0, out.length() - 1);

	public static String decrypt(String input) {
		String output = "";
		String[] str_split = input.split("'");
		int[] split = new int[str_split.length];
		for (int i = 0; i < str_split.length; i++) {
			try {
				split[i] = Integer.parseInt(str_split[i]);
			} catch (NumberFormatException e) {
				new RuntimeException(input, e);
				return input;
		for (int i = 0; i < split.length; i++) {
			output += (char) ((split[i] - 48) ^ (int) key.charAt(i % (key.length() - 1)));
		return output;
// Write note here
on decrypt method it has same variable on the loop please fix :)
take some min to fix the code but still useful