|
Java |
C#
|
|
Main class name and file name |
Must be consistent |
can be inconsistent |
|
How namespaces are imported |
Import keyword |
Using keyword |
|
Constant |
Final keyword |
const keyword |
|
Basic data types |
There are no symbols in C #, and Java does not.
C # has a value type and can define itself as a struct (struct) of a value type.
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 that of int and nullable<int> in C #, which is the wrapper of the former, 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. A conversion of nullable<int> to int in C # must be performed explicitly because a run-time exception is thrown when the value in nullable<int> is null.
The other basic types are similar.
Note:
Nullable types are instances of the system.nullable<t> structure. A nullable type can represent a value that is within the normal range of its underlying value type, plus a null value. For example, Nullable<Int32> read "Nullable Int32", which can be assigned to any value between 2147483648 and 2147483647, or it can be assigned a value null . Nullable<bool>the values that can be assigned include true, FALSE, or null. When working with databases and other data types that contain non-assignable elements, it is null particularly useful to assign values to numeric types or Boolean functions.
|
|
Initialization |
Call the base class constructor:
Subclass () {
Super ();
} |
Call the base class constructor:
Subclass (): Base () {}
or use base () in your code; |
|
Switch Statement |
(1) can only handle int type or character type
(2) After each case block write the break statement, or there will be a penetration problem.
NOTE: no goto statement. |
(1) Same
(2) Each case block is required either to provide a break statement at the end of the block, or to go to the other case label in the switch with a goto.
Note: It is best not to be controlled easily. |
|
Declaring an array |
Flexible.
Int[] x={1,2,3};//Correct
Int x[]={1,2,3};//Correct |
Int[] x={1,2,3};//Correct
Int x[]={1,2,3};//Error |
|
Object oriented |
Fully Object-oriented |
Same |
|
Inherited |
Single inheritance of classes;
Multiple interfaces can be implemented; |
Same |
|
Polymorphic |
Supports some form of polymorphism mechanism |
Same |
|
Rewrite |
The 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. |
The overridden method must add the virtual keyword declared as a virtual method, and the derived class overrides the subclass method when adding the override keyword. |
|
Access modifiers |
Class 4
Public: Members can be accessed from any code;
Protected: Members can only be accessed from derived classes;
Default: Defaults
Private: |
Public access. Not subject to any restrictions.
Private access. This class is restricted to access by members, subclasses, and instances.
protected protected Access. This class and subclass access is limited, and instances cannot be accessed.
Internal internal access. Access is limited to this item and cannot be accessed by others.
protected internal internal protection access. Limited to this item or subclass access, other inaccessible |
|
Inner class |
An inner class can directly access an instance member of an external class |
The inner class of C # does not have direct access to instance members of external classes; The inner class of C # is equivalent to the static inner class of Java |
|
Final class |
The class defined by the final keyword can no longer be derived |
The class defined by the Seale keyword can no longer be derived |
|
Interface |
(1) Keywords: interface;
(2) Internal classes, static fields, etc. are allowed in the interface; |
(1) Keywords: interface;
(2) Internal classes, static fields, etc. are not allowed in the interface; |
|
Memory management |
Managed by the runtime environment, using the garbage collector |
Managed by the runtime environment, using the garbage collector |
|
Pointer |
Not supported at all. Instead of referencing |
Support, you only support in non-secure mode that is rarely used. Usually replaces pointers with references |
|
Generic type |
The erase mechanism used by generic implementations in Java does not cause the new type to appear when the type parameter is passed in, that is, it is still completely unaware of the type parameter at run time, and is intended to be compatible with non-generics (so that it can be implicitly convertible 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). |
|
Parameter reference passing |
Only values are passed |
Use keyword ref: force the value parameter to be passed to the method by reference;
Use keyword out: outputs multiple values in a function when the parameter is not initialized;
Use the keyword params: automatically convert parameters to arrays; |
|
|
REF:
When control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword.
Out:
The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. |