Last Input Code : Java

File Name (Without .java).

Last Output Code :


Online java to swift converter

This is basic tool which is capable to convert custom java code into equivalent in swift 4 code. This tool is support java 8 package, And convert into swift code with limited functionality. Such as String, Map, Set etc.

Java to swift conversion example

Array Example

public class JavaCode
{
	public static void main(String[] args)
	{
		// Array of integer element
		int[] intArr = {
			1 , 3 , 2 , 1 , 4 , 2 , 7 , 9 , 1
		};
		float[][] floatArr = new float[5][3];
		System.out.println(" size intArr : " + intArr.length);
		System.out.println(" size floatArr : " + floatArr.length);
		System.out.println(" size floatArr[0] : " + floatArr[0].length);
		System.out.println("\n Int : ");
		for (int i: intArr)
		{
			System.out.print("  " + i);
		}
		System.out.println("\n Float : ");
		for (float[] i: floatArr)
		{
			System.out.print("  " + i[0]);
		}
	}
}
// File : JavaCode

Generated swift code

import Foundation
class JavaCode
{
	static func main(_ args: [String])
	{
		// Array of integer element
		let intArr: [Int] = [1, 3, 2, 1, 4, 2, 7, 9, 1];
		let floatArr: [
			[Double]
		] = Array(repeating: Array(repeating: 0.0, count: 3), count: 5);
		print(" size intArr : "
			+ String(intArr.count));
		print(" size floatArr : "
			+ String(floatArr.count));
		print(" size floatArr[0] : "
			+ String(floatArr[0].count));
		print("\n Int : ");
		for i in intArr
		{
			print("  "
				+ String(i), terminator: "");
		}
		print("\n Float : ");
		for i in floatArr
		{
			print("  "
				+ String(i[0]), terminator: "");
		}
	}
}
JavaCode.main([String]());

Class Example

// File : SingleLL.java
// File Name : SingleLL

// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public SingleLL()
	{
		this.head = null;
	}
	// Adding new node at beginning of linked list
	public void addNode(int data)
	{
		// Create new node
		LinkNode node = new LinkNode(data);
		// Connect current node to previous head
		node.next = this.head;
		this.head = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			return;
		}
		LinkNode temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			System.out.print(" " + temp.data + " ->");
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" NULL\n");
	}
	public static void main(String[] args)
	{
		SingleLL sll = new SingleLL();
		// Linked list
		sll.addNode(4);
		sll.addNode(3);
		sll.addNode(2);
		sll.addNode(1);
		sll.display();
	}
}

Generated swift code

import Foundation

// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	init()
	{
		self.head = nil;
	}
	// Adding new node at beginning of linked list
	func addNode(_ data: Int)
	{
		// Create new node
		let node: LinkNode? = LinkNode(data);
		// Connect current node to previous head
		node!.next = self.head;
		self.head = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			return;
		}
		var temp: LinkNode? = self.head;
		// iterating linked list elements
		while (temp  != nil)
		{
			print(" "
				+ String(temp!.data) + " ->", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL\n", terminator: "");
	}
	static func main(_ args: [String])
	{
		let sll: SingleLL? = SingleLL();
		// Linked list
		sll!.addNode(4);
		sll!.addNode(3);
		sll!.addNode(2);
		sll!.addNode(1);
		sll!.display();
	}
}
SingleLL.main([String]());

Map Example


import java.util.HashMap;
import java.util.Map;
public class MyHashing
{
	//Function which is display array elements
	public static void display(int[] arr)
	{
		for (int i = 0; i < arr.length; ++i)
		{
			System.out.print("   " + arr[i]);
		}
		System.out.print("\n");
	}
	// Count occurrence of given array
	public static void occurrence(int[] arr)
	{
		// Display given array
		display(arr);
		// Create a empty map
		Map < Integer, Integer > map = new HashMap < > ();
		for (int i = 0; i < arr.length; i++)
		{
			if (map.containsKey(arr[i]))
			{
				// When key exists then update value
				map.put(arr[i], map.get(arr[i]) + 1);
			}
			else
			{
				map.put(arr[i], 1);
			}
		}
		System.out.println("  Occurrence ");
		//  Display calculated result
		for (int key: map.keySet())
		{
			System.out.println("  " + key + "  :  " + map.get(key));
		}
	}
	public static void main(String[] args)
	{
		//Array element
		int[] arr = {
			1 , 3 , 2 , 1 , 4 , 2 , 7 , 9 , 1 , 3 , 3 , 4 , 7
		};
		occurrence(arr);
	}
}

Generated swift code

import Foundation
class MyHashing
{
	//Function which is display array elements
	static func display(_ arr: [Int])
	{
		do {
			var i: Int = 0;
			while (i < arr.count)
			{
				print("   "
					+ String(arr[i]), terminator: "");
				i += 1;
			}
		}
		print("\n", terminator: "");
	}
	// Count occurrence of given array
	static func occurrence(_ arr: [Int])
	{
		// Display given array
		MyHashing.display(arr);
		// Create a empty map
		let map: [Int: Int] = [Int: Int]();
		do {
			var i: Int = 0;
			while (i < arr.count)
			{
				if (map.keys.contains(arr[i]))
				{
					// When key exists then update value
					map[arr[i]] = map[arr[i]]! + 1;
				}
				else
				{
					map[arr[i]] = 1;
				}
				i += 1;
			}
		}
		print("  Occurrence ");
		//  Display calculated result
		for key in Array(map.keys)
		{
			print("  "
				+ String(key) + "  :  "
				+ String(map[key]!));
		}
	}
	static func main(_ args: [String])
	{
		//Array element
		let arr: [Int] = [1, 3, 2, 1, 4, 2, 7, 9, 1, 3, 3, 4, 7];
		MyHashing.occurrence(arr);
	}
}
MyHashing.main([String]());

Challenges

1) Detecting mutable and immutable variables.

2) Dealing with immutable function parameters in swift.

This two is main point which is not converted into valid swift code.

Our goal

This tool will convert limited inbuilt java package which is commonly used in our program. But its is a beginning of this tool. We can capable to replace every method into swift which is exist in java. But its need to know all about equivalent methods. Which is similar in both language.

If you are not satisfied code standard and generated code result please notify me in comment section.



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