I have been switching from C# to Java for a while. I would like to summarize what I think is the difference between the syntax of Java and C#. If you have any different opinions, I would like to ask everyone. When Haihan first learned Java, he felt that the syntax was roughly the same as that of C# (I should say C# Roughly the same as Java, after all, Microsoft’s C# intentionally imitates Java’s syntax habits)
Bill Gates once said: "Java is the most outstanding programming language"
Closer to home, let’s explore the syntax differences between Java and C#. . .
1. Namespaces and packages
In order to organize classes that implement similar functions together, C# introduces the concept of namespace (namespace)
The equivalent of this in Java is called a package
2. Differences in access control of classes
C# has only two types: public and default (same as internal)
public can be accessed by all classes (in the same project and in different projects)
internal (defaults to internal when no control character is added before the class keyword), indicating that the class can only be accessed in the same project
There are only two types of Java: public and default
public can be accessed by all classes. By default (when no control character is added before the class keyword), it can only be accessed by all classes in the same package.
3. Access control of class members
There are four types in C#: public, protected, private (default), internal (note that internal and default are different here)
public can be accessed by all classes
protected can only be accessed by subclasses
private (that is, by default when no control characters are written) can only be accessed within the class
internal can be accessed by classes in the same project
Java also has four types: public, protected, private and default
public can be accessed by all classes
protected can be accessed by other classes in the same package or by subclasses in different packages
Private can only be used inside a class and can be accessed by other classes in this package by default. If a subclass and the parent class are in different packages, the subclass cannot access the default access control members in the parent class.
4. Class inheritance in C# is implemented through colon:, and extends is used in Java.
The interface is implemented through colon: in C# and implements in Java.
Sealed classes are implemented using sealed in C# and final in Java.
Constants are implemented as const in C# and final in Java
Properties in C# are implemented using set and get code blocks. In Java, fields similar to those in C# are generally used to represent properties, or they are implemented using setter and getter constructors.
There is a concept of partial class in C# but not in Java
There is a readonly modified attribute in C# that is read-only, but not in Java.
There are virtual and override modified virtual methods and overridden methods in C#, but not in Java. The methods in the default parent class in Java are all virtual.
There are concepts of static{} and synchroized{} code blocks in Java, but not in C#.
There is a concept of labels (such as labelA:) in Java, but not in C#
In C#, the subclass uses base.method() to call the method of the parent class, and in Java, super.method() is used.
In C#, is is used to determine whether an instance belongs to a certain class, and in Java, instanceof is used.
In C#, foreach(int i in array) is used to traverse each element in the array, and in Java, for(int i : array) is used