Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and C#, how close are they? [closed]

I've been using C/C++ and Python, but I now I see that a lot of new programming books use Java or C# as examples.

I don't think I'll use Java or C# for the time being, but I guess I have to study one of the languages (or both of them) in order to read/understand the books.

  • How similar Java and C#? If I learn Java, is learning C# almost free? Or vice versa?
  • If I have to choose only one of the two languages, which would be better? Which has wider coverage in terms of programming language?
like image 956
prosseek Avatar asked Apr 12 '10 21:04

prosseek


People also ask

What is difference C and Java?

C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).

Which is better C or Java?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.

Is Java based on C?

The syntax of Java is largely influenced by C++ and C. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language.

Can you use Java and C together?

If you want to call native code from Java program, you should use JNI. This will require preparation in the C side of your code, but it works pretty good. If you are new to programming, again, I would recommend avoiding that. Regarding GUI - you can work with Swing.


2 Answers

You are asking several questions at once. Let me address them separately:


How similar Java and C#?

Both C# and Java drew from C/C++ (and Objective C, and others) to define their syntax. And both of them are compiled to an intermediate language.

This common origin makes the languages look similar in many levels, to the point that code in either language can be confused with the other by beginners; and also makes the runtime environment somewhat comparable. However, there are substantial differences in both design principles and how each language evolved that make working with each quite different; here are the most prominent ones:

On the syntax level, Java was influenced by Smalltalk, while C# tried to stay closer to C/C++ (eg: compare Java's extends and implements with C#'s : notation) and took a vague inspiration from VB on those concepts that weren't mappable to C/C++ (example: property syntax).

On the features level, C# 1 was definitely close to Java. Among the few differences they had, I'd highlight C#'s support for "unsafe" code (including pointers) and for delegates; and Java's controversial throws. This makes sense, since one of the goals of C# was to become an alternative to Java.

Many language features differ heavily on implementation details. For example, enums are very C'ish on C#, but are full objects in Java; or generics are implemented on the IL-level in C#, but in Java are dealt with via type erasure (neither is really close to C++'s templates besides syntax).

On the API level, they are worlds apart. C# relies on the .Net Framework, which was built on Microsoft's experience with the Visual Studio family of products (and thus is significantly Windows-oriented), while Java's Class Library was built, IIRC, from scratch, and heavily evolved over time (on these Swing days, does anyone remember AWT? I do).

Finally, it's worth mentioning that each of the languages has its own idioms, and its own community of supporters behind it.


If I learn Java, is learning C# almost free? Or vice versa?

Neither. The key similarity is the basic syntax (semicolons, curly braces, array indexing, case-sensitiveness, etc), and you already have that from C/C++.


If I have to choose only one of the two languages, which would be better?

Short answer: flip a coin. Long answer: it depends on your coding style and on what aspects of the language you value most. My best advise is to start by trying to learn both, until you feel that one of the languages pulls you more strongly than the other.

Alternatively, you can take a look at http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp.


Which has wider coverage in terms of programming language?

If you mean language built-in features, I'd say C# wins for a narrow margin. Most of the features that C# has and Java lacks are syntax sugar (although they together make a significant difference on the learning curve and on the way the language is used). I value really high C#'s operator overload and extension methods. Also, LINQ is quite an interesting concept, but it is essentially a declarative syntax for loops.

Hope this helps.

like image 171
Edurne Pascual Avatar answered Oct 04 '22 23:10

Edurne Pascual


The libraries are very different, and the approach to documentation is very different. I find the C# approach to easier.

To illustrate what I mean, in either language some object that you want to deal with may be implemented by inheriting from "object" to "generic object" to "specific object." In Java, if you want to find out about a property of the "generic object" you have to go to the doc for that, whereas in C# documentation, all the properties are listed for the "specific object."

At least that's the way it seemed to me back a few years ago when I did some Java.

like image 24
SeaDrive Avatar answered Oct 05 '22 00:10

SeaDrive