search
HomeJavajavaTutorialDetailed introduction to the syntactic differences between java and c#

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
How to distinguish between business logic and non-business logic in back-end development and perform reasonable layered design?How to distinguish between business logic and non-business logic in back-end development and perform reasonable layered design?Apr 19, 2025 pm 08:45 PM

Discussing the confusion and practice of layering in back-end development. In the process of back-end development, common three-layer architectures include controller, service and dao. However, how to...

How to add complex borders to Excel cells using GrapeCity Documents for Java library in Java?How to add complex borders to Excel cells using GrapeCity Documents for Java library in Java?Apr 19, 2025 pm 08:39 PM

Using POI library in Java to add borders to Excel files Many Java developers are using Apache...

How to use CompletableFuture to ensure the order consistency of batch interface request results?How to use CompletableFuture to ensure the order consistency of batch interface request results?Apr 19, 2025 pm 08:36 PM

Efficient processing of batch interface requests: Using CompletableFuture to ensure that concurrent calls to third-party interfaces can significantly improve efficiency when processing large amounts of data. �...

In JavaWeb applications, is it reasonable for Dao layer to cache all personnel entity classes?In JavaWeb applications, is it reasonable for Dao layer to cache all personnel entity classes?Apr 19, 2025 pm 08:33 PM

In JavaWeb applications, the feasibility of implementing entity-class caching in Dao layer When developing JavaWeb applications, performance optimization has always been the focus of developers. Either...

Which motorcycle and motorcycle system is better? Comparison of advantages and disadvantages between open Android system and closed self-developed systemWhich motorcycle and motorcycle system is better? Comparison of advantages and disadvantages between open Android system and closed self-developed systemApr 19, 2025 pm 08:30 PM

The current status of motorcycle and motorcycle systems and ecological development of motorcycle systems, as an important bridge connecting knights and vehicles, has developed rapidly in recent years. Many car friends...

How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?How to get Java entity class attribute names elegantly to avoid hard-coded in MyBatis queries?Apr 19, 2025 pm 08:27 PM

When using MyBatis-Plus or tk.mybatis...

How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?How to efficiently query personnel data in MySql and ElasticSearch through natural language processing?Apr 19, 2025 pm 08:24 PM

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)