C # differences with Vb Java

Source: Internet
Author: User
Tags class generator

The differences between C # and Java are summarized as follows: C # and Java are both relatively standard interface object programming languages. vb is an object-based programming language, so C # language, similar to the Java language, there are few differences. C # and Java have more differences with Vb. However, no matter which language, there is no conflict between basic data types and operations. The following is a summary of the three language relationships. I. Syntax basics 1. Operators in C # Have sizeof operations, but not in Java or Vb (instanceof in Java >>>, vb >> ). The sizeof operator is used to return the number of bytes occupied by a data type (Value Type). Usage: [csharp] <span style = "font-size: 18px; "> 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 occupied by the double type }}</span> running result: 2. Data Type C # contains Enumeration type, with the enum keyword, javavb does not contain enumeration types [csharp] <span style = "font-size: 18px;"> enum Ages {ZhangSan, LiSi, WangWu, aLiu} </span> 3, the special statement foreach in Statement C. The foreach statement is used to traverse arrays. Usage: [csharp] <span style = "font-size: 18px;"> class Program {static void Main (string [] args) {int [] arr = new int [] {0, 1, 2, 3}; // defines the arr array and assigns the int even = 0 and odd = 0; // defines two variables, and assign the initial value 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 the element is not an even odd ++; // Add 1 to the odd variable} Console. writeLine (even); Console. writeLine (odd) ;}</span> running result: 4. The method is in C #. A parameter is passed by value by default and passed by reference. The keyword ref is required. In Java, it is 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. There are packages in C # and Java, but they are not in vb. The method in C # starts with an uppercase letter, and the variable starts with a lowercase letter. The method in Java is similar to a variable, the class name must start with an uppercase letter, the variable name in vb, or the form name ); C # Java annotation statements are the same (/**/). vb uses "'"; C # and Java both have a standard library to provide input and output, and C # uses the Console. readLine (), using BufferedReader keyin = new BufferedReadReader (new InputStreamReader (System. in); System. out. printIn (); 6. break must be used after the switch statement in loop C #. 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 # contains 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 there is no need to define the destructor in Java, because there is an automatic garbage collection mechanism in Java. 3. inherit from both C # And Java. C # is a single inheritance. Multiple inheritance is implemented using interfaces. Java contains multiple inheritance. The parent class name uses ", "separated; in C #,": "is used for inheritance. For example, the Dog class inherits Animal class Dog: Animal, and the extends keyword is used in Java. For example, A inherits B class A extends B, only keywords are different. 4. The relationships between C # and Java classes are dependency, association, combination, and aggregation. Dependency is A practical type in the class. Class A depends on Class B, that is, Class B is used in Class A. Class B modification directly affects Class, A weak link is A reference in A class. Class A is associated with Class B, that is, Class A references methods in Class B and is represented in the form of attributes in Class, 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. 3. Exception Handling in C #, an exception is thrown when a method detects an error condition. For example, the divisor is 0. Example: [csharp] <span style = "font-size: 18px;"> class Program {static void Main (string [] args) {int result = 0; // define the result variable and assign the initial value to 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 }}</span> 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: 4: language and running process 1. The interpreted language translates the prepared source program into a sentence and then executes the sentence until the end ....... The execution speed is slow, the efficiency is low, the interpreter is dependent, and the cross-platform is good. 2. The compiled language compiles all the source programs into executable programs of binary code ...... Before executing a program written in a cross-platform poorly compiled language, you must compile the program into a machine language file, if you want to run it later, you don't need to translate it again. Just use the compiled results directly. C # Java is an interpreted language. The source code of Java and C # 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-the operating system platform Vb (C ++) is a compiled language, and the final code generated by it is executable. the pseudo-compiled language is used before vb6.0, the final encoding cannot be executed and must be explained and executed by dynamically linked libraries. Versions later than vb6.0 and vb6.0 directly generate executable code.

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.