Java & C#

closed account (N36fSL3A)
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.

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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:
        public void   SetName(string name) {this.name = name;}
        public string GetName()            {return name;}
        private string name;
    }

    class Program
    {
        static void Pause()
        {
            System.Console.WriteLine("Press any key to continue...");
            // I like this
            System.Console.ReadKey();
        }

        // Seems a bit like Java, but I like this
        static int 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;
        }
    }
}


Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
public class Main
{
	public class Person
	{
		// Same with C#
		public void SetName(String name) {}
		public String GetName() {return name;}
		
		private String name;
	}
	
	// I hate that I have to add 'throws IOException'
	public static void 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 :/
	public static void 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.
I think in C# you should also be able to also do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Person
{ 
   private string name;

   public string Name
   {
      get
      {
         return name;
      }
  
      set
      {
         name = value;
      }
   }
}
Last edited on
You can also use auto-implemented properties in C# 3 and above.

1
2
3
4
5
6
class Person
{ 
    public string Name { get; set; }

    // .. Additional methods, events, etc.
}


Edit:
PS. CLR Via C# by Jeffrey Richter is worth a read if you get interested in .net development.
Last edited on
Topic archived. No new replies allowed.