C#和Java对比
关键字:
base - super
bool - boolean
extern - native
foreach - for
get n/a
internal protected
is instanceof
namespace package
object n/a
out n/a
override n/a
params …
partial n/a
protected n/a
readonly n/a
ref n/a
sealed final
typeof n/a
using import
value n/a
virtual n/a
: extends
: implments
Just like Java, C# has a single rooted class hierarchy where all classes in C#
are subclasses of System.Object the same way all Java classes are
subclasses of java.lang.Object. The methods of the two languages'
Object classes share some similarities (e.g. System.Object's
ToString() to java.lang.Object's toString()) and differences
(System.Object does not have analogs to wait(), notify() or
notifyAll() in java.lang.Object).
继承值System.Object, java.lang.Object
Of Virtual Machines and Language Runtimes
Just like Java is typically compiled to Java byte code which then runs in managed execution environment (the Java Virtual Machine or JVM) so also is C# code compiled to an Intermediate Language (IL) which then runs in the Common Language Runtime (CLR). Both platforms support native compilation via Just In Time compilers.
NOTE: While the Java platform supports interpretation of byte code or byte code being JITed then run natively, the .NET platform only supports native execution of C# code because the IL code is always natively compiled before running.
Strings Are Immutable
C# has a System.String class which is analogous to the java.lang.String class. Both classes are immutable meaning that the values of the strings cannot be changed once the strings have been created. In both instances methods that appear to modify the actual content of a string actually create a new string to return, leaving the original string unchanged. Thus the following C# and Java code does not modify the string in either case
C# Code
String csString = "Apple Jack";
csString.ToLower(); /* Does not modify string, instead returns lower case copy of string */
Java Code
String jString = "Grapes";
jString.toLowerCase(); /* Does not modify string, instead returns lower case copy of string */
To create a string-like object that allows modification in C# it is advisable to use the System.Text.StringBuilder class whereas in Java one would use the java.lang.StringBuffer class.
NOTE: In C#, the string class can either be written as string or String.
浙公网安备 33010602011771号