The difference between Java and C + + syntax

Source: Internet
Author: User

1. Annotations can act as document markers in Java programs

Class Document Tags:

1) @version

2) @author

3) @param

4) @return

5) @exception

2. Java has two bytes of characters and is Unicode encoded

3. Scope--

There is no global and local scope for Java.

No duplicate name: Java block cannot duplicate a variable declared by an inner scope with its outer scope.

4. Java programs cannot have methods in global zones outside the class.

5. Java Arrays

Defined:

int arr[] = new INT[10];

Array-related methods:

1) Copying an array

System.arrycopy (...);

2) Array sorting

Array.Sort (Arrayname);

6. Classes for Java

1) A pair of parentheses after the class name of the new statement

Mans P1 = new Man ();

The handle of the class name P1, equivalent to a C + + pointer, referencing a man object

2) Comparison of objects

operator = = When

Two variables refer to the memory address equal to return True

3) This reference handle

Another construction method can be called through this in the constructor method

{...,...,...);}

7. Garbage collection mechanism

Arrays and objects are allocated on the heap

Arrays and objects become garbage when no reference variable points to it, can no longer be used, but still occupy memory and are freed by the garbage collector at a later indeterminate time, which is why Java accounts for memory.

8.static keywords

Note: Variables in any method body cannot be declared static (only members of the class can be declared as static members)

1) Static code block

Class code{    {      System.out.println ("Code building Block");    }        static{        System.out.println ("code static Block");        }            Public code () {        System.out.println ("Code construction Method");}    }

Not contained in any method body, when the class is loaded, the static code block is executed automatically, and is executed only once, often as a property in the initialization class;

In Java, a class is loaded into the JVM in 3 steps: Mount, link, and initialize. Therefore, static code blocks are executed before the constructor.

2) The Main method is static

The declaration of this main () method is: public static void Main (String args[]). This must be defined, which is the Java specification.

Therefore, the non-static members in the class cannot be accessed directly in the main () method.

9. Inheritance of Classes
Class A{}class B extends A{}class C extends B

When you construct a subclass, you need to construct the parent class in the constructor in super (parameter) . When a subclass has methods or variables that conflict with the parent class, you can use Super to make a difference.

Class Student extends person{public  Student (string name, Int. age, String school) {    super (name,age);    This.school=school;}  }

10. Abstract classes and interfaces

1) Abstract keywords must be used to modify abstractions, and abstract methods must also be modified with abstract.

Classes that contain abstract methods must be declared as abstract classes, and subclasses of an abstract class must overwrite all abstract methods before they can be instantiated, or the subclass is an abstract class.

2) interface Interface--java improvements to C + + multiple inheritance

An interface is a collection of definitions of abstract methods and constant values;

Interfaces can be regarded as a special kind of abstract class, which contains only the definitions of constants and methods, and no implementation of variables and methods;

The variables in the interface are identified by default with public static final, so the variables defined in the interface are global static constants;

Use the Implements keyword to implement all or part of the method in an interface;

11. Polymorphism of objects

All classes in Java inherit from the object class, and object is the top-level class in the Java class layer

Methods such as GetClass (), notify (), Notifyall (), wait (), and so on in the object class cannot be overridden because these methods are defined as final types.

The difference between Java and C + + syntax

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.