Migrating from Java to C++ Quick List(Part 1)

ช่วงหลังๆมีเหตุจำเป็นให้ยุ่งกับ C++ มากขึ้นครับ หลับหูหลับตาหยิบหนังสือมาเล่มหนึ่ง (Beginning C++ Game Programming by Michael Dawson, 2004) นั่งอ่าน-ไปเรื่อยๆ ขอบันทึกไว้ละกันครับ ว่าสำหรับการขยับจาก Java ไป C++ จุดที่แตกต่างมีอะไรบ้าง

This quick list is suitable for someone who’s migrating from Java to C++. The purpose of it is to reference or a quick reminder not guide or tutorial .

At first, I intend to write this in Thai. However, due to many terms that must use English, I have switching between Thai and English a lot. This makes the list confusing. So I decided to translate them all into my *poor-skilled* English. I hope that it won’t make it becomes more harder to understand😛

Since I’m still very new to C++ . There’s no doubt that everything is not covered completely yet, so any recommends will be appreciated (about grammar, organization, errors etc)

1. Importing

  • There’s no import keyword, C++ use #include instead
  • C++ doesn’t have namespace like jave, so there’s keyword using namespace XXX; to be used for shortening commands.

2. System.out.println(“hello world”) equals to std::cout << “hello world” << std::endl;

3. Both use same comment syntax

4.Operator are same (note that 7/3 = 2 but 7/3.0 = 2.333333, all idea and syntax about casting are same)

5. Primitive variable

  • C++ have unsigned shor int, unsigned int, unsigned long int while Java doesn’t.
  • C++ use keyword bool instead of boolean ใน java
  • C++ have command typedef for creating new keyword for primitive types.
  • มี Combined Assignment Operators (+=, -=, /=, etc) เช่นเดียวกัน


6. constant,enum

  • the keyword final will be const in c++
  • C++ มี enum
      NOVICE will equal 0 and UNBEATABLE will equal 4. Note that NUM_LEVEL (which equals 5) can be used to check condition in for loop (for(int i = 0; i < NUM_LEVEL; i++))
    • enum ship {FIGHTER = 25, BOMBER, CRUISER = 50, DESTROYER = 100};
      If you declare enum this way, the value of enums will be those you assigned to them.
    • you can treats declared enum as integer,
      eg. int tmp = (FIGHTER-10) //= 15

7. Branching (if-else, switch) and Loop (for, while, do-while) are all the same !! including (break/continue keyword)

8. C++ doesn’t have ArrayIndexOutOfBoundException , it seems to be a good news… but this might lead to destructive error. Writing into unavaliable array index may cause program to crash or lead to unpredicable results. So, be more careful when dealing with nested loop. Here’s an example of a good practice

cout << “\n\nCounting with nested for loops:\n”;
const int ROWS = 5;
const int COLUMNS = 3;
for (int i = 0; i < ROWS; + + i)
for (int i = 0; j < COLUMNS; + + j)
cout << i << “,” << j << ” “; cout << endl;

9.Using string

  • #include <string> (not “String” !!)
  • we treat can treat string like an array of chars (adoptted from C)
  • Instead of using charAt() , You can refer to each character in the string via its index ( myString[0])
  • calling function on string object (and all object) have no different (myString.erase(0,3);)
  • Java ——> myString.length(); C++ —-> myString.size();
  • Name of functions are completely different , open the manual !!
  • C++ also use escape character like Java
  1. #1 by Idetrorce on December 16, 2007 - 1:14 am

    very interesting, but I don’t agree with you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: