Syntax differences between C + + and Java

Source: Internet
Author: User
Tags throw exception java throws

Syntax Differences between C + + and Java

First, the two big differences are the main function and how to compile the difference, followed by a lot of small differences.

Main function
C++

Free-floating function
int main (int argc, char* argv[])
{
printf ("Hello, World");
}
Java
Each function must be part of a class, and when Java <class> Run is a particular class, the main function is called
(So you can have a main function for each class, which is useful in writing unit tests)
Class HelloWorld
{
public static void Main (String args[])
{
System.out.println ("Hello, World");
}
}

Declaration of the class
Except Java is not required to use sub-exception is almost the same.
C++
class Bar {};

Java
class Bar {}

Method declaration
All the same except in Java, the method must always be part of a class and may be public/private/protected as a cosmetic


Constructors and destructors
Constructors are the same (that is, the name of the class), and Java does not have a destructor of the exact meaning

Static member functions and variables
Method declarations are the same, but Java provides static initialization blocks to initialize static variables (which do not need to be declared in the source file):
Class Foo
{
static private int x;
Static initialization blocks
{x = 5;}
}

Declaration of the object
C++
//In the stack
MyClass x;

or in the heap.
MyClass *x = new MyClass;

Java
//always declared in the heap
MyClass x = new MyClass ();

Inheritance
C++
class Foo:public Bar
{ ... };

Java
class Foo extends Bar
{ ... }

Access level (abstraction barriers)
C++

Public :
void Foo ();
void Bar ();

Java
public void foo ();
public void Bar ();

Virtual functions
C++
virtual int foo ();//or non-virtual function writing int foo ();

Java
///function default is virtual function, prevent overloading with final keyword
int foo (); Or, Final int foo ();

Memory management
Basically the same--new to allocate, but Java has no delete because it has a garbage collector.

Null vs NULL
C++
//Initialize a pointer to NULL
int *x = NULL;

Java
//The compiler will capture a reference that uses an uninitialized
But if you assign a null because you need to initialize a reference, this is not valid
MyClass x = null;

Boolean type
Java is a bit verbose: you have to write a Boolean rather than a bool.
C++
BOOL Foo;
Java
boolean foo;

constant
C++
const int x = 7;

Java
Final int x = 7;

Throw exception
First, Java throws exceptions in the compiler-if your method might throw exceptions you must explicitly report
C++
int foo () throw (IOException)
Java
int foo () throws IOException

Array
C++
int x[10];
Or
int *x = new X[10];
Use X, then return the memory
Delete[] x;

Java
int[] x = new INT[10];
With X, the memory is recycled by the garbage collector or
or return it to the system at the end of the program life cycle

Collections and iterators
C + +

An iterator is a member of a class. The start of the range is < container >.begin (), and the end is < container >.end (). Increment with the + + operator and use the * operator to visit.
Vector Myvec;
for (Vector<int>::iterator ITR = Myvec.begin ();
ITR! = Myvec.end ();
++ITR)
{
cout << *itr;
}

Java
An iterator is just an interface. The beginning of the range is < collection >.iterator, you must use Itr.hasnext () to see if the end of the collection is reached. Use Itr.next () (a combination of the operator + + and * operations in C + +) to get the next element.
ArrayList myarraylist = new ArrayList ();
Iterator ITR = Myarraylist.iterator ();
while (Itr.hasnext ())
{
System.out.println (Itr.next ());
}

Or, in Java 5
ArrayList myarraylist = new ArrayList ();
for (Object o:myarraylist) {
System.out.println (o);
}


Abstract class
C + +
You only need to include a pure virtual function
Class Bar {public:virtual void foo () = 0;};

Java
Syntax allows the display of the declaration!
Abstract class Bar {public abstract void foo ();}

Or you can also declare an interface
Interface Bar {public void foo ();}

Then let a class inherit this interface:
Class Chocolate implements Bar
{
public void Foo () {/* do something */}
}

Reference vs Pointer
C + +
References are immutable, and more flexibility is gained by using pointers
int bar = 7, Qux = 6;
int& foo = bar;

Java
The reference is mutable and only the object address is stored;
No pointer type
MyClass x;
X.foo (); Error, X is a null ' pointer '

Note that you always have to use. To access the domain

Compiler
C++
//Compile
g++ foo.cc-o outfile
Run
./outfile

Java
//Compile the class in the Foo.java file into <classname>.class javac Foo.java
run by calling the static Main method in <classname>
Java <classname>

Comments
The two languages are the same (//and/* */Can be used)

Reprinted from: http://blog.csdn.net/xylary/article/details/1678413

Syntax differences between C + + and Java

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.