Differences between C ++ and Java syntax
First, the two major differences are the differences between the main function and the compilation method, followed by many minor differences.
Main Function
C ++
// Floating Function
Int main (INT argc, char * argv [])
{
Printf ("Hello, world ");
}
Java
// Each function must be part of a class. When Java <class> runs as a main function of a specific class, it is called.
// (You can enable each class to have a main function, which is useful in unit test writing)
Class helloworld
{
Public static void main (string ARGs [])
{
System. Out. println ("Hello, world ");
}
}
Class Declaration
In addition to Java, the number is almost the same.
C ++
Class bar {};
Java
Class bar {}
Method Declaration
Except Java, methods must always be part of a class and may be public/private/protected
Constructor and destructor
Constructors are all the same (that is, the class name), and Java does not have an accurate destructor.
Static member functions and variables
The method declaration is the same, but Java provides static initialization blocks to initialize static variables (do not declare them in the source file ):
Class foo
{
Static private int X;
// Static initialization Block
{X = 5 ;}
}
Object Declaration
C ++
// In the stack
Myclass X;
// Or in the heap
Myclass * x = new myclass;
Java
// Always declare in the heap
Myclass x = new myclass ();
Inheritance
C ++
Class FOO: Public Bar
{...};
Java
Class Foo extends bar
{...}
Authorization Action 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
// The default function is a virtual function. Use the final keyword to prevent overloading.
Int Foo (); // or, final int Foo ();
Memory Management
In general, it is the same -- New for allocation, but Java does not have Delete because it has a garbage collector.
Null vs null
C ++
// Initialize a pointer to null
Int * x = NULL;
Java
// The compiler will capture references that are not initialized
// However, If you assign a null value because you need to initialize a reference, this is invalid.
Myclass x = NULL;
Boolean
Java: you must write Boolean instead of bool.
C ++
Bool Foo;
Java
Boolean Foo;
Regular
C ++
Const int x = 7;
Java
Final int x = 7;
Throw an exception
First, Java forces an exception in the compiler-if your method may throw an exception, you must explicitly report it.
C ++
Int Foo () Throw (ioexception)
Java
Int Foo () throws ioexception
Number Groups
C ++
Int X [10];
// Or
Int * x = new x [10];
// Use X and return the memory
Delete [] X;
Java
Int [] x = new int [10];
// Use X, and the memory is recycled by the garbage collector or
// Or return it to the system at the end of the life cycle of the program
Set and iterator
C ++
An iterator is a member of a class. The range starts with <container>. Begin () and ends with <container>. End (). Increment with the ++ operator and access with the * operator.
Vector myvec;
For (vector <int >:: iterator itr = myvec. Begin ();
Itr! = Myvec. End ();
++ Itr)
{
Cout <* itr;
}
Java
The iterator is just an interface. The start of the range is <set>. iterator. You must use itr. hasnext () to check whether the range has reached the end of the set. Use itr. Next () (a combination of operators ++ and * operations in C ++) to obtain 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 ++
// Only one pure virtual function is required
Class bar {public: Virtual void Foo () = 0 ;};
Java
// Statements that can be displayed in syntax!
Abstract class bar {public abstract void Foo ();}
// Or you can 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 ++
// Reference cannot be changed. You can use pointers to obtain more flexibility.
Int bar = 7, qux = 6;
Int & Foo = bar;
Java
// The reference is variable and only the object address is stored;
// No pointer type
Myclass X;
X. Foo (); // error, X is a null ''pointer''
// You must always use. To access the domain
Compilation and Translation
C ++
// Compile
G ++ Foo. CC-O OUTFILE
// Run
./OUTFILE
Java
// Compile the class in the foo. Java file to <classname>. Class javac Foo. Java
// Call the static main method in <classname> to run
Java <classname>
Note
The two languages are the same (// and/**/available)