What is the difference between java and c?

Source: Internet
Author: User

Java designers hate the complexity of C ++, so Java is very concise, GC also makes memory management very convenient, C # is interested in Java GC, and virtual machine technology, we hope to integrate Microsoft's major languages. NET. Therefore, C # is not simple or even complicated in terms of language.

The two languages have different designs. Java is the compiling and interpreting language, and C # Is the compiling and then compiling and running language. Java is not delegated, and C # is delegated. Java tends to use interfaces to implement delegate functions, while Abstract Class plays a greater role in C # Than Interface.

Java follows the camel naming rules, and C # follows the Pascal naming rules. But now more and more Java people are using C #, and the camel naming rules are also brought to C #, which may make C # code more and more difficult to read. Why didn't C # follow camel? I don't see any bad naming rules for camel.

I. Class Name. this and internal class

In java, we often see usage similar to class name. this is the current object instance. Why does the class name appear before? C # programmers may be confused about this.

In Java, the internal class is used in many places, and you can even access members in the external class in the internal class. At this time, when this is used in the internal class, the question of who this is, whether it is the current object instance of the internal class or the current object instance of the external class.

In Java, the Class Name of the external class is added before this to indicate that the current object instance of the external class is used in the internal class.

Let's take a look at the example below.

Copy codeThe Code is as follows: package core. sisheng;

// External class definition
Public class OuterClass {

// Internal class definition
Private class InnerClass
{
// No id member is defined in the internal class. Access the members in the external class here.
Public int getId () {return OuterClass. this. id ;}
Public void setId (int id) {OuterClass. this. id = id ;}

// The name Member is defined in the internal class to directly access members in the internal class. By default, this accesses members in the current class.
Private String name;
Public String getName () {return this. name ;}
// You can add an internal class name before this.
Public void setName (String name) {InnerClass. this. name = name ;}

// Members with the same name in the external class can also be accessed in the internal class. The name of the external class must be added.
Public String getOuterName () {return OuterClass. this. name ;}
Public void setOuterName (String name) {OuterClass. this. name = name ;}

@ Override
Public String toString ()
{
Return "Id:" + this. getId () + ", Inner Name:" + this. getName () + ", Outer Name:" + this. getOuterName ();
}
}

// The member id and name defined in the external class
Private int id;
Private String name;

Private InnerClass innerInstance;
Public OuterClass ()
{
This. innerInstance = new InnerClass ();
This. innerInstance. setId (20 );
This. innerInstance. setName ("Tom ");
This. innerInstance. setOuterName ("Alice ");
}

Public String toString ()
{
Return this. innerInstance. toString ();
}
}

In C #, classes are classified as Nested classes and non-nested classes. The former is the class declared inside other data types. The latter is a class that is directly defined in a namespace. Nested classes are rarely defined in C.

Non-embedded classes only allow public and internal access control, while built-in classes allow all five access control operators, private, protected, internal protected, public and internal. The internal class can also access all methods of the external class, including the instance method and private method, but an instance of the external class needs to be passed explicitly.

The internal class in C # can use the type and static method defined by the external class, but cannot directly use the instance method of the external class. Therefore, the above problem does not exist.

In C #, the external class is more like a namespace for internal classes. As long as the access control permits, you can use the following method to create an internal class object instance.

OuterClass. InnerClass obj = new OuterClass. InnerClass (); this instance has no direct relationship with any instance of the external class. Similar to static internal classes in Java.

Ii. class Name. class and type

In java, the usage of class name. class is often seen. This usage is equivalent to typeof (class Name) in C #, used to obtain type object instance reference.

In java, each class has a corresponding Class object. When a class is compiled and compiled. in the class file, a Class object is generated to indicate the type information of the class. Three methods to obtain a Class instance:

Obtain the Class instance of the object by calling the getClass () method of the object instance.

Use the static Class method forName () to obtain a Class instance with the Class name. Class. forName (xxx. xx. xx) returns a Class, which requires the JVM to search for and load the specified Class. That is to say, the JVM will execute the static code segment of the Class.

Class Name. calss method to obtain the Class instance. For the encapsulation Class of the basic data TYPE, you can also use. TYPE to obtain the corresponding Class instance of the basic data TYPE.

In C #, the method for getting type object instances is simpler and clearer.

This method is obtained by calling the GetType () method of the data instance. This method inherits from the Object, so any Object in C # has the GetType () method, x. getType (), where x is the variable name.

In typeof (x), x must be a specific class name, type name, etc. It cannot be a variable name.

Use the static method System. Type. GetType () of System. Type ().

Iii. Anonymous class

In java, many anonymous classes are used. For example, in Android, code similar to this is often seen during button listening.

Copy codeThe Code is as follows: this. listener0 = new OnClickListener (){

@ Override
Public void onClick (View arg0 ){
Intent intent = new Intent (MainActivity. this, ActivityFrameLayout. class );
SetTitle ("FrameLayout ");
StartActivity (intent );
}
};

Here, OnClickListenter is actually an interface. Can it be used to create an object instance? Of course not.

Therefore, java automatically creates an anonymous class that implements the interface here. What we create is actually an object instance of this anonymous class.

The advantage of this is that we do not have to define a class that is only used once, and then create an object instance through this class to simplify program development.

For example, we have the following interface.

Copy codeThe Code is as follows: public interface myInterface {
Void onClick ();
}

You can create an anonymous class object instance that implements the interface through the interface, and then use this object instance.

Copy codeThe Code is as follows: myInterface instance = new myInterface (){
@ Override
Public void onClick ()
{
System. out. println ("Clicked! ");
}
};

Instance. onClick (); in C #, this form is not used at all. Through delegation, the same function can be implemented very simply.

Note that there is no delegate in java.

If we output the instance type, you will see the actual type of the anonymous class.

Copy codeThe Code is as follows: System. out. println (instance. getClass ());
// Class core. sisheng. Study1 $1 iv. Property)

The attribute concept should be very familiar to everyone. class member functions can freely access any attribute Member in this class. However, if you want to access attributes of another class from one class, it is quite troublesome. Therefore, we often use the Getxxx and Setxxx methods, which seems extremely unnatural, for example, if Java or C ++ is used, the Code is as follows:

Copy codeThe Code is as follows: foo. setSize (getSize () + 1 );
Label. getFont (). setBold (true );

However, in C #, this method is "attribute. The same Code becomes in C:
Foo. size ++;
Label. font. bold = true;
As you can see, C # is obviously easier to read and understand. We can also see the similar situation from the subprogram code of this "attribute method:

Java/C ++:

Copy codeThe Code is as follows: public int getSize ()
{
Return size;
}
Public void setSize (int value)
{
Size = value;
}

C #:

Copy codeThe Code is as follows: public int Size
{
Get {return size ;}
Set {size = value ;}
}

To distinguish this attribute-based method from the attribute members of a class, the attribute members are called "field" in C )", "attribute" is a special term for this "attribute-based method. By the way, this attribute method is often encountered in VB and DELPHI. In VB, it is also called attribute. In addition, Get and Set must appear in pairs in C #. An attribute cannot only Get but not Set (only Get or Set can be used in Java and C ++ ), the advantage of doing so in C # is that it is easy to maintain. If you want to modify a certain attribute, you will pay attention to the Get and Set methods at the same time, and you will not forget this.

V. Object indexing mechanism (Indexer)

The object index mechanism is introduced in C. Clearly, the object index is actually an object array. Here, we will talk about the properties in the previous section. The Get and Set methods need to be hidden for the properties. In the index mechanism, the Get or Set methods of each object are exposed. For example, the following example clearly illustrates this point. The differences between C # and Java

Copy codeThe Code is as follows: public class Skyscraper
{
Story [] stories;
Public Story this [int index] {
Get {
Return stories [index];
}
Set {
If (value! = Null ){

Stories [index] = value;
}
}
}
...
}

The differences between C # and JAVA are described above. I hope to help you understand C # and JAVA.

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.