I just started converting my Windows Euchre game to an Android app, and there’s already some things I miss about C#.
- Operator overloading
- In C# I can create functions to overload the =, ==, <, >, and other operators, so that I can do things like if(card1 < card2)
- In Java I have to create functions like .set, .equals, .lessThan, .greaterThan, etc. So I have to now change all of the == to .equals in my code, and so on with the other operators. Now I have to do if(card1.lessThan(card2))
- set/get Functions
- In C# I can do something like this:
public string name
{
set
{
this.m_name = value;
}
get
{
return this.m_name;
}
}Then access the variable like this:
player1.name = “Randy”;
and
name = player1.name; - In Java, I have to create 2 functions, setName, and getName, then have to access like this: player1.setName(“Randy”); and name = player1.getName();
- Native Data Types: This one I’ve run into before while programming with Android/Java… native data types have different names in C# and Java. And I’m so used to using C/C++/C# that I often find myself using the wrong name.
- In C#, true/false values are bool, and text values are string
- In Java, true/false values are boolean, and text values are String (capital S)
- This is because (as I explain in more detail in #5) strings in Java are objects, whereas they are a native data type in C#.
- Naming Convention
- In C#, generally variables start with lowercase letters, and functions and classes start with uppercase letters
- In Java, generally variable and functions start with lowercase, and only classes start with uppercase.
- String comparison: Because there is no operator overloading in Java, and strings in Java are actually objects not a native data type, this is different too
- In C#, I can say if(string1 == string2) and it will compare them.
- This is partly because strings are a native data type in C#. But even if they weren’t, you would have the ability to override the == operator.
- In Java, I have to say if(string1.equals(string2)) to compare them
- This may not seem like a big deal, but doing string1==string2 is legal, and a valid statement, but it likely will not return the result you are looking for. This will check to see if the two variables are in the same memory location, NOT whether or not they contain the same information.