Talk:Comparison of C Sharp and Java
How does Java have a simpler language? The CLR has some JIT time optimizations as well as runtiem optimizations. Java and C# run at about the same speed really. It might not be true that Java is faster.
The article needs to be cleaned a bit... Some entries are listed as both advantages for one language and disadvantages for the other, which is at best redundant. Otherwise, it's not bad; quite complete now. GregorB 22:49, Jan 31, 2005 (UTC)
I think there is good reason for pointers to be listed as both an advantage and a disadvantage for C#. I don't think there exists enough consensus about whether pointers should be used in managed languages. It might be appropriate to go into more detail about how pointers are implemented with relative safety in C# on another page and then link it. Tbjablin 05:36, 8 Feb 2005 (UTC)
I've decided it belongs in differences after all. Tbjablin 05:44, 8 Feb 2005 (UTC)
- Both languages allow the use of enumerations, via the
enum
keyword.
As far as I recall, this is false. C# uses the enum
keyword, whereas Java uses a Class (link java.lang.Enum) introduced in Java 1.5. I haven't edited the page to reflect this, as I am unsure and would like confirmation first.
In Java 5.0 enums are lower case, like types, and have their own syntax.
private enum Coin {
penny(1), nickel(5), dime(10), quarter(25);
Coin(int value) { this.value = value; }
private final int value;
public int value() { return value; }
}
Also enums can be used in switches. Classes cannot be used this way.
switch(menu) {
case FILE:
System.out.println("FILE Menu");
break;
case EDIT:
System.out.println("EDIT Menu");
break;
case FORMAT:
System.out.println("FORMAT Menu");
break;
case VIEW:
System.out.println("VIEW Menu");
break;
}
Check out http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/ http://zamples.com/JspExplorer/samples/jdk1_5.jsp http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
In Java even arrays are technically classes ie
Object o = new int[10];
Further compiling:
public class Test {
private int enum = 10;
}
Yields the error:
Test.java:2: as of release 1.5, 'enum' is a keyword, and may not be used as an identifier
(try -source 1.4 or lower to use 'enum' as an identifier)
private int enum = 10;
^
1 error
My inclination is to call it a keyword, because of the new syntax, and the compiler error. Tbjablin 04:34, 9 Mar 2005 (UTC)
- Yeah, it seems pretty clear that
enum is a keyword in Java. Neilc 01:08, 10 Mar 2005 (UTC)
Keyword or not, enum
is a feature of all (or nearly all?) programming languages in the C language family, and as such is uninteresting in this context. I've deleted the reference, hope that's OK... GregorB 12:37, Apr 29, 2005 (UTC)
Virtual machine?
C# is compiled to MSIL which is compiled into native machine code; Java is compiled into .class files which are interpreted by the JVM. The article says they both run on a virtual machine, but C# does not. I ran a series of benchmarks which I posted here and am quite sure that C# is appreciably faster than Java.
Both C# and Java use JIT compilation. Java can run in interpretted mode and even has a flag for it
java -Xint, but java -Xmixed (interpretted mode mixed with compilation) is the default. You might want to try rerunning your benchmark with java -server -Xincgc -Xmx512M and Java 1.5 Tbjablin 01:25, 24 Apr 2005 (UTC)
Also, looking at your benchmarks they are quite clearly dominated by Java's slow startup time. You might consider benchmarks that run for atleast a minute. Compare your results with [[1]] at that time Java was outprerforming C# in every area except trig performance, but things may have changed since then. Tbjablin 01:33, 24 Apr 2005 (UTC)
So I tried to repeat your benchmark. Here's what I found:
cat Test.java
public class Test {
public static void main(String[] args) {
long time = System.currentTimeMillis();
for(int i = 1; i < 1000000000; i++) {
}
System.out.println((System.currentTimeMillis() - time));
}
}
java -hotspot Test
1612
java -server Test
3
Tbjablin 03:05, 24 Apr 2005 (UTC)
Event Handling?
I'm not sure if your observation is correct about event handling. In java one ActionListener can listen to many different UI elements. I think that the net result is the same as in C#, and that the difference is really semantic. Tbjablin 02:28, 24 Apr 2005 (UTC)
I think this link supports my point: [[2]]
--Agreed, the only difference between the use of event handling in Java vs C# is simply an overloaded operator. --capnmidnight
Method invocation of Java is simpler than C#?
The article states about Java:
"Method invocation model is simpler, making it easier to implement and at times easier to read."
Maybe it's just me, but I don't see the difference between
javaObject.methodCall()
and
csharpObject.methodCall()
Am I missing something? Maybe you are talking about the "best practice" of capitalizing method names. This is not a feature of C# any more than creating meaningful variable names, or using for loops instead of while loops, i.e. it's up to programmer preference.
"C# includes delegates, whereas Java does not. Some argue that delegates are extremely useful, but others point out that their inclusion complicates the method invocation model." This is what that line was refering to. I didn't understand it when I read it either, so I changed it. Hopefully, it is clearer now. Tbjablin 05:02, 24 Apr 2005 (UTC)
--In that case, delegates don't complicate the model, they expand the model. One can completely ignore delegates and still be a quite succesful coder in C#. I think it gives a false impression that C# is more difficult to learn than Java. While it will certainly take you longer to learn completely (since there are more language-level features in C#), base functionality in both languages is a nearly identical task.
If anything, if we define a task T that is particularly suited to being implemented with a delegate pattern, then Java is technically more complicated. One would have to use a lot of interfaces, annonymous inner classes, and the Reflection API to achieve all the same things that delegates achieve very easily. I shudder to think of the ugly hack that would result from trying to reimplement delegate concatenation in Java.
This is the same arguement that C zealots make against C++, that C++'s inclusion of additional keywords made it a more complicated, and more difficult to learn. It's just not true, more language features make languages easier. We invent new langauges to make our jobs as programmers easier, not to make them more difficult. Imagine if your speaking vocabulary were cut in half, would you be able to express yourself as easily?
Delegates are there if you know how to use them, and they sit out of the way if you don't. They're not there at all in Java, so even if you know how to use them, you're out of luck.
--capn_midnight
I think the criticism is that determining which method is actually called at run-time more complicated, and can incur a performance hit. Check this out [[3]], but I guess all OO features are more complicated than just calling static function. Tbjablin 13:16, 25 Apr 2005 (UTC)
Switching Strings
Just throwing a question out there: should we mention that C# allows for strings to be used in switch statements? It is a minor feature, but it is a difference. Does Java have this yet? DoomBringer 08:12, 8 May 2005 (UTC)
- Java does not support switching on strings. It seems like a minor feature to me though. Also, does C# support just switching on Strings, or switching on all Objects? Tbjablin 13:26, 8 May 2005 (UTC)