Talk:Comparison of C Sharp and Java/Archive 2: Difference between revisions

Content deleted Content added
move 2011 talk
m Replaced deprecated <source> tags with <syntaxhighlight> (via WP:JWB)
Line 344:
: 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:
<sourcesyntaxhighlight lang="cpp">
class Zem
{
Line 360:
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"
</syntaxhighlight>
</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:
<sourcesyntaxhighlight lang="cpp">
// C++ code
 
Line 384:
foo(value) ; // value is passed by copy
}
</syntaxhighlight>
</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:
 
<sourcesyntaxhighlight lang="cpp">
// C++ code
 
Line 403:
pi += 4 ; // now, pi is invalid [pointer semantics]
}
</syntaxhighlight>
</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)
Line 416:
 
For example, for build-in value/primitive types:
<sourcesyntaxhighlight lang="java">
// Java code
 
Line 437:
array.get(0) += 5 ; //WON'T COMPILE !!!
}
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="csharp">
C# code
 
Line 455:
array[0] += 5 ; // fast: and now, 1st element of array is 47
}
</syntaxhighlight>
</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).
 
Line 461:
 
So, for those user-defined types supposed to behave like value/primitive types:
<sourcesyntaxhighlight lang="java">
// Java code
 
Line 508:
}
}
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="csharp">
C# code
 
Line 562:
}
}
</syntaxhighlight>
</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).