Doing C# long enough, you get yourself familiar with C# concepts and syntax. That’s fine, but sometime when you need “the same” concepts in Java, it’s quite difficult. This cheat sheet here to tackle that problem.

You can view other posts in the series here:

  1. Introduction
  2. Hello World
  3. Cheats sheet (this post)

Table of Content

1. Classes

1.1. Some keywords

C#JavaExplanation for Java
: (to extend the class)extendsJava use the keyword extends to extend a class
basesuperTo access the base class methods
namespacepackageJava class belongs to a package, which is the name of the folder contains the .java file
internalJava use the concept of package. More complicated convertion is here
usingimport

Java Example

// File Animal.java, inside folder animals
package main.java.animals;

import RandomPackage.*;
import RandomPackage.RandomSubPackage.*;

public class Animal {
    ... other methods and properties ...
    public Double getWeight() {
        return weight;
    }
    ... other methods and properties ...
}

// File Pig.java, inside folder animals
package main.java.animals;

public class Pig extends Animal {
    ... other methods and properties ...

    // override the getWeight method
    public Double getWeight() {
        // do your override stuffs
        return super.getWeight();
    }
    ... other methods and properties ...
}

1.2. Generic Class

Generic class with constraint

C# Example

public class GenericClass<T&gt; where T: SomeBase
{
}

Java Example

public class GenericClass<T extends SomeBase&gt;
{
}

1.3. Interface Implement

Java Example

public class Pig implements IAnimal {
    public void Run() {
        ...your code to make the pig run...
    }
}

2. Variables

C#JavaExplanation for Java
stringjava.lang.Stringstring in both is a immutable class (cannot change instance of object after create)
objectjava.lang.Object
decimaljava.math.BigDecimal

Java Example

String hello = "Hello World!";
System.out.print(hello);

3. Methods

3.1. Extension Method

In C#, you can write extension to an object, there is no equivalent to this in Java.

C#

public static class StringExtension
{
    public static string GetFirstThreeLetters(this string targetString)
    {
        // your code here to do the job
        // for example:
        // string result = targetString.SubString(0,3);
        return result;
    }
}

public class TestClass
{
    public void TestMethod()
    {
        string randomString = "This is so random";
        Console.WriteLine(randomString.GetFirstThreeLetters);
    }
}

// Console Output
// Thi

But in Java, you need to have the full static class

public final class StringUtils {
    public static String GetFirstThreeLetters(String stringToGet) {
        // your code here to do the job
        // for example:
        // String result = stringToGet.substring(0,3);
        return result;
    }
}

public class TestClass
{
    private void TestMethod()
    {
        String randomString = "This is so random";
        StringUtils.GetFirstThreeLetters(randomString);
    }
}

// Console Output
// Thi

3.2. Safely use resource

In C#, you can use a resource safely with the keyword using

C# Example

using(MyResource myResource = new MyResource())
{
    myResource.DoSomething();
}

In Java, you will use a different keyword: try

Java Example

try(MyResource myResource = new MyResource()) {
    myResource.DoSomething();
}

3.3. Class Properties

It is very convenient for C# developers to create and use property with auto-property, property with private backing fields.

C# Example

public class Animal
{
    // Auto-property
    public int Height { get; set; }

    // Property with private backing field

    // {rivate field
    private int _weight;

    // Exposed with public property
    public int Weight
    {
        get
        {
            return _weight;
        }
        set
        {
            // do some check to validate the value
            if(value > 0)
            {
                _weight = value;
            }
        }
    }
}

// using in other class
public void RandomMethod()
{
    Animal pig = new Animal();
    pig.Weight = 500;

    Console.WriteLine("Pig weight: " + pig.Weight);
}

There is no equivalent to this in Java, you need to create 2 normal methods for get and set value of a private field

Java Example

public class Animal {
    private int _weight;

    public void setWeight(int weight) {
        if(weight > 0) {
            _weight = weight;
        }
    }

    public int getWeight() {
        return _weight;
    }
}

// using in other class
public void RandomMethod() {
    Animal pig = new Animal();
    pig.setWeight(500);

    System.out.print("Pig weight: " + pig.getWeight());
}

Other different is just one google search result away 😉 See you next post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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