• Post Reply Bookmark Topic Watch Topic
  • New Topic

Learn Java the Easy Way: main difference(s) between C# and Java?  RSS feed

 
dorel iancu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Not sure I'm in the right place for Bryson Paine's book promotion.
I got a question though: I am familiar with C# and new to Java. They look pretty similar syntax- wise. So, aside of Java running on a JVM, what would be the main difference(s) between C# and Java ?
Thank you.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dorel iancu wrote:Not sure I'm in the right place for Bryson Paine's book promotion.

Yes, you are at the right place. Welcome to the Ranch

One of the main differences I'd name an eco system. C# uses .Net framework which runs on Windows platform, so such applications are targeted to run on Microsoft Windows operating system.
While Java is platform independent, Java Virtual Machine can run on almost any OS.

For more throughout answer author is the best resource
 
dorel iancu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
So, the main difference between C# and Java is the fact that Java runs on a virtual machine. Isn't this making Java slower ?
However, Java seems to be more popular and versatile than C# so there may be other reasons as well. Wonder what Bryson Paine would say about this.
Thank you,
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dorel iancu wrote:. . .
So, the main difference between C# and Java is the fact that Java runs on a virtual machine.
No. It is a long time since I wrote any C#, but as far as I know, both run on a virtual machine and both interpret a sort of bytecode.
Isn't this making Java slower ? . . .
No. Older versions of Java® might have been slow, but at least since Java5, Java® has been about as fast as anything else.

It is possible to run C# with a compiler called Mono on a Linux box or similar, but as Liutauras said, C# is intended for the Windows® platform. The differences between the two languages are very subtle, but they are closely related in terms of their underlying OO concepts.

And ... welcome to the Ranch again.
 
dorel iancu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that makes sense.
Thank you.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dorel iancu wrote:. . . . Thank you.
That's a pleasure
 
Bryson Payne
Author
Ranch Hand
Posts: 35
6
Java PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dorel,
Thanks for the question - it's one I get a lot, and both Liutauras and Campbell are right, there are more similarities than differences in Java and C#. Microsoft's C# was at least influenced by Java (in fact, Microsoft's J++ language, which preceded C#, conformed to the Java language specification - until a court battle between then-Java-owner Sun Microsystems and Microsoft shut J++ down). Consider the following Hello World app:
In Java:

And in C#:

Besides some capitalization and a class/function name or two, you can pretty much translate directly between the two for simpler apps (kind of like speaking Spanish and being able to read some Italian or Portuguese). There's a longer discussion of the similarities/differences posted (from 16 years ago! - this has been going on for a while) at https://coderanch.com/t/35934/sharp-Java-sharp-clone-Java - but the main difference is that some employers are "Microsoft" shops, building on .NET platforms, and others are "Java" shops, often with PC/Mac/Linux and web-based apps running Java.
I recommend to my students that they learn both Java and C# (along with one or more of Python, JavaScript, PHP) if they want to be an enterprise developer, as both languages are in high demand among employers, and they each have strengths depending upon the application and the culture of the company.
The most important thing is to start learning now - whether you pick Java, C# or another programming language, get started today, and add new languages/skills as you grow .
I hope that helps a bit, and thanks again for posting,
Bryson

 
dorel iancu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, thank you very much for your answer Bryson.
Still, I have two questions (..sorry    ):
- why Java seems to be more  popular than C# ?
- in the C#, your 'Hello world' construct, the Main method is listed as public.
I used C# to create some test tools I needed when I had a job and I really like C# but I still have a lot to learn. So, long story short: why would I need to make Main() public in C# ? From all I saw and use, Main() was always private.
I realize that these questions are kind of time waste for which I apologize.
Before I go I really need to say this: this 'Ranch' is awesome. Really.
Thank you.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dorel iancu wrote:. . . why Java seems to be more  popular than C# ?
Possibly because it is awkward to run C# code other than on Windows®. That restricts the market.
. . . I realize that these questions are kind of time waste for which I apologize.
No, they are not a waste of time.

Before I go I really need to say this: this 'Ranch' is awesome. Really.
Thank you.
. . . and thank you for that.

Maybe this SO thread will help about your other question.

 
Bryson Payne
Author
Ranch Hand
Posts: 35
6
Java PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dorel,
Thanks for the follow-up - Campbell's right again, Java runs everywhere (from every Android mobile device to virtually every PC/Mac/Linux desktop/laptop), but C# is usually limited to Windows servers (and only Windows desktops, plus the tiny number of Windows phones) - Java even runs on your BluRay DVD player. In short, it's got a huge footprint compared to C#, and Java was cross-platform and web-enabled from its infancy.
On your second question, I deliberately made the C# program look more Java-ish by declaring Main() as public, and avoiding the using and namespace statements, but both programs will compile and run - C# doesn't mind if Main() is declared as private (you can even leave out the string[] args, or return an int instead of void). C# just looks for a Main() method as the entry point of the program you're running, while Java requires the main() method to be declared as public static void main(String[] something).
No problem at all - asking these questions and digging until you find the answer is what makes you a better programmer. And, understanding how things work is fun (and useful)!
Let me know if I can help anytime, and thanks again for posting,
Bryson
 
Stephan van Hulst
Saloon Keeper
Posts: 7975
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides the things already mentioned in this thread relating to the ecosystem, there are many differences in the languages themselves. C# is much richer in language features:

  • C# has properties. Java only has methods and fields.
  • C# supports operator overloading. Java doesn't.
  • C# has structs and non-virtual methods.
  • C# allows you to add non-virtual methods to existing classes through extension methods.
  • C# has output parameters.
  • C# has cool functional features like pattern matching and deconstruction.
  • C# has events built into the language.
  • C# allows client code to initialize fields and properties that don't have an associated constructor parameter.
  • C# supports default values for method parameters.
  • C# has collection initializers that you can also use for custom collections.
  • C# generics use declaration site variance while Java generics use call site variance.
  • C# retains generic type arguments at runtime.
  • C# allows you to declare type aliases.
  • C# has language support for asynchronous tasks.

  • I'm not saying that all these features that C# has are necessarily a good thing. The Java language may be less rich, but it is also much more strict. You can easily abuse features such as extension methods, operator overloading and output parameters. C# also lacks two huge language features that Java does support: Anonymous classes and checked exceptions. It's hotly debated whether checked exceptions are a good thing, but personally I really miss them in C#.

    Despite C# being a "cooler" language to use, I'm much more a Java man. The primary reason is that Java's standard API is outstanding, and it's incredibly well documented. Sometimes it's very difficult to find out exactly what the results of calling a certain method in .NET are.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I suspect that some of Java®'s shortcomings go back to its earliest days: did the Green project look like runnin gout of money or time, causing them to miss out generics, assertions, etc.
    It is possible to write properties in JavaFX; I remember discussing that about a year ago.

    Also: how much does C# maintain backward compatibility?
     
    dorel iancu
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ... Just wow !
    Well, as they say: 'be careful what you wish for'  
    On a serious note: the above are the most/ very comprehensive  explanations I ever got for anything. Awesome.
    I got a lot of knowledge from you guys and of course other questions which I intend to solve by my own.
    Thank you so (very) much !
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!