Java or C++?

Brian Vanderburg II BrianVanderburg2 at aim.com
Mon Apr 14 23:46:04 EDT 2008


> My idea, if you really love Python and never think about erasing it 
> from your mind, go for C (not C++). A script language plus C can solve 
> every problem you need to solve. Also Python works pretty fine with C.
I agree mostly with this one.  Scripting is very nice when writing an 
application especially one that needs to change very often.  Plus there 
are several toolkits and existing libraries available for Python.  I've 
used wxPython some with it, and it came in handy for a few applications 
that I would have normally written in C++/wxWidgets and have to 
recompile every few weeks to suit my needs (data extraction tools/etc)

However there are cases when a compiled language is better, especially 
anything that needs to be 'fast' and have a lower overhead.  I wouldn't 
use Python to create a very graphics intensive game or anything, though 
it can be used with pygame/PyOpenGL for some nice simple stuff. Also 
everything in Python is an object so it can start to consume memory when 
handling very large data sets.  And since there is no guarantee of when 
garbage collection occurs, simply 'deleting' an item does not ensure it 
is completely gone, especially if there are cyclic references, though 
that can be handled by using 'gc.collect()'.



I consider C++ just a simplification of C, in the sense that it makes it 
easier to do things that would take more work to be done in C.  One can 
still use C++ without all of the more complicated aspects but still take 
advantages of other aspects.

C has the advantage that it does not to anything behind your back.  This 
is very useful especially for any form of system development or where 
you must know exactly what is going on.  It is still possible to do 
'object oriented' development in C, it just requires some more typing to 
set up whatever is needed. Even things like COM for windows can be done 
in C, it just requires manually building the 'vtable' so to speak.  
Also, C seems to avoid the use of temporaries where as C++ can use them 
in conversions and assignments automatically if needed.

C++ makes some of it easier by doing certain things for you.  Take a 
string object for example.  In C, assignment would only do a memory copy 
operation:

String a, b;
b = a;

The statement 'b = a' would only copy the memory and pointers from 'b' 
to 'a' which means they would both point to the same buffer.  To avoid 
this, a copy constructor or assignment operator can be implemented when 
using C++.  The same in C would be something like:

String_Assign(&b, &a); /* instead of b = a */

Then if a structure contains objects, more work is needed.  For example, 
in C:

typedef struct Name
{
    String honorary;
    String first;
    String middle;
    String last;
    String lineage;
} Name;

void Name_Create(Name* name)
{
    String_Create(&name->honorary);
    String_Create(&name->first);
    String_Create(&name->middle);
    String_Create(&name->last);
    String_Create(&name->lineage);
}

void Name_Assign(Name* self, Name* other)
{
    String_Assign(&self->honorary, &other->honorary);
    String_Assign(&self->first, &other->first);
    String_Assign(&self->middle, &other->middle);
    String_Assign(&self->last, &other->last);
    String_Assign(&self->lineage, &other->lineage);
}

Name p1, p2;
Name_Create(&p1);
Name_Create(&p2);
Name_Assign(&p2, &p1);

But in C++, this is no problem:

Name p1, p2;
p2 = p1;

This will automatically call the constructors of any contained objects 
to initialize the string.  The implicit assignment operator 
automatically performs the assignment of any contained objects. 
Destruction is also automatic.  When 'p1' goes out of scope, during the 
destructor the destructor for all contained objects is called.

And if you want more control you can implement the default and copy 
constructors, destructor, and assignment operator, and tell them to do 
what you want.  Just beware because the explicit constructors only calls 
default constructors of any parent classes (even the copy constructor) 
unless an initializer list is used, and an explicit assignment will not 
automatically do assignment of parent classes.

Neither C nor C++ is really better, it depends on the what needs to be 
done.  C does only what it is told, and also has easier external linkage 
since there is no name mangling.  C++ does a lot of the needed stuff 
automatically, but explicit constructors and assignment operators can 
still be declared to control them, and frequently doing the same thing 
in C++ takes fewer lines of code than in C.

As for Java, I think it is 'overly-used' in some areas, especially in 
embedded development.  I attended NC State and during orientation this 
representative was talking about a small little robotic device and how 
it had a full Java VM inside it and it only took '6 minutes to boot'. 
Some claim it is for portability that Java is so good in embedded 
devices. But still, if the program is moved to another device, it may 
still need to be changed and recompiled if the new devices has different 
IO pins, timers, interrupts, etc. Most chips have a C 
compiler/translator available, and the same program could have been 
written in C, compiled directly to the machine code needed for the 
device, 'booted' immediately, and not needed a Java VM as well. If 
portability to other devices is desired then an abstract layer could be 
created in the program and the device/hardware specific code could be 
seperated to that layer seperatly from the rest of the program.

Well, all of that is just my opinion though, not meant to offend anyone.







More information about the Python-list mailing list