Talk:Comparison of C Sharp and Java: Difference between revisions

Content deleted Content added
SineBot (talk | contribs)
m Signing comment by Skoobiedu - "Type system > Enumerations: new section"
MiszaBot I (talk | contribs)
Line 3:
|archiveheader = {{aan}}
|maxarchivesize = 100K
|counter = 23
|minthreadsleft = 4
|algo = old(90d)
Line 144:
 
I agree with you. The standard way of defining a listener in Java is by using interface EventListener. The standard way of defining a property is by following the JavaBeans specification. etc. And ''it's pretty obvious'' that the reason for which the Java language intentionally lacks some specific syntax for these features, is that they can be easily implemented in the class library instead, so that the language can be kept simple. Therefore, I think it definitely makes little sense to talk about the programming language without saying that, in a real-world application, you will typically make use of some BCL class instead of a language-specific keyword. --[[Special:Contributions/151.75.53.61|151.75.53.61]] ([[User talk:151.75.53.61|talk]]) 03:36, 10 June 2011 (UTC)
 
== Value types (again) ==
 
Value types are *not* a C# invention. Even Java has the "primitive" types which has the same semantics. Only Java does not allow developers to specify their own value types, C# does. Value types are not a C# invention, even Pascal and C++ has them. In Pascal they are called "records". C++ has them not as an explicit concept, but any type act as a value type when used as an automatically (stack allocated) variable. So can we please lay this discussion to rest? [[User:Useerup|Useerup]] ([[User talk:Useerup|talk]]) 09:30, 28 November 2010 (UTC)
: There is a big difference between C/C++ structures and value types because structures in C or C++ are passed by reference, and they are passed by value in C#. It's the same in Pascal, where records are passed by reference too. Plus there are two kinds of Value types in C#: structures and enumerations. There are enumerations in Java too. Which means that the Value type line is misleading and incorrect. What would be correct would be to have two lines: structures (C# has, Java has not), and enumerations (the two languages have). There is also a specificity in C# because structures are passed by values (contrary to what is done in other languages which have this concept). I have another problem with the table, and most of the article: with almost NO sources, it is 90% OR, even if I agree that a lot of the text may be right. [[User:Hervegirod|Hervegirod]] ([[User talk:Hervegirod|talk]]) 14:41, 28 November 2010 (UTC)
::I think you may want to read up on C++ and Pascal. If I define a class (or struct) in C++ and declare a local variable of that class, it is *not* a reference; it is an ''automatic'' (look it up) object. It is automatically constructed (using a default constructor) and automatically destroyed. It has copy semantics: If I define another variable of the same type and assigns the forst one to it, C++ will "shallow" copy the members from the 1st object to the 2nd (actually, C++ will just copy the memory representation of the object). Also, C++ passes parameters of such type by value, ''unless'' you explicitly specify a pointer or reference type. Demonstration:
<source lang="cpp">
class Zem
{
public:
char* question;
int answer;
};
 
Zem zem1, zem2;
zem1.question = "The life, universe and everything?";
zem1.answer = 42;
zem2 = zem1;
zem2.question = "What is six times nine?";
std::cout << zem1.question << " " << zem1.answer << std::endl; // outputs "The life, universe and everything? 42"
std::cout << zem2.question << " " << zem2.answer << std::endl; // outputs "What is six times nine? 42"
</source>
::As you can see, zem2 is ''copied'' from zem1, but their "questions" clearly refer to different objects. If they had been copied by reference there would be only one question. [[User:Useerup|Useerup]] ([[User talk:Useerup|talk]]) 07:49, 29 November 2010 (UTC)
 
:: @Hervegirod : "<i>There is a big difference between C/C++ structures and value types because structures in C or C++ are passed by reference</i>" : You're wrong. In C and C++, you can pass a struct by copy:
<source lang="cpp">
// C++ code
 
struct Value
{
int i;
double j;
std::string s;
} ;
 
void foo(Value p_value)
{
// p_value is a copy of the original variable
}
 
void bar()
{
Value value ;
foo(value) ; // value is passed by copy
}
</source>
 
::Fact is, in C++, you can pass whatever you want by copy or reference (my personal preferred curiosity being the reference to a pointer) or even const reference. The "Value Type" semantics exists, too, on C++: Anything pointed to could be considered to not be a value type (a pointed type? ... :-) ...), but all this discussion is blurred by the fact you have references or pointers to a value type, even when allocated on the stack:
 
<source lang="cpp">
// C++ code
 
void foo()
{
int i = 0 ; // "C# value type"/"Java primitive"
int j = i ; // j is now a copy of i, but a distinct one.
int & ri = i ; // ri is an alias/reference to i
int * pi = &i; // pi points to i
 
i += 2 ; // now, ri == 2 (and *pi == 2), but j == 0 [value type semantics]
ri += 3 ; // now, i == 5 (and *pi == 5), but j == 0 [value type semantics]
*pi += 4 ; // now, i == 9 (and ri == 9), but j == 0 [value type semantics]
pi += 4 ; // now, pi is invalid [pointer semantics]
}
</source>
 
:: In C++, the value type (or concrete type) would be something whose meaningful value you access directly.<br />Whereas the pointer type would be something to be access indirectly, through pointer dereferencing, but if you play directly with the pointer (i.e. its address value), then lose its meaningful value (and in the example above, you have a bug). <b>So there is a notion of value types vs. pointed types in C++ (which mirrors the difference between value types and reference types in Java/C#)</b>.<br /><br />[[User:Paercebal|Paercebal]] ([[User talk:Paercebal|talk]]) 15:12, 23 April 2011 (UTC)
 
I confirm about value types being an important topic. Two examples comparing Java and C# code. Please bear in mind that the point is to use light-weight types (as in: not using the GC or boxing/unboxing as much as possible):
 
In Java and C#, the "Value Type" is an existing language concept (called primitive in Java).
 
This can be summed up to: Do you access the data through a reference, or do you access it directly? But despite their similarity, C# Value Types and Java primitives have some important differences:
 
<b>C# Value Types are C# objects, whereas Java primitives are not Java objects, meaning C# can (and does handle) Value Types both efficiently and genericly, whereas Java cannot handle primitives both efficiently and genericly.</b>
 
For example, for build-in value/primitive types:
<source lang="java">
// Java code
 
void foo()
{
// testing a build-in primitive
int i = 42 ;
 
// testing Object Oriented properties of a Java primitive
i.toString() ; // WON'T COMPILE because int is not an Object
new Integer(i).toString() ; // will compile, but will be slow: manual boxing involved
// Testing generic containers of java primitives
Vector<int> array = new Vector<int>() ; // WON'T COMPILE !!!
Vector<Integer> array = new Vector<Integer>() ; // only way to have a vector of ints
 
// Testing boxing/unboxing of java primitives
array.add(i) ; // slow: autoboxing involved
boolean b = array.get(0) == 25 ; // slow: auto-unboxing involved
array.get(0) += 5 ; //WON'T COMPILE !!!
}
</source>
<source lang="csharp">
C# code
 
void foo()
{
// testing a build-in value type
int i = 42 ;
 
// testing Object Oriented properties of a C# value type
i.ToString() ; // fast: no boxing involved
 
// Testing generic containers of C# value types
List<int> array = new List<int>() ;
array.Add(i) ; // fast: no boxing involved
bool b = array[0] == 25 ; // fast: no unboxing involved
array[0] += 5 ; // fast: and now, 1st element of array is 47
}
</source>
The example above shows that C# handle build-in value/primitive types efficiently, and as first class citizens (C# value types have methods, and derive from Object, while Java don't).
 
The example below will show a Java and C# implementation of an aggregation of data. This data needs to be mutable, needs to used in arrays of millions of those those data, and could be used as keys for Hash tables/maps. Being able to handle those data with value copy semantics is important, too (i.e. the assignment creates a deep copy, not a clone).
 
So, for those user-defined types supposed to behave like value/primitive types:
<source lang="java">
// Java code
 
// Of course, there is no way to have user-defined primitives,
// so will use full Java classes instead
 
class MyValueTypeSize
{
public int quantic ;
public double length ;
 
// constructors, methods, etc.
}
 
class MyValueType
{
public boolean truthness ;
// This will need the allocation a separate MyValueTypeSize on the GC heap
public MyValueTypeSize size ;
 
// constructors, methods, etc.
}
 
// etc.
 
void foo()
{
// allocation of one array of contiguous of 1-million references to items
// (i.e. each item will be allocated separately)
int size = 1000000;
MyValueType[] array = new MyValueType[size];
 
for(int i = 0; i < size; ++i)
{
// for the following line
// allocation on GC part: 1 for MyValueType and 1 for MyValueTypeSize
array[i] = new MyValueType(i, 3.1415 * i, (i % 2) == 0);
 
// for the following lines
// to avoid new allocation on GC part, the code must be written
// very carefully. We choose to write a specific method increment
// (there are possibly one increment method for each constructor)
array[i].increment(42);
array[i].increment(true, 5.55);
array[i].increment(i * 25, 0.05 * i, ((i + 1) % 2) == 0);
}
}
</source>
<source lang="csharp">
C# code
 
// testing user-defined value types
 
struct MyValueTypeSize
{
public int Quantic ;
public double Length ;
 
// + constructors, operators, etc., as needed
}
 
struct MyValueType
{
bool Truthness ;
 
// no need for a separate GL allocation for this...
public MyValueTypeSize Size ;
 
// ... so, for all intents, we can consider MyValueType
// to be an aggregation of an int, a double and a bool
// in the same structure
 
// + constructors, operators, etc., as needed
}
 
// etc.
 
void foo()
{
// allocation of one array (whose size is enough to contain 1-million
// contiguous items)
int size = 1000000;
MyValueType[] array = new MyValueType[size];
for(int i = 0; i < size; ++i)
{
// for the following line
// no allocation on GC part.
// Everything is modified in the array
array[i] = new MyValueType(i, 3.1415 * i, (i % 2) == 0);
 
// for the following lines
// no allocation on GC part
// Note that here is only ONE operator + defined to handle the
// following lines, instead of one overload for each MyValueType
// constructor in Java
array[i] += new MyValueType(42);
array[i] += new MyValueType(true, 5.55);
array[i] += new MyValueType(i * 25, 0.05 * i, ((i + 1) % 2) == 0);
}
}
</source>
 
And please note that this difference is not mere plain syntactic sugar: There is a major performance cost involved there (allocating millions of small objects is always a problem, both in speed in low-latency cases, and in memory in large data cases).
 
[[User:Paercebal|Paercebal]] ([[User talk:Paercebal|talk]]) 15:50, 23 April 2011 (UTC)
 
== Proposal for Deletion ==
 
I believe this article should be removed.
 
First, I believe it is meaningless to try to compare the languages separately from the run-time libraries. Indeed, the article does not, and makes arbitrary choices about whether to include or exclude library features. Both languages are fundamentally useless without at least some library, and are designed to work with library support.
 
Second, the cited sources are either opinion, or relate only to one language or the other. As such, I believe this article is a synthesis of published material, and therefore contravenes the policy on [[Wikipedia:No original research|original research]].
 
[[User:Spockwithabeard|Spockwithabeard]] ([[User talk:Spockwithabeard|talk]]) 14:39, 19 February 2011 (UTC)
:: This article has become a kind of forums where C# advocates like to push their own Point of views, I think without even knowing they are doing this. For example, there was a lengthy discussion where people considered that java did not have events, because part of the events framework was provided by the BCL. But the same people have no problem to write that C# has Expression trees or Query language, where it is only a BCL feature. [[User:Hervegirod|Hervegirod]] ([[User talk:Hervegirod|talk]]) 14:33, 6 March 2011 (UTC)
:BTW, I'm not sure about expression trees, but LINQ querying is definitely a language feature. [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 17:12, 16 March 2011 (UTC)
I agree that there is original synthesis going on here. If we are to have an article on this subject, then the sources should limited to reliable publications that explicitly ''compare'' C# and Java. I don't think there are many of those - maybe the introductory sections of some books that then go on to describe one language or the other in detail. Of course, if those books are themselves published by Microsoft or Sun (Oracle), then they are hardly neutral. As it stands, this article is mostly a mess of "mine is bigger than yours" bragging by fans, and not much use to ordinary readers. I tried to join in the discussion about events, but just got shouted down, from what I remember. I haven't bothered since, and that's no way to run a WP article. --[[User:Nigelj|Nigelj]] ([[User talk:Nigelj|talk]]) 16:31, 6 March 2011 (UTC)
 
To make my point more clear, we need to start from "The developers of Java based it on the C++ programming language, but removed many of the language features that are rarely used or often used poorly."[http://www.webbasedprogramming.com/Java-Unleashed/ch03.htm] (I can't remember where I first came across that on Sun's old website). What C# seems to have done (although Microsoft won't admit it) is start by directly copying the Java language and concept, then, release by release, adding back in various C++, VB and other random concepts, whether they are or could be used poorly or not. This article repeats, "C# has this; does Java have this? No? Fail." It does this without considering ''why'' the feature was left out of Java, what the pros and cons of providing developers with the feature are, or any other relevant issue. Take the first point in the comparison table: "Single-root (unified) type system? Java No; C# Yes". This could be worded, "Explicit language expression of when expensive boxing and unboxing routines have been invoked? Java Yes; C# No". This is what I meant above by '"mine is bigger than yours" bragging by fans', without the depth of coverage that would make the comparison useful to readers. For possible reader types, consider the project manager needing to decide which language to hire developers and develop a solution in, or the school-leaver deciding which language to study in depth to further their programming career. This article is too shallow and too detailed to help either of these. Who is it aimed at? Surely not just the egos of the Wikipedia authors who wrote it?! --[[User:Nigelj|Nigelj]] ([[User talk:Nigelj|talk]]) 17:21, 6 March 2011 (UTC)
 
Keep. If the threshold for inclusion in the article is the ability to cite reliable publications that explicitly ''compare'' C# and Java, then indeed the entire comparison category (e.g. file systems etc) would have go to away. No, the threshold to meet is that any claims must be verifiable. The language specifications are reliable sources as far as the syntax and semantics goes. Other sources must be used for e.g. history, philosophy. I also do not agree that this is "bragging". Does the article have issues? sure. We should fix them instead of deleting the article. This article is not stale; it still receives edits and it is read by visitors. Clearly keep. But let's fix the problems. Who is it aimed at? As is evident from the discussions above, there is obviously a lot of readers with experience from one language but without understanding of what/how the other is different. As the two languages are overlapping and competing in the marketspace it is and will continue to be a controversial topic; but also an interesting topic. [[User:Useerup|Useerup]] ([[User talk:Useerup|talk]]) 14:57, 9 March 2011 (UTC)
 
Strong Keep. The constant discussions, sometimes drifting into a flamewar, may be annoying. But this underlines how important the topic is to many people. This article is pretty much about the question why C# was made in the first place, even though it started off so close to Java. [[User:Vandroiy|Vandroiy]] ([[User talk:Vandroiy|talk]]) 20:54, 13 March 2011 (UTC)
 
Strong Keep. I think this is a very useful and informative article that can teach people something about both languages and their differences. There have been many discussions about what form the article should have and what content should be included by it, and there are definitely areas that can be improved, but that doesn't mean we should give up and delete the article. [[Special:Contributions/82.210.112.192|82.210.112.192]] ([[User talk:82.210.112.192|talk]]) 19:52, 15 March 2011 (UTC)
 
Strong Keep.
I agree that the article doesn't use a clear method of comparison and that subjective statements are difficult to avoid, especially for contributors who only know one of the languages well.
But I don't think this is inherent to the subject.
Language features can be clearly separated from library features, and core library features can be distinguished from features in optional libraries.
We just need to systematically qualify all statements about features with this information. For instance, statements such as "Java does not support events" or "Java supports events" are inadmissible, because they lack this qualification.
Statements must also be qualified with language versions, if the feature in question is version-specific. For instance, "C# has special syntax to support events" is inadmissible.
Won't doing this fix most of the problems? [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 17:12, 16 March 2011 (UTC)
 
Weak delete. The article has some value and I don't mind if it stays, but it's impossible to give a realistic comparison of these languages without the corresponding discussion of frameworks and runtimes. [[User:Maghnus|Maghnus]] ([[User talk:Maghnus|talk]]) 10:53, 23 March 2011 (UTC)
 
Weak delete. However, I would not mind to keep this article, if only it really compared these two languages. As it is, it has become a feature-fest, not a comparison. I don't think that anybody can have an understanding of how these two languages are similar, and how they differ, by reading this. Only people already knowing Java of C# can really understand anything here. And I'm even not talking about the fact that there are so few references. [[User:Hervegirod|Hervegirod]] ([[User talk:Hervegirod|talk]]) 23:48, 24 March 2011 (UTC)
 
Strong Keep.<br />I followed both Java and then C# evolution, and this is no accident if there are more "features" in C#. C# evolves faster, whereas Java evolves slowly.<br />The reason could be justifiable prudence or unjustifiable laziness, or could be the justifiable need to keep it simple or the unjustifiable incapacity to make the language evolve, I don't care.<br />So the extra features could be useful, or useless, I don't care.<br />Let the reader decide if delegates, events, unsigned integral types, user-defined value types, operator overloading, etc. are a good thing or not, but don't remove the feature comparison just because you believe the feature is useless in your chosen language.<br />If you have any doubt about the usefulness of a feature, this Talk page is not the right forum to discuss it: Bring to issue to a developer's forum, http://www.stackoverflow.com for example, and compare the answers from the experts lurking there.
[[User:Paercebal|Paercebal]] ([[User talk:Paercebal|talk]]) 16:10, 23 April 2011 (UTC)
 
== Edit ==