Difference between Java and C # syntax

Source: Internet
Author: User

I have been transferred from C # to Java for a while. I personally think that the differences between Java and C # syntax have different opinions.

When I first learned Java, I thought the syntax is roughly the same as that of C # (it should be said that C # is roughly the same as Java. After all, Microsoft's C # is intended to imitate Java's syntax habits)

Bill Gates once said: "Java is the best programming language"

Let's get down to the point. Next we will discuss the differences between Java and C # syntax...

1. namespaces and packages

C # To organize classes that implement similar functions together, the namespace concept (namespace) is introduced)

In Java, the corresponding content is called a package)

2. Differences in class Access Control

C # there are 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 (the default value is internal when the control character is not added before the class keyword), indicating that the class can only be accessed in the same project

Java only has two types: public and default.

Public can be accessed by all classes

By default (when the control character is not added before the class keyword), it can only be accessed by all classes in the same package.

3. Access Control for Class Members

C # has four types: public, protected, private (default), and internal (note that internal is different from default)

Public can be accessed by all classes

Protected can only be accessed by quilt

Private (that is, when no control operator is written) can only be accessed inside the class.

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 other classes in the same package or by sub-classes in different packages.

Private can only be used within a class

By default, it can be accessed by other classes in this package. If a subclass is in a different package from its parent class, the subclass cannot access the default access control member in the parent class.

4. class inheritance in C # is implemented by colon (:) and extends is used in Java.

The implementation interface in C # is implemented through the colon (:), and implements is used in Java

The sealed class is implemented in C # and final class is used in Java.

In C #, constants are implemented using const and final is used in Java.

The attributes in C # are implemented using the set and get code blocks. in Java, fields similar to those in C # Represent attributes, or the setter and getter constructors are used for implementation.

C # has the concept of partial, which is not in Java.

C # The readonly modifier attribute is read-only and does not exist in Java.

In C #, virtual and override methods are used to modify Virtual Methods and rewrite methods. In Java, methods in the parent class are all virtual by default.

Java has the concept of static {}, synchroized {} code block, which is not in C #.

Java has the label (such as labela :) concept, which is not in C #.

In C #, The subclass calls the method of the parent class using base. Method (), and the super. Method () in Java ()

C # use is to determine whether an instance is of a certain class. Use instanceof in Java

C # Use foreach (int I in array) to traverse each element in the array. Use for (int I: array) in Java)

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.