Home  >  Article  >  Java  >  What are the differences between c# and java

What are the differences between c# and java

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-11-11 13:16:443403browse

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 and Dictionary. There is no need to unbox and pack them, and it is safer.

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!

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