首先是兩件大事-主要功能以及如何編譯它,然後有很多微小差異。
main function
C++
// free-floating function
int main( int argc, char* argv[])
{
printf( "Hello, world" );
}
Java
// every function must be part of a class; the main function for a particular
// class file is invoked when java <class> is run (so you can have one
// main function per class--useful for writing unit tests for a class)
class HelloWorld
{
public static void main(String args[])
{
System.out.println( "Hello, World" );
}
}
編譯
C++
// compile as
g++ foo.cc -o outfile
// run with
./outfile
Java
// compile classes in foo.java to <classname>.class
javac foo.java
// run by invoking static main method in <classname>
java <classname>
注釋
兩種語言相同(//和/ * * /都可以)
類的聲明
幾乎相同,但是Java不需要分号
C++
class Bar {};
Java
class Bar {}
方法聲明
相同,除了在Java中,必須始終是類的一部分,并且可以以public / private / protected開頭
構造函數和析構函數
構造函數在兩者(類名)中具有相同的文法,Java沒有與析構函數完全等效的文法
靜态成員函數和變量
與方法聲明相同,但是Java提供了一個_靜态_初始化_塊_來初始化靜态變量(而不是将定義加入源代碼檔案中):
class Foo
{
static private int x;
// static initialization block
{ x = 5; }
}
确定靜态方法和命名空間的作用域
C++
如果您有一個類并希望引用一個靜态方法,則使用Class :: method形式。
class MyClass
{
public:
static doStuff();
};
// now it's used like this
MyClass::doStuff();
Java
Java中的所有作用域都使用。再次,就像通路一個類的分段一樣,它也更正常一些:
class MyClass
{
public static doStuff()
{
// do stuff
}
}
// now it's used like this
MyClass.doStuff();
對象聲明
C++
// on the stack
myClass x;
// or on the heap
myClass *x = new myClass;
Java
// always allocated on the heap (also, always need parens for constructor)
myClass x = new myClass();
通路對象字段
C++
如果您使用的是基于正常的對象,則可以使用點來通路其細分:
myClass x;
x.my_field; // ok
但是在使用指針時,您可以使用箭頭運算符(->)通路類的細分:
myClass x = new MyClass;
x->my_field; // ok
Java
您始終使用引用(至少指針,請參見下一段),是以始終使用點:
myClass x = new MyClass();
x.my_field; // ok
引用與指針
C++
// references are immutable, use pointers for more flexibility
int bar = 7, qux = 6;
int& foo = bar;
Java
// references are mutable and store addresses only to objects; there are
// no raw pointers
myClass x;
x.foo(); // error, x is a null ``pointer''
// note that you always use . to access a field
繼承
C++
class Foo : public Bar
{ ... };
Java
class Foo extends Bar
{ ... }
通路修飾符
C++
public:
void foo();
void bar();
Java
public void foo();
public void bar();
虛函數
C++
virtual int foo(); // or, non-virtually as simply int foo();
Java
// functions are virtual by default; use final to prevent overriding
int foo(); // or, final int foo();
抽象類
C++
// just need to include a pure virtual function
class Bar { public: virtual void foo() = 0; };
Java
// syntax allows you to be explicit!
abstract class Bar { public abstract void foo(); }
// or you might even want to specify an interface
interface Bar { public void foo(); }
// and later, have a class _implement_ the interface:
class Chocolate implements Bar
{
public void foo() { /* do something */ }
}
記憶體管理
大緻相同--u new_2;allocates,但在Java中沒有_delete_,因為它有垃圾回收。
null的差別
C++
// initialize pointer to NULL
int *x = NULL;
Java
// the compiler will catch the use of uninitialized references, but if you
// need to initialize a reference so it's known to be invalid, assign null
myClass x = null;
Boolean類型
Java有點冗長:必須編寫boolean而不僅僅是bool。
C++
bool foo;
Java
boolean foo;
Const-ness
C++
const int x = 7;
Java
final int x = 7;
抛出異常
C++
int foo() throw (IOException)
Java
int foo() throws IOException
數組
C++
int x[10];
// or
int *x = new x[10];
// use x, then reclaim memory
delete[] x;
Java
int[] x = new int[10];
// use x, memory reclaimed by the garbage collector or returned to the
// system at the end of the program's lifetime
集合與疊代
C++
疊代器是類的成員。範圍的開始是.begin(),結束是 < container> .end()。使用 ++ 運算符前進,并使用 * 進行通路。
vector myVec;
for ( vector<int>::iterator itr = myVec.begin();
itr != myVec.end();
++itr )
{
cout << *itr;
}
Java
疊代器隻是一個接口。範圍的開始是.iterator,然後使用 itr.hasNext()檢查是否在結尾處。您可以使用 itr.next()(在 C ++ 中使用 ++ 和 * 的組合)獲得下一個元素。
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 );
}
===myJavaKeKeArticleKeyId:202011110003===
作者:Alex Allain
來源連結:
https://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html
