C # vs. Java comparison (i)

Source: Internet
Author: User
Tags access properties class definition comparison header int size
Compare C # to Java comparison (turn one)

C # is the weapon that Microsoft uses to compete with Java, because the two are surprisingly similar to a large extent, but there are also many different places, the so-called "see the difference in nuances". So where are the similarities and differences between the two? From today onwards, we will compare the features of C # and Java from various angles, hoping to help friends who are learning and using C #.

1. Overview of C # and. NET Platforms

June 2000, Microsoft releases the C # language and. NET platform. C # language is a strong type, object-oriented language, it has a simple syntax, strong expression of the characteristics, and the. NET platform is composed of Microsoft. NET Plan "is the cornerstone.

. NET platform's core includes two aspects, on the one hand is the famous common language running machine (Common Language Runtime), although this noun has been obscure, but you can compare it with the Java Virtual machine, both accomplish the task is roughly the same On the other hand is a bunch of general-purpose library functions that can be invoked in multiple languages, and compile all produce a common intermediate language (intermediate Language), which can also be likened to Java bytecode, although in some different ways.

2, C # and Java

The following is a simple list of C # and Java similarities, although here we focus on the differences between C # and Java, but it is also necessary to understand the similarities.

Both are compiled into Cross-platform, Cross-language code, and the code can only run in a controlled environment

Automatically reclaims garbage memory and eliminates pointers (pointers can be used in C #, but must be marked with unsafe keywords)

Do not require a header file, all code is limited to a scope by "package (Package)", and because there is no header file, it eliminates the cyclic dependency of the class definition

All classes are derived from objects, and memory must be allocated using the New keyword

Use object locking to support multithreading

Has the concept of an Interface (interface)

Inner class

Inheriting a class is not inherited with a specific access right;

No global function or constant, all must belong to class;

The array or string has its own length calculation and boundary checking;

Use only "." operator, no "-$#@62;" and "::";

"Null", "Boolean" and "bool" became keywords;

Any variables are initialized before use;

You cannot use integers to return to an if conditional statement, you must use a Boolean value;

"Try" module can have "finally";

3. Properties (property)

The concept of attributes should be familiar to everyone, and class member functions are free to access this class. Any property member. But it's cumbersome to access properties from one class to another, so many times we use the GetXXX and Setxxx methods to look unnatural, such as in Java or C + +, where the code is:

Foo.setsize (GetSize () + 1);
Label.getfont (). Setbold (True);

In C #, however, such a method is "attributed". The same code, in C #, becomes:

foo.size++;
Label.font.bold = true;

As you can see, C # is clearly easier to read and understand. We can see something like this in the subroutine code for this "property method":

Java/c++:

public int GetSize ()
{
return size;
}

public void setSize (int value)
{
size = value;
}

C#:
public int Size
{
Get{return size;}
Set{size = value;}
}

To differentiate between this attributed method and the property members of the class, attribute members are referred to in C # as field, and property becomes the term for this "attributed method". Incidentally, in fact, such a method of Attribution in VB and Delphi is often encountered in VB, it is also called properties.

In addition, get and set in C # must appear in pairs, a property cannot have only get without set (in Java and C + + you can have only got or set), the advantage of doing so in C # is that it is easy to maintain, and if you want to modify a property, you will pay attention to both the get and set methods , at the same time modify, will not change this forget that.

4. Object indexing mechanism (Indexer)

The object indexing mechanism is introduced in C #. To be clear, object indexing is actually an array of objects. This is associated with the attributes in the previous section. For the purposes of this, properties need to hide the get and set methods, whereas in the indexing mechanism, the get or set methods of each object are exposed. For example, the following example illustrates this more clearly.

public class skyscraper
{
Story[] Stories;
public Story this [int index] {
get {
return stories [index];
}
set {
if (value!= null) {
stories [index] = value;
}
}
}
...
}

Skyscraper empirestate = new skyscraper (...);
empirestate [102] = new Story ("The Top One", ...);

Oh, with this feature, we can no longer be afraid of the classroom teacher told us to write object array of this program.

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.