The difference between C # and VB Java

Source: Internet
Author: User
Tags exception handling garbage collection readline
 

The difference between C # and Java is summarized as follows:

Both C # and Java are the programming languages of the more canonical object-oriented objects, VB is a programming language based on objects, so the C # language, similar to the Java language, different points less, C # and Java and VB differences between many points, but no matter which language, the basic data types and operations there will be no conflict.

The following is a summary of the three language relationships.

One: The grammatical basis

1, operator

There are sizeof operations in C # that are not available in Java and VB (there is >> in instanceof >>>,vb in Java).

The sizeof operator is used to return the number of bytes occupied by a data type (value type), as follows:

    Class program
    {
        static void Main (string[] args)
        {
            Console.WriteLine (sizeof (Int32));    Displays the number of characters
            Console.WriteLine (sizeof Double) for the Int32 type;   Displays the number of characters in double type

        }
    }


Run Result:

2, data type

C # has enumerated types, with the enum keyword, there are no enumerated types in Java, in VB.

Enum Ages{zhangsan,lisi,wangwu,aliu}


3, Statement

Unique statement foreach in C #. The purpose of a foreach statement is to traverse an array. Usage is as follows:

    Class program
    {
        static void Main (string[] args)
        {
            
            int[] arr = new int[]{0,1,2,3};//define ARR array and assign
            int even=0,odd=0;              Define two variables and assign the initial value to 0
            foreach (int i in ARR)  //Traverse arr array for each number
            {if
                (i% 2 = 0)    //If the elements in the array arr are even
                    even++;        Even variable plus 1
                else               //If not even
                    odd++;         Odd variable plus 1
            }

            Console.WriteLine (even);
            Console.WriteLine (odd);

        }
    


Run Result:

4, the method

In C #, a parameter by default is passed by value, when passed by reference, to have the keyword Ref,java is also passed by value and by reference, in VB is passed by value and address delivery, the name is different, the same nature.

The key word value in VB is ByVal, and the address is byref. Pass the address (reference), passing is worth referencing, after passing, point or that space, pass the value, passing is a specific value, after passing no longer have any relationship.

5,c# and Java have a package, VB did not;

In C # The method begins with a capital letter, the variable starts with a lowercase letter, the method in Java is similar to a variable, begins with a lowercase letter, the class name begins with a capital letter, the name of the variable in VB, or the name of the form, the beginning of a capital letter (hump);

C # Java Annotation statement (* */*), VB with "'";

Both C # and Java have a standard library to provide input and output, C # in Console.ReadLine (), BufferedReader keyin=new Bufferedreadreader in Java (new   InputStreamReader (system.in); System.out.printIn ();

6, Cycle

C # in the switch statement must be used in Break;java and VB can not be used in both Java and VB allows for case statements using the "natural Move Down" semantics, that is, if the current case results do not correspond, automatically jump to the next cases.

II: Class 1, C # and Java have classes with the Class keyword flag, for the application type, and each class has an access modifier (public). VB also has classes, the creation of the time is in the module, the use of Class builder, and in C # and Java directly defined in a space can be.

2, Constructors

There are constructors and destructors in C #, the constructor name and class name are consistent, and there is no return value to initialize some values, destructors to terminate operations, and constructors in Java, unlike in Java, where the automatic garbage collection mechanism exists.

3, inheriting

C # and Java have inheritance, C # is a single inheritance, multiple inheritance with the interface implementation, in Java there are many inheritance, the parent class name between "," separated;

Inheritance in C # using ":" such as the Dog class inherits Animal Class class Dog:animal, in Java with the extends keyword, such as a inheriting B Class A extends B

Meaning, the difference is only the keyword.

4, relationships between classes in C # and Java have dependencies, associations, combinations, and aggregations.

Dependencies are practical in Class A, Class A relies on Class B, which uses Class B, B-class modification directly affects Class A, and the relationship is weak

An association relationship is a reference in a class, a class B, which refers to a method in Class B, which is represented in the Class A, in the form of a property, strongly dependent

Combinatorial aggregation is the relationship between the whole and the part, the difference is that the contain-a (indivisible) aggregation relationship is has-a (divisible).

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

The form is a visual form, when it is run, the module is the code display, the class module is mainly for the object-oriented, the definition in the class module achieves the encapsulation purpose, similar to the third party control.

Also here is the scope, the variable defined in the module is a local variable, and the variable defined in the class module is a global variable.

III: Exception Handling

In C #, an exception is thrown when a method detects an error condition, such as a divisor of 0. Examples are as follows:

    Class program
    {
        static void Main (string[] args)
        {
            int result=0;                                   Define the result variable and assign an initial value of 0
            Console.WriteLine ("Please enter x:");
            int x = Convert.ToInt32 (Console.ReadLine ());   Use X to get the value of the keyboard input
            Console.WriteLine ("Enter a value of y:");            
            int y = Convert.ToInt32 (Console.ReadLine ());   Use Y to get the value of the keyboard input
            try
            {result
                = N/(x-1)/(y-1); 
            }
            catch (Exception E)                           //catch exception
            {
                Console.WriteLine (e.message);             Output Reason
            }
            Console.WriteLine ("Results {0}", result);       Output result

        }
    }

Run Result:

Java also has exception handling statements, like the C # language, but if there is an exception in VB, we need to manually add exception handling statements. Form: On Error Goto XX XX:

Four: Language and running Process 1, explanatory language

Translate a good source program into one sentence and then execute it until the end .... Slow execution, low efficiency, dependency interpreter, Cross-platform.

2, compile-language

The program that compiles all the source programs into binary code can be run ... Fast execution, high efficiency, reliance on compilers, poor cross platform

Compiled language written program before execution, need a specialized compilation process, the program compiled into machine language files, the future to run the words do not have to repeat the translation, directly using the results of the compilation of the line.

Java is a semi-compiled, semi-explanatory language, C # is a compiled language, Java, such as the source code first compiled into bytecode, and then by the virtual machine "interpretation" of the execution of bytecode. Where the Java run process is the source program. Java--compiler. class--Operating system Platform

VB is an interpreted language, it runs before it to generate the. exe format of the file, vb6.0 version is a pseudo-compiled language, the final final code can not be executed, the need for dynamic link library to explain the implementation, vb6.0 and vb6.0 after the version is directly generated executable code.

Attachment: Procedures for the implementation of the process

1,load to Memory Area

2, find the Main method to start the execution

3, memory management in the process of execution

Code segment (Code snippets) methods and functions for storing codes

Data segment (segment) static variable string constant

Stack (stack) local variables

Heap (heap) new out of something

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.