i've just learn the basics of c++ and figured out some differences of c++ and java. i'd like to post this for others to have a clue when learning a new language, either
java to c++ or
c++ to java. i personally learn java first and only know its basics so be free to add more on this article or to correct things.
namespace
c++ uses namespace to group related functions and classes and to avoid variable name conflict.
1 2 3
|
namespace name {
//function and classes here
}
| |
java uses package to group related classes since everything in java is inside a class. in this example its like a namespace inside a namespace
1 2 3
|
package string.manip.*; //namespace manip inside namespace string
class className {
}
| |
include
c++ uses header files to include codes already written before and to be able to use this snippet later.
#include "mystringmanip.h"
though java uses package as namespace and also use like acts like header file
import java.io.*;
arguments to main
in c++ argc is the number of arguments and argv is the string arguments on an array
int main(int argc, char* argv[])
in java args is automatically parse as an array object
public static void main(String args[])
desctructor
in c++
~classname()
in java
void finalize()
passing arguments to function
c++ passes args by value but still can pass by reference with the operator &.
java automatically pass primitives(int, char, boolean) by value and by reference for objects
pointers
pointers are use in c++ to access data in memory and dynamic memory. in java i think there is no such thing though i'm not sure.
delete[] / garbage collection
in c++ its always the job of the programmer to release allocated memory. in java, garbage collection is automatically done when data in memory has already no way to access it. though you can force a garbage collection or assigning null to an object.
terminologies
in c++ foo() is called function
in java its called method
field - in java a field is a member of a class that describes the object
in c++ foo<int>() is called template in java its called generics
oh i'll continue this later, i'll have my lunch first.