The difference between Java and C + + 's "syntax"!

Source: Internet
Author: User

I actually think there is no comparability between the language grammar!

But because the amount has been learned in C + +, and now learn Java, there is Zhao Benshan said: "Knowledge is learned miscellaneous!" ", so I personally summarize, hope to remind myself!"

Java vs. C + +:

In Java:

double to use%f;

Boolean type: boolean;

The character type accounts for two bytes;

If, while,for can only use the logical expression when the true and false judgment, cannot use the number.

A longer integer to add l;

By default all real numbers are of type double;

High security: When an advanced type is converted to a low-level type, an error is added! For example, to convert a double to float, float a=2.0f (F);

When taking the remainder: the divisor and divisor can be real numbers, but the positive and negative of the remainder are the same as the divisor;

There are no pointers in Java:

A *a=new a ();//c++

A a=new a ();//java

When memory is allocated dynamically: C + + uses pointer A in the stack to point to the allocated heap space.

Java, in form, uses the class's object a (equivalent to static pointer variables) to point to memory in the heap, but can use a to represent memory in the heap;

constructor function:

In Java, if you define a constructor that has parameters, you must define the parameterless constructor yourself when you call the parameterless constructor, which is not the case in C + +;

When an object is created, the member variables of each of these types are initialized automatically, the numeric type is initialized, the Boolean is initialized to False, and the reference is initialized to null;

Arrays in C + + and C can be created in the stack, but the arrays in Java are reference types and can only be created in the heap.

Enum types in C and C + + can be used 0,1 ... To express, but not in Java;

Example:enum MyColor {red, green, blue};

MyColor m= red;//c or C + + define enum type new variable in

MyColor m= Mycolor.red;//java define enum type new variable in

There is no string reference type in C, it is represented by a character array and ends with the ' \ n ' table character, while C + + and Java have string reference types;

In addition, the string object in C + + can be manipulated with s[i], while Java does not, you need to convert to char[];

The difference between Java and C + + 's "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.