The compilation results of 1, C# and JAVA languages are independent of computers and programming languages, and executable files can be executed under management.
Execution in the environment;
2. Both C # and JAVA adopt automatic garbage collection mechanism;
3. Pointer operation has been cancelled in C # and JAVA languages;
4. Neither C # nor JAVA language has a header file;
5. Both C # and JAVA languages only support single inheritance. To realize the function similar to multiple inheritance, it must be realized through the interface.
6. Classes are all derived from Object classes, and the objects of classes are generated by the keyword new;
7. Both C # and JAVA languages support threads;
8.C# and JAVA languages have no global variables and global functions, and all variables and functions belong to a certain class.
9. Both C # and JAVA languages support strict checking of array and string boundaries, and there will be no boundary overflow;
10, C# and JAVA all use "."operators instead of "->" and "::"operators;
1 1, C# and JAVA both use null and bool as keywords;
All values in 12, C# and JAVA languages must be initialized before they can be used;
If statements in 13, C# and JAVA languages are not allowed to use integers as judgment conditions;
Try blocks in 14, C# and JAVA languages can all be followed by finally blocks.
The difference between C# and JAVA:
Although C# and JAVA have many similarities, they are independent because they are high-level programming languages developed by two different companies.
Self-contained, each with its own characteristics. The differences between C# and JAVA are as follows:
1, property
For those developers who often use rapid development tools such as Delphi or Visual Basic, attribute is a very familiar concept.
Generally speaking, the value of an attribute can be read through getXXX, and the value of an attribute can be set through setXXX.
Common attribute operation statements in JAVA: foo.setsize (foo.getsize ()+1); label.getFont()。 SetBold (true);
Common attribute operation statements in c#: foo.size++; label . font . bold = true;
Obviously, the above attribute setting method is more concise and readable than JAVA. This fully embodies the simple characteristics of C#.
JAVA definition of attribute: public int getsize () {returnsize; } public void setSize(int value){ size = value; }
C# simplifies the definition of attributes: public int size {get {return size} set {size = value}}
2. Index
C# provides the function of index to add indexes to objects, so that objects can be treated in an array-like way, but the JAVA language does not support index.
A typical way to define an index in C# is as follows:
Public story this[int index]
{
Get {returnstories [index]; }
Setup {
If (value! =null){
Story [index] = value
}
}
3. Delegate: It can be considered as a type-safe and object-oriented function pointer.
C# enables delegate to access different functions through a single name, which realizes similar functions as interface in JAVA, but is more useful than interface.
4. Events
C# provides direct support for events. It handles events through delegates and event keywords. The event keyword hides all delegate methods, and the operators "+=" and "-+"allow programmers to add or delete time handlers freely.
5.enum: enumeration is used to specify a series of objects.
C# defines and uses enumerations through the following statements:
Definition: public enumeration direction (North; East; West and South);
Usage: direction wall = direction. North;
JAVA does not directly support enumeration. If you want to realize the function similar to C#, you must first define a class.
Direction of public courses {
public final static int NORTH = 1;
public final static int EAST = 2;
public final static int WEST = 3;
Public final static int SOUTH = 4;; }
After defining the Direction class, JAVA can use enumeration by referring to the values in the class:
Int wall= direction. NOTRH
6.foreach statement
C# provides a standard for loop and a foreach statement (imported from VB) to loop through the elements in the collection.
A typical way for JAVA to traverse all elements in a collection is as follows:
And (! collection.isEmpty())
{
object o = collection . get();
connection . next();
…
}
C# traverses all the elements in the set: foreach (object o in the set) {…}
7. Unified data type:
Most high-level programming languages have basic data types, such as integer and floating point. At the same time, in order to better meet the actual needs, different data types have different processing methods. Obviously, if we can combine the processing of simple data types with the processing of complex data types and handle them in a consistent way, it will undoubtedly greatly improve the efficiency of application design and enhance the flexibility of program design.
JAVA language also adopts the strategy of dealing with basic data types separately, but it provides a series of classes that encapsulate these basic data types on the basis of basic data types, such as Integer encapsulated by class Integer and double-precision floating point encapsulated by class double.
C# provides a different way from JAVA to realize the unification of data types. In fact, in C#, even a simple data type like int is realized through a structure Int32 inside c#. In C#, you can think that int is just an alias for the structure Int32. Because the structures in C# are also inherited from class objects, and each structure also has methods defined in the Object class, integers can be manipulated in C# in the following ways: int I = 5;; System. console . WriteLine(I . ToString());
8. Operator overloading
Operator overloading can manipulate various data types in a more natural way, thus greatly improving the readability and flexibility of the program. The "= =" operator in C# is defined in the Object class, and the = = operator defined in the Object obtains the final result by comparing the references of two values. If there is a class related to a collection, the ICompar interface must be implemented in such a class. A method CompareTo is defined in this interface, which returns the comparison result of two objects. On this basis, you can further define the operators for comparison, such as
>, =, < =, etc. In fact, numeric types (int, long, etc. You can use these comparison operators directly, and they all implement the ICompare interface internally.
9. Polymorphism
Virtual method provides polymorphic technology. Polymorphism means that a derived class can define a method with the same name as a base class. Although both JAVA and C# support polymorphism, the specific implementation methods are still different.
In the JAVA language, by default, objects in the base class can directly call virtual methods in the derived class. In C# language, the base class must call the virtual method in the derived class through the virtual keyword. At the same time, in C# language, if a method overloads a method with the same name in the base class, it must be implemented by the keyword override. The typical procedure to realize polymorphism in C# is as follows:
Class b {public virtual void foo{}}
Class d: b {public override d void foo () {}}
The above simply compares the similarities and differences between C# and JAVA. In fact, the contrast between the two is far more than the above. To learn these two languages well, we need to do a lot of practical work to distinguish them in practice.