The difference between Java and C #

Source: Internet
Author: User

Both Java and C # are programming languages, and they are two languages in two different directions

Same point:

They are all object-oriented languages, that is to say, they all implement object-oriented thinking (encapsulation, inheritance, polymorphism)

Difference:

The namespaces in 1.c# are namespace similar to the package (packages) in Java, importing packets into Java and using using in C #.
Both 2.c# and Java are imported from the main function, but the first letter of the main function in C # must be capitalized, and it is written in four ways as follows:
static void Main (String args[]) {}
static int Main (string args[]) {}
static void Main () {}
static void Main () {}
In Java, there is only one form: static void Main (String [] args) {}
3. Data type: Java is basically the same as C #, but the first letter of the Java string type must be uppercase, and C # can be lowercase or uppercase, as well as the Boolean type, in Java boolean,c# is bool.
4. The name of the variable: the $ symbol can be used in Java, but not in C #.
5. Note: Java is less a "///" document comment than C #.
6. Output: C # There are three ways to output: Cosole.writeline (); Cosole.writeline (the value to output); Cosole.writeline ("format string", variable list); The first two usages are the same as the Ystem.out.println () method in Java, and the Third Way is based on the placeholder output, which is more convenient than Java.
7. Control flow statements: C # is similar to Java, and the switch in C # must have a break if there is something behind the case; Java can have no break;
8. Arrays: Both languages are declared with the new keyword. Can be initialized at the same time as: int a[]={1,2,3,5,5} while creating an array, but C # is more than two types of initialization such as: int a[]=new int[3]{1,2,3}; int A[]=new int[]{1,2,3};
9. Arguments passed in the method: both languages use value passing and reference passing.
C # 's reference to pass the keyword is ref with OUT,REF focus on the modification, out focusing on the output. In Java, all values are transmitted in the same way;
10. Access modifier: The access modifier in C # corresponds to the basic in Java, but has a 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. Because there is no final keyword in C #, if you want a class that is no longer derived, you can use the Sealed keyword seal.
12. Collections: Both languages have set ArrayList, and there is hashmap in Java that accesses values by key, and C # is Hashtable. C # is easier than the Java multi-generic collection list<t> and dictionary<k,v>, and is safer without unpacking.
13. Inheritance: Java uses the keyword extends,c# only ":" On the line. Call the constructor of the parent class Java uses the Super keyword, while C # uses the base keyword.
14. Polymorphism: Abstract class and abstract method both languages use the abstract keyword. Another class in Java, if inherited, implements a direct override of this method, while C # must be prefixed with the keyword override implementation. C # is more than a virtual method of Java to achieve polymorphism.
15. Interface: All with the keyword interface definition, java implementation by keyword implements;c# ":" Implementation. In C #, all methods within an interface are public methods by default. In Java, a method declaration can have the public modifier (even if it is not necessary), but in C #, it is illegal to explicitly specify the public modifier for the method of the interface.
16. The Is operator in C #, like the instanceof operator in Java, can be used to test whether an instance of an object belongs to a particular type. There are no operators in Java that are equivalent 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 being tested to the target type, otherwise it sets the variable reference to NULL.
17. The enumerator, which is the enum type (Java none), uses it as a type of variable value to limit the possible range of variables to the value that appears in the enumerator.
18. Structs are similar to classes, and structs are value types that are stored in stacks or embedded, structs can implement interfaces and can have members like classes, but structs do not support inheritance.
19. C # retains the pointer. Unsafe. (The last one to be verified)

The difference between Java and C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.