Okay, so I was taking a break from my projects, and since I have Visual Studio 2012 I decided to take a look at C# coding. It's amazing. It's so awesome and everything is nice and OOP, it's just so cool.
While I was at it I started messing around with Java, and I have to say they're very similar. I mean, most of the code was a direct Copy and paste and it worked. Anyway here is my code for both and things I like and dislike about them. I'm using a simple console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
// Classes are straightforward, however I don't really understand I
// don't have to put a semi colon...
class Person
{
// The function format reminds me of Java but I don't really like not being able to just use
// public:
// and
// private:
publicvoid SetName(string name) {this.name = name;}
public string GetName() {return name;}
private string name;
}
class Program
{
staticvoid Pause()
{
System.Console.WriteLine("Press any key to continue...");
// I like this
System.Console.ReadKey();
}
// Seems a bit like Java, but I like this
staticint Main(string[] args)
{
// Kinda weird that namespaces use the . operator instead of the :: operator
System.Console.WriteLine("Hey, what's your name?");
// Why do I have to explicitly put parenthesis?
// Why do I have to use the 'new' keyword instead of just making an object?
Person person = new Person();
// I like this
person.SetName(System.Console.ReadLine());
// I like this
System.Console.WriteLine("Your name is :");
System.Console.WriteLine(person.GetName());
Pause();
return 0;
}
}
}
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
// It's bad that each class needs its own file though
publicclass Main
{
publicclass Person
{
// Same with C#
publicvoid SetName(String name) {}
public String GetName() {return name;}
private String name;
}
// I hate that I have to add 'throws IOException'
publicstaticvoid Pause() throws IOException
{
System.out.println("Press any key to continue...");
System.in.read();
}
// Nice that the main function doesn't have to be capitalized,
// but 'String' does :/
publicstaticvoid main(String[] args)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Just like C# I have to explicitly add the parenthesis :/
// Along with no pointers and always having to use new to
// create and object
Person person = new Main().new Person(); // Object creation is weird af
// Why do these things have to be so long... Same with C# too :/
System.out.println("Hey, what's your name?");
try
{
person.SetName(in.readLine());
}
catch(IOException e1)
{
e1.printStackTrace();
}
System.out.println("Your name is:");
System.out.println(person.GetName());
try
{
Pause();
}
catch (IOException e)
{
return;
}
}
}
I think the reason why I like C# more than Java is due to the IDE that I'm using, (eclipse for java) which really sucks imo. I'd have a better experience if I was using VS 2012 and there was a Java IDE. The languages are very similar, but I found C# to be more similar to C++ and I love it.