What are the differences between c# and java
Java and C# are both programming languages. They are two languages in two different directions.
Same points:
They are all object-oriented languages, that is to say, they can all implement object-oriented ideas (encapsulation, inheritance, polymorphism).
Difference:
1. The namespace in C# is similar to the package in Java. To import a package in Java, use import and in C#, use.
2. Both c# and Java enter from the main function, but the first letter of the main function in c# must be capitalized. There are four ways to write it as follows:
static void Main(string args[]){}
static int Main(string args[]){}
static void Main(){}
static void Main(){}
And in Java there are only One form: static void main(String [] args){}
3. Data type: Java and C# are basically the same, but the first letter of the String type in Java must be capitalized, while in C# it can be lowercase or capitalized, and there is also a Boolean type, which is boolean in Java and bool in C#.
4. Naming of variables: The $ symbol can be used in Java, but not in C#.
5. Note: Java has one less "///" documentation comment than C#.
6. Output: C# has three ways to output: Cosole.WriteLine(); Cosole.WriteLine (value to be output); Cosole.WriteLine ("format string", variable list); The usage of the first two is the same as system.out in Java The usage of the .println() method is the same. The third method outputs based on placeholders, which is more convenient than Java.
7. Control flow statement: C# is similar to Java, and the switch in C# must have a break if there is content behind the case; Java does not need a break;
8. Array: The declaration in both languages uses the new keyword. You can initialize the array while creating it, such as: int a[]={1,2,3,5,5}; but C# has two more initializations than Java, such as: int a[]=new int[3]{1, 2,3}; int a[]=new int[]{1,2,3};
9. Parameters passed in methods: Both languages use pass-by-value and pass-by-reference.
The keywords passed by C# references are ref and out, ref focuses on modification, and out focuses on output. In Java, all methods are passed by value;
10. Access modifiers: The access modifiers in C# basically correspond to those in Java, but there is an extra internal. In short, C# has 5 types of accessibility as follows:
public: Members can be accessed from any code. protected: Members can only be accessed from derived classes.
internal: Members can only be accessed from within the same assembly.
protected: Members can only be accessed from derived classes within the same assembly.
private: Members can only be accessed within the current class.
11. Since the final keyword does not exist in C#, if you want a class to no longer be derived, you can use the sealed keyword to seal it.
12. Collections: Both languages have collections ArrayList, and accessing values by key is HashMap in Java and HashTable in C#. C# is easier than Java's multi-generic collections List
13. Inheritance: Java uses the keyword extends, and C# only uses ":". To call the constructor method of the parent class, Java uses the super keyword, while C# uses the base keyword.
14. Polymorphism: Both abstract classes and abstract methods use the abstract keyword in both languages. If another class in Java inherits it, it can directly override this method; while in C#, the keyword override must be added to implement it. C# also has one more virtual method than Java to implement polymorphism.
15. Interface: They are all defined with the keyword interface, Java is implemented with the keyword implements; C# is implemented with ":". In C#, all methods within an interface are public methods by default. In Java, a method declaration can have the public modifier (even though this is not required), but in C# it is illegal to explicitly specify the public modifier for an interface method.
16. The is operator in C# is the same as the instanceof operator in Java. Both can be used to test whether an instance of an object belongs to a specific type. There is no equivalent operator in Java to the as operator in C#. The as operator is very similar to the is operator, but it is more "aggressive": if the type is correct, the as operator attempts to convert the object reference under test into the target type; otherwise, it sets the variable reference to null.
17. The enumerator is the enum type (none in Java), which is used as a variable value type, thus limiting the possible value range of the variable to the values that appear in the enumerator.
18. A structure is very similar to a class, and a structure is a value type that is stored on the stack or embedded. The structure can implement interfaces and have members like a class, but the structure does not support inheritance.
19. C# retains pointers. Unsafe. (C# is not safe to use pointers, the last one needs to be verified)
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of What are the differences between c# and java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

In-depth discussion of the differences in console.log output in this article will analyze the reasons why the output results of console.log function in a piece of code are different. Code snippets involve URL parameter resolution...

Discussion on problems when using RxJS to operate on elements in streams in learning and using RxJS...

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics
