Home  >  Article  >  Java  >  Detailed introduction to the syntactic differences between java and c#

Detailed introduction to the syntactic differences between java and c#

PHP中文网
PHP中文网Original
2017-06-22 13:30:081658browse

I have been switching from C# to Java for a while. Let me summarize what I think is the difference between Java and C# syntax. If you have any different opinions, I hope you will understand
When I first learned Java, I felt that the syntax was roughly the same as that of C#. The same (it should be said that C# is roughly the same as Java. After all, Microsoft's C# intentionally imitates Java's grammatical habits)
Bill Gates once said: "Java is the most outstanding programming language"
Getting to the point , let’s explore the syntax differences between Java and C#. . .
1. Namespace and package
In order to organize classes that implement similar functions together, C# introduces the concept of namespace (namespace)
The corresponding thing in Java is called a package
2, the difference 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 (in When there is no control character before the class keyword, the default is internal), 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
Default ( When there is no control character before the class keyword) it can only be accessed by all classes in the same package
3. There are four types of access control for class members
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 by classes Internal access
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 both classes in the same project Accessed by other classes, it can also be accessed by subclasses in different packages
private can only be used inside the class
It can be accessed by other classes in this package by default, if a subclass and the parent class are in different packages , subclasses cannot access the default access control members in the parent class
4, Class inheritance in C# is implemented through colon:, and extends is implemented in Java.
The interface is implemented in C# through colon:, and in Java Sealed classes in implements
are implemented with sealed, in Java they are implemented with final
Constants in C# are implemented with const, in Java they are implemented with final
C# properties are implemented with set, get code blocks, in Java Generally, fields similar to those in C# are used to represent properties, or setters and getter constructors are used to implement them.
There is a concept of partial class (partial) in C#, which is not available in Java.
There is a readonly modified attribute in C# that is read-only. In Java, No
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 static{}, synchroized{} code blocks in Java Concept, there is no concept in C
#There is a concept of label (such as labelA:) in Java, there is no concept in C
#C# uses base.method() to call the method of the parent class in a subclass, and super.method() in Java
In C#, use is to determine whether an instance belongs to a certain class. In Java, use instanceof.
In C#, use foreach(int i in array) to traverse each element in the array. In Java, use for( int i : array)

The above is the detailed content of Detailed introduction to the syntactic differences between java and c#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn