The syntax differences between Java and c-sharp are described in detail

  • 2020-04-01 01:09:30
  • OfStack

It has been a while since C# was transferred to Java. I have summarized the differences between Java and C# syntax in my opinion
When I first learned Java, I thought the syntax was roughly the same as C# (I should say C# is roughly the same as Java, after all, Microsoft's C# is intended to imitate Java's grammar habits).
Bill Gates once said, "Java is the preeminent programming language."
To get back to the point, let's explore the syntax differences between Java and C#...
1. Namespace and package
C# introduces the concept of namespaces in order to group together classes that implement similar functions
The corresponding thing in Java is called a package.
2. Differences in access control of classes
There are only two types of C# : public and default (same as internal)
Public can be accessed by all classes (same project and different projects)
Internal (which defaults to internal when the class keyword is not preceded by a control), indicates 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 controls are added before the class keyword), they 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, and internal.
Public can be accessed by all classes
Protected can only be accessed by subclasses
Private (that is, by default when no control is written) is accessible only within the class
Internal can be accessed by classes in the same project
There are also four types of Java: 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
By default, it can be accessed by other classes in the package, and if a subclass is in a different package from its parent, it cannot access the default access control members in the parent class
4. Class inheritance in C# is implemented with the colon:, and extends in Java
Implements an interface in C# via the colon: implementation, and implements in Java
Sealed classes in C# are implemented with sealed and final in Java
C# constant is implemented by const, and final is used in Java
Properties in C# are implemented by sets and get blocks of code, and in Java they are generally implemented by fields similar to those in C# to represent properties, or by setters and getters
There is a concept of partial in C#, not in Java
The readonly modifier property is read-only in C#, not in Java
Virtual and override modify virtual methods and override methods in C#, not in Java, where methods in the default parent class are virtual
Java has the concept of static{},synchroized{} code block, not C#
The concept of a tag (such as labelA :) is available in Java, but not in C#
In C#, subclass calls the method of superclass with base.method(), and in Java with super.method().
C# USES is to determine whether an instance is of a class, and Java USES instanceof
C# USES foreach(int I in array) to iterate over each element in the array, and for(int I: array) in Java.

Related articles: