C # differences with VB Java

Source: Internet
Author: User
Tags class generator

C # The differences with Java are summarized as follows:

C # and Java are both relatively standard object-oriented programming languages. VB is an object-based programming language. Therefore, C # is similar to Java and has few differences, C # is different from Java and VB. However, in either language, the basic data types and operations do not conflict.

 

The following is a summary of the three language relationships.

I. Syntax Basics

 

1, Operator

There is a sizeof operation in C #, which is not in Java and VB (instanceof in Java >>>, VB >>> ).

The sizeof operator is used to return the number of bytes occupied by a data type (Value Type). The usage is as follows:

Class program {static void main (string [] ARGs) {console. writeline (sizeof (int32); // display the number of characters of the int32 type console. writeline (sizeof (double); // display the number of characters in the double type }}

Running result:

 

 

2. Data Type

C # has an enumeration type. The Enum keyword is used. Java does not have an enumeration type. VB does.

enum Ages{ZhangSan,LiSi,WangWu,aLiu}

 

3. Statements

C # special statement foreach. The foreach statement is used to traverse arrays. The usage is as follows:

Class program {static void main (string [] ARGs) {int [] arr = new int [] {0, 1, 2, 3}; // defines the ARR array, assign int even = 0 and odd = 0; // define two variables and assign the initial value to 0 foreach (int I in ARR) // traverse each number in the ARR Array {if (I % 2 = 0) // If the element in the array arr is an even ++; // Add 1 else to the even variable // if it is not an even odd ++; // Add 1 to the odd variable} console. writeline (even); console. writeline (ODD );}}

Running result:

 

 

4. Method

In C #, a parameter is passed by value by default. When passed by reference, a keyword ref is required. In Java, it is also passed by value and by reference, in VB, it is passed by value and by address. The name is different and the nature is the same.

In VB, the value is byval and the transfer address is byref. The transfer address (reference) transmits a reference value. After the transfer, it points to the space. After the transfer, it passes a value and a specific value. After the transfer, it no longer has any relationship.

 

5. Both C # and Java have packages, but not in VB;

The method in C # starts with an uppercase letter, the variable starts with a lowercase letter, and the method in Java is similar to a variable. It starts with a lowercase letter, the class name starts with an uppercase letter, and the variable name in VB is named, or the form name must start with an upper-case letter (camper );

C # the Java annotation statement is the same (/**/), and VB uses "'";

Both C # and Java must have a standard library to provide input and output. C # uses the console. readline (), using bufferedreader keyin = new bufferedreadreader (New inputstreamreader (system. in); system. out. printin ();

 

 

6. Loop

After the switch statement in C #, break must be used. It can be used in Java and VB. in Java and VB, the syntax of "natural move down" can be used for case statements, that is, if the result of the current case does not match, the system automatically jumps to the next case.

 

 

Ii. Class 1, C #, and Java are classes marked with class keywords. They are application types, and each class has an access modifier (public ). There are also classes in VB, which are created in the module by using the class generator, and can be directly defined in a space in C # and Java.

 

2. Constructor

C # has constructor and destructor. The constructor name is the same as the class name and has no return value. It is used to initialize some values. The constructor is used to terminate the operation. Java also has constructor, the difference is that you do not need to define the destructor in Java, because there is an automatic garbage collection mechanism in Java.

 

3. Inheritance

C # is inherited from Java. C # is a single inheritance. Multiple inheritance is implemented using interfaces. Multiple inheritance exists in Java, and the parent class names are separated by commas;

In C #, ":" is used for inheritance. For example, the dog class inherits animal class Dog: animal, and the extends keyword is used for Java. For example, a inherits B Class A extends B.

The meaning is the same. Only keywords are different.

 

4. The relationships between C # and Java classes are dependency, association, combination, and aggregation.

Dependency is a practical method in the class. Class A depends on Class B, that is, Class B is used in Class A. Class B changes directly affect class A, and the relationship is weak.

Association is a type of reference in a class. Class A associates with Class B, that is, Class A references methods in Class B, which are expressed in the form of attributes in Class A and are strongly dependent.

Composite aggregation is the relationship between the whole and the part. The difference is that the composite relationship is contain-A (inseparable) and the aggregation relationship is has-a (severable)

 

In VB, we often discuss the relationship between modules, forms, and class modules.

A form is a visualized form. It is displayed during running, while a module is a code display. The class module is mainly used to hook up with object-oriented objects and define things in the class module, the encapsulation is similar to a third-party control.

 

The scope is also reflected here. The variables defined in the module are local variables, and the variables defined in the class module are global variables.

 

Iii. Exception Handling

In C #, an exception is thrown when a method detects an error condition, such as a division of 0. Example:

Class program {static void main (string [] ARGs) {int result = 0; // defines the result variable and assigns the initial value 0 console. writeline ("Enter the value of X:"); int x = convert. toint32 (console. readline (); // use X to obtain the keyboard input value console. writeline ("Enter the value of Y:"); int y = convert. toint32 (console. readline (); // use y to get the keyboard input value try {result = 24/(x-1)/(Y-1);} catch (exception E) // catch exceptions {console. writeline (E. message); // cause of output} console. writeline ("Result: {0}", result); // output result }}

Running result:

 

 

 

There are also Exception Handling statements in Java, which are the same as the C # language, but if there is an exception in VB, We need to manually add an exception handling statement. For example, on error goto XX:

 

 

Iv. Language and running process 1. interpreted language

Translate the prepared source program into a sentence, and then execute the sentence until the end ....... Slow execution speed, low efficiency, dependency interpreter, good cross-platform performance.

 

2. compiled language

Compile all the prepared source programs into executable programs of binary code ...... Fast execution speed, high efficiency, dependent compiler, poor cross-platform

Before executing a program written in a compiled language, a special compilation process is required to compile the program into a machine language file. If you want to run the program later, you do not need to translate it repeatedly, just use the compiled results.

 

Java is a semi-compiled semi-explanatory language, and C # Is a compiled language. The source code of Java is first compiled into bytecode and then "interpreted" by the virtual machine to execute bytecode. The Java running process is the source program. Java
-- Compiler. Class -- Operating System Platform

 

Vbis an interpreted language. Before runtime, a file in .exe format is generated. Before VB6.0, a file in pseudo-compiled language is used. The final encoding cannot be executed and must be interpreted and executed by a dynamic link library, versions later than VB6.0 and VB6.0 directly generate executable code.

 

 

Appendix: Program Execution Process

1. load to the memory Zone

2. Locate the main method and start execution.

3. Memory Management During execution

Code segment (code segment) methods and functions for storing code

Data Segment (Data Segment) static variable String constant

Stack local variables

Heap (HEAP) New stuff

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.