Last Input Code : Java

File Name (Without .java).



Last Output Code :

  • Your recently tested file

Files are removed when session is expired.


Online java to python converter

This is very basic and alpha version of java to python code converter. The main goal of this tool are provide functionality to deal with transform custom java code to equivalent python code. We are very happy to have implemented this tool within a week. And hopefully soon we will present its final version in front of you.

How to convert java to python code?

You can do this using our tools. To do this, you have to keep a few things in mind. If you are already used of our other code converter tool, So you can skip this instruction.

1) Your code must be valid Java 8 based code. which you want to convert. (Note that you can use your own compiled Java code, which is run successfully without any errors.)

2) The file name should be the same as your public class method. And only one public class is allowed in your filed. This name is required to be entered in the File name section.

If you are facing any difficulty, you can read our other tutorials. Such as How to convert java to c#.

Code conversion example

We are assume you are know java and trying to convert your code into python. For your understanding, we are presenting some facts which describes the usefulness of this tool.

Class and class method example

Java is pure object oriented programming language. In Python we can use class. This concept allows us to convert a Java class to a Python class. We convert Java's class and its methods into Python.

Java Code

// Define class of linked list Node
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode prev;
    public LinkNode(int data)
    {
        this.data = data;
        this.next = null;
        this.prev = null;
    }
}
public class DoublyLinkedList
{
    public LinkNode head;
    public DoublyLinkedList()
    {
        // Set inital value
        this.head = null;
    }
    // Insert new node at beginning position
    public void insert(int value)
    {
        // Create a node
        LinkNode node = new LinkNode(value);
        node.next = this.head;
        // When linked list is not empty
        if (this.head != null)
        {
            this.head.prev = node;
        }
        // Make new head
        this.head = node;
    }
    // Display node element of doubly linked list
    public void display()
    {
        if (this.head == null)
        {
            System.out.println("Empty Linked List");
        }
        else
        {
            System.out.println("  Doubly Linked List Element :");
            // Get first node of linked list
            LinkNode temp = this.head;
            // iterate linked list 
            while (temp != null)
            {
                // Display node value
                System.out.print("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
    }
    public static void main(String[] args)
    {
        DoublyLinkedList dll = new DoublyLinkedList();
        // Insert following linked list nodes
        dll.insert(70);
        dll.insert(60);
        dll.insert(50);
        dll.insert(40);
        dll.insert(30);
        dll.insert(20);
        dll.insert(10);
        //  NULL <- 10 <--> 20 <--> 30 <--> 40 <--> 50 <--> 60 <--> 70->NULL
        dll.display();
    }
}
/*
    File : DoublyLinkedList
*/

Generated python code


#  Define class of linked list Node
class LinkNode :
    def __init__(self, data) :
        self.data = data
        self.next = None
        self.prev = None
class DoublyLinkedList :
    def __init__(self) :
        #  Set inital value
        self.head = None
    #  Insert new node at beginning position
    def insert(self, value) :
        #  Create a node
        node = LinkNode(value);
        node.next = self.head
        #  When linked list is not empty
        if (self.head != None) :
            self.head.prev = node
        #  Make new head
        self.head = node
    #  Display node element of doubly linked list
    def display(self) :
        if (self.head == None) :
            print("Empty Linked List")
        else :
            print("  Doubly Linked List Element :")
            #  Get first node of linked list
            temp = self.head;
            #  iterate linked list
            while (temp != None) :
                #  Display node value
                print("  " + str(temp.data), end ="")
                #  Visit to next node
                temp = temp.next
    def main(self, args) :
        dll = DoublyLinkedList();
        #  Insert following linked list nodes
        dll.insert(70)
        dll.insert(60)
        dll.insert(50)
        dll.insert(40)
        dll.insert(30)
        dll.insert(20)
        dll.insert(10)
        #   NULL <- 10 <--> 20 <--> 30 <--> 40 <--> 50 <--> 60 <--> 70 -> NULL
        dll.display()


if __name__=="__main__":
    DoublyLinkedList().main([])

We are trying to make your Python code very simple without losing the quality of the converted code. Such as proper comment, function format, instance variable selection etc.

Array example

Array is the most common data structure capable of storing multiple elements. In python it is known as list. In this section mentioned few example which is create some arrays.

public class A
{
    public static void main(String[] args)
    {
        int[] a = new int[4];
        A[][][] b = new A[2][2][3];
        int[] c = {
            1 , 2 , 3
        };
        // Get length
        System.out.println(a.length);
        System.out.println(b.length);
        System.out.println(c.length);
    }
}
// find : A.java

Generated python code

class A :
    def main(self, args) :
        a = [0] * (4);
        b = [[[None] * (3) for _ in range(2)] for _ in range(2)];
        c = [1, 2, 3];
        #  Get length
        print(len(a))
        print(len(b))
        print(len(c))


if __name__=="__main__":
    A().main([])

If you want to study about arrays, then you can see Array and List tutorial.

Switch case example

Switches are basically used to define multiple test conditions in the Java programming language. Python does not have switch case. So we can change the switch case by using (if..else statement). It is not a better approach but it is a better option to start with. If you have any suggestions then submit your solution. Such as proper comment format, function format etc.

Java code with switch


public class SwitchCase
{
    public static void main(String[] args)
    {
        // Case 1 Work of Integer values
        int dataOne = 2;
        switch (dataOne)
        {
            // pattern are integer value
            // Case expression result in form of integers
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
            case 4:
                System.out.println("3-4");
                break;
            default:
                // Default statement of this switch
                System.out.println("Default Statement");
        }
        // Case 2 Work of String value
        String name = "C++";
        switch (name)
        {
            // pattern are string value
            case "C++":
                System.out.printf("C++ Programming");
                break;
            case "Java":
                System.out.printf("Java Programming");
                break;
                // Remove default statement  
        }
    }
}

/*
    File : SwitchCase.java
*/

Generated python code


class SwitchCase :
    def main(self, args) :
        #  Case 1 Work of Integer values
        dataOne = 2;
        if (dataOne==1):
            print("One")
        elif(dataOne==2):
            print("Two")
        elif(dataOne==3 or dataOne==4):
            print("3-4")
        else:
            #  Default statement of this switch
            print("Default Statement")
        #  Case 2 Work of String value
        name = "C++";
        if (name=="C++"):
            print("C++ Programming", end ="", sep ="")
        elif(name=="Java"):
            print("Java Programming", end ="", sep ="")


if __name__=="__main__":
    SwitchCase().main([])

Learn how to use switch in java.

Example of while loop

While and Do-while loop are commonly used in java programming language. Python are use while loop so we can implement do-while using while loop easily.

// Example in Do while loop
public class Test
{
    public static void main(String[] args)
    {
        int counter = 0;
        do {
            System.out.println(counter);
            // Updating terminating condition
            counter += 2;
        } while (counter <= 10);
    }
}
// File : Test.java

Generated python code


#  Example in While loop
class Test :
    def main(self, args) :
        counter = 0;
        while True :
            print(counter)
            #  Updating terminating condition
            counter += 2
            if((counter <= 10) == False) :
                break


if __name__=="__main__":
    Test().main([])

Learn how to use while loop in java.

For loop example

For of java is based on three section (initialization, test condition, increment decrement section) and body of loop. We can use this loop (for loop) in many ways. We can represent it using (while loop). For example

// For loop example
public class Test
{
    public static void main(String[] args)
    {
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                System.out.printf("i = %d j = %d\n", i, j);
            }
        }
    }
}
// File : Test.java

Generated python code


#  For loop example
class Test :
    def main(self, args) :
        i = 0
        while (i < 2) :
            j = 0
            while (j < 3) :
                print("i = %d j = %d\n" % (i,j),end ="",sep ="")
                j += 1
            i += 1


if __name__=="__main__":
    Test().main([])

Map example

import java.util.HashMap;
import java.util.Map;
/*
  HashMap Example
*/
public class MyHashMap
{
    public static void main(String[] args)
    {
        HashMap < Integer, Integer > x = new HashMap < Integer, Integer > ();
        Map < String, Integer > y = new HashMap < String, Integer > ();
        x.put(1, 100);
        y.put("One", 1111);
        System.out.println(x);
        System.out.println(y);
    }
}

Generated python code


#  HashMap Example
class MyHashMap :
    def main(self, args) :
        x =  dict()
        y =  dict()
        x[1] = 100
        y["One"] = 1111
        print(x)
        print(y)


if __name__=="__main__":
    MyHashMap().main([])

Set example

import java.util.Set;
import java.util.HashSet;
/*
  Set Example
*/
public class MySet
{
    public static void main(String[] args)
    {
        Set < Integer > x = new HashSet < Integer > ();
        HashSet < String > y = new HashSet < String > ();
        x.add(1);
        y.add("One");
        System.out.println(x);
        System.out.println(y);
    }
}

// File : MySet.java

Generated python code


#  Set Example
class MySet :
    def main(self, args) :
        x =  set()
        y =  set()
        x.add(1)
        y.add("One")
        print(x)
        print(y)


if __name__=="__main__":
    MySet().main([])

String example

public class MyString
{
    public static void main(String[] args)
    {
        String x = "ABCD";
        String y = "123";
        String z = x.concat(y);
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        // charAt
        System.out.println(x.charAt(2));
        // equals
        System.out.println(x.equals(y));
    }
}
// File : MyString.java

Generated python code


class MyString :
    def main(self, args) :
        x = "ABCD"
        y = "123"
        z = x.concat(y)
        print(x)
        print(y)
        print(z)
        #  charAt
        print(x[2])
        #  equals
        print(x==y)


if __name__=="__main__":
    MyString().main([])

Operator example

class MyOperator
{
    public static void main(String[] args)
    {
        // &&
        boolean x = true && true;
        System.out.println(x);
        // ||
        x = true || true;
        System.out.println(x);
        // ! (logical not)
        x = !x;
        System.out.println(x);
        // Bitwise
        // &
        int a = 5 & 4;
        System.out.println(a);
        // |
        a = 3 | 4;
        System.out.println(a);
        // ^
        a = 5 ^ 4;
        System.out.println(a);
        //  ~
        a = ~14;
        System.out.println(a);
        // shift 
        System.out.println(15 << 2);
        System.out.println(15 >> 2);
    }
}

Generated python code


class MyOperator :
    def main(self, args) :
        #  &&
        x = True and True
        print(x)
        #  ||
        x = True or True
        print(x)
        #  ! (logical not)
        x = not x
        print(x)
        #  Bitwise
        #  &
        a = 5 & 4
        print(a)
        #  |
        a = 3 | 4
        print(a)
        #  ^
        a = 5 ^ 4
        print(a)
        #   ~
        a = ~14
        print(a)
        #  shift 
        print(15 << 2)
        print(15 >> 2)


if __name__=="__main__":
    MyOperator().main([])

This is basic example which is normally used in java programming language. Here lot of package and method are direct convert by this tool. There are many possibilities to optimize code standard but its need time. I hope you are enjoy when use this tool.



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







Shaik Esub Babu     12 Day ago
hi this babu.can you create a converter javascript to java.please it is very helpful to all