Moving from C# to Java

I have been working with C# for a while now, but now a new project came written in Java and it is time to switch languages. Both languages are quite similar and in same time there are many differences. In this post I noted down few high level differences.

Namespaces and packages

In C# namespaces are used to group logical things together - classes, while equivalent in Java would be packages.

//C#
using System.IO;
namespace ComputerSystem
{
	class ComputerMemory
	{

	}
}
//JAVA
package ComputerSystem;
import java.IO.*;
class ComputerMemory
{

}

const and final In C# constant values are marked with const field modifier, while in Java final is used

Comparing strings In both languages strings are immutable, it means that a string value cannot be modified. If a string value is modified a new string is created. In C# strings can be compared with == or !=, in Java equals method on a string must be executed. In Java == will compare only references.

Unsigned values In C# it is possible to create unsigned integer, while in Java it is not. Starting with Java 8, there is APi available to perform unsigned arithmetics, but declaration of negative values is still not possible.

typeof in Java In Java Class is used for reflection, that can be used to get information about an object. For example, object.getClass().getName();

is and instanceof Are used to check if object is an instance of a specific class

Task and executors For concurrency and asynchrony in C# there are tasks, while in Java they are called Executors

foreach in Java In C# foreach is used to iterate over each object in a collection. In Java same can be achieved with for (String line : lines) { }

sealed and final These modifiers will not allow a class to be inherited

params and varargs Type for a method parameter when amount of parameters is not determined. public void Add(Object… objects) {}

base and super When accessing base class’s properties or constructor from a derived class

Method overriding In C# methods must be explicitly market with the virtual modifier, while in Java all methods are virtual by default

comments powered by Disqus