Converting C# to Java

  • Post author:
  • Post category:칼럼
  • Post comments:0 Comments
  • Post last modified:December 12, 2010

Document Number: 1.0.5

Author: Jae-Hoon CHOI

February 2006

Updated February 2006

Table of Contents

  1. Related links
  2. Comparing two string instances
  3. Operator overloading
  4. Property
  5. const, readonly vs. final
  6. sealed vs. final
  7. lock vs synchronized
  8. Enumerated Types
  9. namespace vs. package
  10. using vs. import
  11. Inheritance
  12. Chain Constructors

Comparing two string instances

[C#]

String first= "String Comparision";
String second = "String Comparision";

if(first == second) { Console.WriteLine("Good #1"); }
if(first.Equals(second)) { Console.WriteLine("Good #2"); }
Good #1
Good #2

[JAVA]

String first = "String Comparision";
String second = "String Comparision";

if(first == second) { System.out.println("Good #1"); }
if(first.equals(second)){ System.out.println("Good #2"); }
Good #2

Operator overloading

Java doesn’t support operator overloading. In above section, I’ve showed that when two Java strings arecompared with ‘==’ operator, it means that their references but values should be compared.

[C#]

class Revenue
{
	private int amount;

	public Revenue(int amount)
	{
		this.amount = amount;
	}

	public int Amount
	{
		get { return amount; }
	}

	public static Revenue operator+(Revenue thisRevenue, Revenue thatRevenue)
	{
		return new Revenue(thisRevenue.Amount + thatRevenue.Amount);
	}
}

class EntryPoint
{
	[STAThread]
	static void Main(string[] args)
	{
		Revenue r1 = new Revenue(10);
		Revenue r2 = new Revenue(10);

		Revenue total = r1 + r2;
		Console.Write(total.Amount);
	}
}
20

[JAVA]

class Revenue
{
	private int amount;

	public Revenue(int amount)
	{
		this.amount = amount;
	}

	public int getAmount()
	{
		return amount;
	}

	public static Revenue Add(Revenue thisRevenue, Revenue thatRevenue)
	{
		return new Revenue(thisRevenue.getAmount() + thatRevenue.getAmount());
	}
}

public static void main(String[] args) {
	Revenue r1 = new Revenue(10);
	Revenue r2 = new Revenue(10);

	Revenue total = Revenue.Add(r1,r2);
	System.out.println(total.getAmount());
}
20

Property

[C#]

private string fileName = "foo.txt";

public string FileName
{
	get { return fileName; }
	set { fileName = value; }
}

[JAVA]

private String fileName = "foo.txt"

public String getFileName()
{
	return fileName;
}

public void setFileName(String name)
{
	fileName = name;
}

const, readonly vs. final

[C#]

public class Car
{
	private const string configFileName = "settings.config";
	private readonly string carName;

	public Car(string name)
	{
		carName = name;
	}
}

[JAVA]

public class Car
{
	private static final String configFileName = "settings.config";
	private final String carName;

	public Car(String name)
	{
		carName = name;
	}
}

sealed vs. final

[C#]

public sealed class SingletonClass
{
	...
}

[JAVA]

public final class SingletonClass
{
	...
}

lock vs. synchronized

[C#]

public bool Stopping()
{
	lock (stopLock)
	{
		return stopping;
	}
}

[JAVA]

public boolean Stopping()
{
	synchronized (stopLock)
	{
		return stopping;
	}
}

Enumerated Types

Now that Java before 1.5 doesn’t support ‘enum’, the developer have to make the alternative class to ‘enum’.

You are a lucky man, if you are using Java 5. You don’t have to code anything. ‘enum’ is supported.

[C#]

enum Drink{ pepsi, coke, juice }

Drink drink = Drink.pepsi;

[JAVA >= 1.5]

enum Drink{ pepsi, coke, juice }

Drink drink = Drink.pepsi;

[JAVA < 1.5]

public abstract class EnumClass
{
	private int enumValue = 0;

	public EnumClass(EnumClass enumClass)
	{
		this(enumClass.getValue());
	}

	public EnumClass(int enumValue)
	{
		this.setValue(enumValue);
	}

	protected abstract void Validate(int enumValue) throws IllegalArgumentException;

	public int getValue()
	{
		return this.enumValue;
	}

	public void setValue(int enumValue)
	{
		Validate(enumValue);
		this.enumValue = enumValue;
	}

	public void setValue(EnumClass enumClass)
	{
		this.setValue(enumClass.getValue());
	}

	public boolean equals(Object aThat)
	{
	    //check for self-comparison
	    if ( this == aThat ) return true;

	    if ( aThat == null || aThat.getClass() != this.getClass() ) return false;

	    //cast to native object is now safe
	    EnumClass that = (EnumClass)aThat;

	    return equals(that.getValue());
	}

	public boolean equals(int that)
	{
		if(this.getValue() == that) { return true; }
    	else { return false; }
	}
}

public final class Drink extends EnumClass
{
	public final static int pepsi = 0;
	public final static int coke = 1;
	public final static int juice = 2;

	public EResult(EResult enumClass)
	{
		super(enumClass);
	}

	public EResult(int result)
	{
		super(result);
	}

	protected void Validate(int enumValue) throws IllegalArgumentException
	{
		if(enumValue!=pepsi
				&& enumValue!=coke
				&& enumValue!=juice)
		{
			throw new IllegalArgumentException(enumValue + " is not allowed.");
		}
	}
}

Drink drink = new Drink(Drink.pepsi);

namespace vs. package

[C#]

namespace kaistizen
{
	public class SampleClass {
		public SampleClass() {}
	}
}

[Java]

package kaistizen;

public class SampleClass {
	public SampleClass() {}
}

using vs. import

[C#]

using kaistizen;

class EntryPoint {
	[STAThread]
	static void Main(string[] args)	{
		SampleClass sample = new SampleClass();
	}
}

[Java]

import kaistizen.*;

public class EntryPoint {
	public static void main(String[] args) {
		SampleClass sample = new SampleClass();
	}
}

Inheritance

[C#]

public interface IInterface
{
	void Do();
}

public abstract class AbstractClass
{
	public AbstractClass() {}
}

public class ChildClass : AbstractClass, IInterface
{
	public ChildClass() {}

	public void Do() {}
}

[Java]

public interface IInterface
{
	void Do();
}

public abstract class AbstractClass
{
	public AbstractClass() {}
}

public class ChildClass extends AbstractClass implements IInterface
{
	public ChildClass() {}

	public void Do() {}
}

Chain Constructors

[C#]

public abstract BaseClass
{
	public BaseClass
	{
		Console.WriteLine("BaseClass Constructor");
	}
}

public class ThisClass : BaseClass
{
	public ThisClass() : base()
	{
		Console.WriteLine("ThisClass Constructor #1");
	}

	public ThisClass(Object nothing) : this()
	{
        	Console.WriteLine("ThisClass Constructor #2");
	}
}

class EntryPoint
{
	[STAThread]
	static void Main(string[] args)
	{
		Console.WriteLine( new string('=',25) );
		ThisClass thisClass1 = new ThisClass();

		Console.WriteLine( new string('=',25) );
		ThisClass thisClass2 = new ThisClass(null);
	}
}
=========================
BaseClass Constructor
ThisClass Constructor #1
=========================
BaseClass Constructor
ThisClass Constructor #1
ThisClass Constructor #2

[Java]

public abstract class SuperClass {
	public SuperClass() {
		System.out.println("SuperClass Constructor #1");
	}
}

public class ThisClass extends SuperClass {
	public ThisClass() {
		super();
		System.out.println("ThisClass Constructor #1");
	}

	public ThisClass(Object nothing) {
		this();
		System.out.println("ThisClass Constructor #2");
	}
}

public class EntryPoint {
	public static void main(String[] args) {
		System.out.println( "========================" );
		ThisClass thisClass1 = new ThisClass();

		System.out.println( "========================" );
		ThisClass thisClass2 = new ThisClass(null);
	}
}
=========================
SuperClass Constructor
ThisClass Constructor #1
=========================
SuperClass Constructor
ThisClass Constructor #1
ThisClass Constructor #2
Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments