SUNWEN tutorial ---- C # advanced (11)

Source: Internet
Author: User

OK, I have come back. I have written so much in front, and I have had enough comparisons with JAVA. Now I will use my own example, let's take a look at the differences between C #, a new-generation programming language called JAVA # and JAVA.

First we create a C # program file cs1.cs, and then create a JAVA source program file cs1.java. Their content is:

Cs1.cs:

Using System;
Public class cs1 {
Public static void Main (){
Console. WriteLine ("I am sunwen! ");
Sunwen mySunwen = new sunwen ();
Console. WriteLine (mySunwen. name );
}
}


Class sunwen {
Public String name = "chenbin! ";
}

Cs1.java:

Import System .*;

Public class cs1 {
Public static void main (String args []) {
System. out. println ("I am sunwen, how are you! ");
Sunwen mySunwen = new sunwen ();
System. out. println (mySunwen. name );
}
}

Class sunwen {
String name = "chenbin! ";
}


OK. Let's run these two programs. during the compilation process, we found that C # is indeed much faster than JAVA. (not to mention M $) In fact, the output of the two programs is the same, both of which are:

I am sunwen!
Chenbin!

One important difference is that one row is publc String name = "chenbin! ", But in JAVA, It is String name =" chenbin! ". If we remove this public in cs1.cs, an error will be generated, because in C #, when no range modifier is needed, the default value is protect, therefore, it cannot be accessed outside the class.

This is one of the important differences. in addition, if we change the public class cs1 in the C # program cs1.cs to public class cs2, save the disk, and compile it, we can see that the program runs normally. in JAVA, this obviously does not work, because JAVA requires that only one public class can be contained in a file, and the name of this class must be the same as the file name. this is another difference. in C #, it uses the Main method to locate the entry. if a program does not have a method named "Main", an "portal error cannot be found" is returned ". don't write Main as main. I often make such errors.

Well, let's talk about it. Let's talk about it later!

 

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.