C # vs. Java comparison

Source: Internet
Author: User
Tags case statement goto switch case

C # vs. Java comparison

After writing, I learned that Wikipedia has a much more comprehensive comparison:

Http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
NET (C #) Java basic Type Basic Type C # has unsigned numbers, Java does not. There is a value type in C #, and you can define the structure of the value type yourself (struct). The basic type (or primitive type) in Java is a value type, but Java does not have a struct, so a value type cannot be customized. Value types in C # (including all basic types) are indirectly inherited from object, have their own methods to invoke, and the value type in Java (that is, the base type) does not inherit from object, just simple data, no method can be called. In C #, int equals System.Int32, is a value type, and bool is equivalent to System.Boolean; In Java, int is the basic type, is the value type, and the integer is the reference type, the integer is the wrapper of int, int has no method, there are some methods for integer, an implicit conversion between int and integer (resulting in boxing and unboxing), However, when the integer value is null, an exception is thrown at run time. Boolean and the like. The correspondence between int and integer in Java is similar to int and nullable in C #<intThe corresponding >, the latter of which is the former wrapper, and the latter can be equal to null. But nullable<int> is actually still a value type (so it's still very light-level), so the correspondence between int and object in C # from memory is closer to Java. C # in nullable<intThe conversion of > to int must be performed explicitly because the nullable<int>A run-time exception is thrown when the value in is null. The other basic types are similar. delegate, event [the delegate in no]c# can be thought of as a type of method, so the method can be passed in a variable. The event is a layer of packaging for the delegate. Java implements the functions of delegates and events in C # through an interface that can be used by anonymous classes to achieve the function of anonymous delegates in C # (as well as the ability to implement closures). In addition, there are anonymous classes in C #, but anonymous classes in C # have no methods of data. unmanaged [No]c# can have unmanaged code, can have pointers, and so on. Java does not. Indexers [no]c# have indexers to facilitate the container class to achieve an array-like effect. Java does not, the Java container is basically put,Get, set and other methods achieve the same effect. Property [No]c# property is defined internally by the GetThe/set method makes the external use of the variable field as if it were in use, but is actually called get/set method to achieve the purpose of transparent encapsulation of data. Java has no concept of attributes. Java adds the Getxx,setxx method to the field XX by convention to achieve the same purpose. precompiled directives [No]c# have pre-compiled instructions to facilitate debugging, and there is conditionalattribute to describe the method. Java does not. operator overloading [No]c# can overload operators. Java does not. Java itself overloads the string's+ and + =, but without overloading = =, this is the most error I have made in this period of time. The = = of string in C # is equal to the comparison value, and in Java = = is the default behavior of object: Compare reference equality and compare values equal to the Equals method. (I never seem to have encountered a reference equal to compare two string variables since so many years of programming.) For comparison values equal, = =The symbol looks much more elegant than the Equals method call, and the method invocation also has to pay attention to the case of the null pointer. Inner class inner class Java inner class can access the instance members of the outer class directly. C # is no good. The inner class of C # is equivalent to the static inner class of java. Goto、Switch[Goto]、SwitchC # allows for Goto. The Goto of Java is a reserved keyword and cannot be used. However, Java allows tags, which can be followed by continue, break, and tag names when nested loops are available. C # switch can use long, String;java not. The case clause in Java switch jumps directly to the next cases clause without following a break, and C # allows the non-write break to jump directly to the next case,c# if no code is in the previous case You can jump to another case by Goto. enum        enumthe enumeration in C # is a value type, and it is based on a numeric type (by default, int), which sets the number of the enumeration item, and cannot add any other members, such as methods. Enumerations in Java are reference types (except for primitive types in Java, any type is a reference type), not numeric-based. In addition to the inability to inherit, it is not very different from the ordinary class, you can add member methods and member variables, etc. (you can also rewrite the ToString method). Both C # and Java enumerations can be used with switch. You can view the enumeration of C # as a numeric value and directly perform bitwise operations, so you can store multiple bit markers in a variable. Java enumerations are not directly related to values, so they cannot be used directly. Java uses Enumset to store enumeration flags, and does not need to use bit operations directly, much farther from the bottom. Override@OverrideC # The method that can be overridden must be added with the virtual keyword declared as a virtual method, and the override keyword is added when a derived class overrides a subclass method. Java default methods can be overridden, and derived classes and subclass methods are considered rewritten when they are signed. To declare a method that cannot be overridden, you need to add the final keyword before the method. When overridden, you can add annotations before a method (that is, custom attributes in C #) @Override so that if the method cannot find the overridden method, the compiler will make an error to prevent spelling errors. Custom feature labeling C # Enclose custom attributes in brackets []. Java starts with @, followed by the name of the custom feature. The erase mechanism used by generic implementations in generic generics, which is passed to the type parameter, does not cause the new type to appear, that is, when the type parameter is passed in, the concrete type of the type parameter is still completely unknown at run time, and is intended to be compatible with non-generics (so that it can be implicitly converted between generics and non-generics) There will be compile warnings but no compile errors, which is of course not secure); This also spawned a series of problems: You cannot define an array of generic type parameters such as t[], you cannot instantiate generics by using new T (), etc. Java generics do not support value types (words that are used are automatically wrapped into reference types). C # generics produce a new type after the type parameter passes in the type (although the CLR's optimization mechanism causes the reference type to share the same code), you can get type information for the type parameter at run time. You can define a generic array, and you can add a constraint so that it can be new. C # generics can use value types (which are not boxed). For Java generics, it's easy to say that its benefits are only at compile time and run without any generic meaning. This usually satisfies the requirements when you are using an existing generic class, but if you want to define a generic class yourself, you need to know how much of it you think it should be able to do, but in fact it is not possible. parameter reference passing [None]c# allows the use of the keyword Out,ref to explicitly specify how the parameter is passed as a reference. Java only values are passed. @ string [no]c# can be canceled by adding an @ sign before the quotation mark when writing a string/the escape effect. Java does not. ??[Non-]c#??Two-tuple operator returns the value of the preceding expression when the preceding expression is not null, or the value of the subsequent expression when the preceding expression is null. Java does not. usingimportc# can specify an alias for a namespace or class with a using. (using and dispose of, regardless of namespace) Import of Java can introduce classes or packages (that is, the C # namespace) .StaticImport can introduce members of a class. The syntax for initializing the Initialize C # Call base class constructor is: Subclass ():Base() {}java calls the base class constructor with the syntax: Subclass () {super ();} Both C # and Java can invoke other constructors of the same class in a similar syntax. (change base and super to this) Java has a code block concept that executes before the constructor (the base class's constructor). When a member variable is declared, Java allows its assignment expression to refer to another variable declared earlier, such as:Private intx =1;Private inty = x +Tenhere, the assignment statement for the variable y has a variable x. C # does not allow this. Interface        Interfaceinternal classes, static fields, and so on are allowed in Java interfaces. C # is not allowed. ReadOnly,ConstThe const of finalc# is an absolute constant and must be assigned at the same time in the declaration statement, and only numeric values, enumerations, and strings can be declared as Const. The const values are inline to each used place. The readonly of C # indicates that a variable cannot be changed after the constructor has finished executing. It only constrains the variable itself, but cannot constrain the variable reference (if it is a reference type or has a member that is a reference type). Final in Java (when constraining variables) looks more like ReadOnly. But C # readonly and const have a difference, ReadOnly int is not a switch case statement, the const can. And the final of Java is: Sometimes you can not----compile-time can be a definite value can be, and vice versa. such as: Finalintx =1;//this canFinalinty =NewRandom (). Nextint ();//that's not possible .then it can be understood that when the compiler can get a definite value, final is equivalent to C # Const, when compile can not get the definite value, final is equivalent to C # readonly. [None] Throwsjava when a possible exception is thrown, in addition to runtimeexception (including derived classes), it is either captured or declared with the throws keyword in the method declaration to continue throwing. C # does not employ this force-handling mechanism. With the same functionality but with different syntaxnamespace==The package (Java) is also required for the file structure;Internal==[Default] (no write access modifier in Java means that access is package;c# default is private.) C # 's internal protected is not in Java. )Lock==synchronized (synchronized can be modified in Java, C # can achieve the same effect with custom features [MethodImplAttribute (methodimploptions.synchronized)]) : ==extends,implementsBase==Super is==instanceof (C # has As,java not)typeof== .class[SerializableAttribute] Custom features==serializable interface [NonSerializedAttribute] Custom features==transientparams== ... (variable number parameter) in this list, there is basically only one place where Java is more beautiful than C #: enumerations. Java enumerations are more high-level and more flexible. But the memory cost is higher than the C # enumeration, which is probably why you still use constants instead of enumerations in Android. So from this point of comparison, C # is almost out of Java, and C # 's new features like perfect type inference, dynamic programming features, lambda expressions, LINQ, and so on are not included in the comparison. Of course. NET and Java two systems comparison, language is only one aspect, also has the platform, the IDE, the open source and so on many other aspects, here does not say. 
View Code

C # vs. Java comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.