Monthly Archives: April 2012

C++ and Java

This semester has been an enlightening one to be sure, although every semester of CS has felt that way (probably a sign I’m in the right major).  Object-Oriented Programming has exposed me to some fundamental differences between Java and C++ that shed more light on the process of choosing a suitable language for a task than my Programming Languages class did.  I spoke to an early-thirties itinerant programmer this weekend and he remarked that when templated programming was introduced it was amazing, but very few people know how to use it.  He also mentioned with some contempt that Java templates are actually quite different from (and to him, inferior to) C++ templates.

As I felt at the beginning of the semester, I don’t see myself programming in C++ any time soon, but at least those feelings are better-informed now.  Also, the simultaneous analysis of C++ and Java gives me a better picture of the underpinnings and hidden costs of certain features that I just treated as costless abstractions in Java (constructors and destructors, garbage collection, etc).

To conclude, here are three esoteric facts that I learned today, related to the differences between C++ and Java, courtesy of Wikipedia:

  • Java’s bounds checking can be turned off using HotSpot
  • C++ has standardized minimum limits for all numerical types, but the maximum is implementation-dependent; Java is entirely standardized for numerical type limits
  • C++ supports goto statements; Java has break and continue loops that allow some goto functionality

The Game of Life Continues

Being the final project, Life has fittingly been the most fascinating we have worked on.  There is significant design liberty and a lot of places where you can end up shooting yourself in the foot.  I’m glad my partner has a better understanding of Friend classes and virtual methods than I do, because at least one of our problems have been solved by simply adding Friend class Life to our AbstractCell, a fix I would not have been able to make.

Last week we learned the mysterious syntax of virtual method() = 0, used to implement abstract classes.  Seeing something this foreign makes me wonder if C++ is a dramatically less intuitive language than Java or if my intuition is only capable of seeing things relative to Java.  I am inclined to give Stroustrup the benefit of the doubt for creating the language to be readable as well as powerful.  I guess that’s just another tradeoff you get from moving between C++ and Java.

The papers for this week were interesting.  That women in CS often feel less confident as a result of not having much exposure to or encouragement towards the discipline at an earlier age is not a surprise.  I have sometimes felt the same way, given that I am in the minority of CS students who took no CS classes in high school.

Downing’s Game of Life

Conway’s Game of Life is the final project for OOP.  I would not have expected this coming into the class.  It is the prototypical CS assignment, but that doesn’t mean it’s easy.  The idea of programming Game of Life in C++ at the beginning of the semester was unfathomable.  I began work with my partner this weekend and we got the foundation of the program, but halted when we had to start understanding handles, figuring it would be more time-efficient to wait for a clear explanation of them from Downing rather than struggle this early.

I have begun to study for the final exam.  Repetition is the only way to learn.  The material has gotten very complex, and it seems like whenever I learn something new, some existing piece of knowledge is evicted.  As soon as I began to understand explicit, friends, and operator overloading, I forgot the most basic series of constructor calls we could be quizzed over.

Templates are fascinating and they make me better understand why “Polymorphism” is a tenet of OOP.  The further we delve into C++’s idiosyncrasies, the more I wonder how much of this confusing machinery is used in industry code.

Bottoms Up

For the Darwin project, I worked with a Masters student from India.  It was a completely different pair programming experience than anything I’ve had in the past.  He told me that things are very different in India.  Computer Science is taught almost strictly bottom-up; starting with chemistry and physics, then moving on to transistors and EE material, then assembly, then C and C++.  This is in contrast to UT’s program, which seems more top-down; the first course I took was in Java.

While he drove, our C++ code expanded at more than twice the pace it would have if I would have been driving.  The language is as second-nature to him as Java is to me.  However, judging by our conversations, it is easier to move from a C++ paradigm to Java than vice versa.  Does this speak to a deficit of the UTCS curricular arrangement?

As we blazed through the assignment, I asked him this.  He said that the problem with the bottom-up approach is that he felt pigeonholed into systems programming.  This makes sense, as I feel pigeonholed into writing greedy, garbage-collecting Java and Ruby programs.  But then again, I wouldn’t have it any other way.  I like to think of modern memory sizes as a margin of error for inefficiencies, not something to be frequently acknowledged with constructors and destructors.

As the semester winds down and final projects are assigned in my two C++ courses, I am looking forward to waving goodbye to the language, at least for awhile.

Bjarne’s World

I used partner.random() on this project and was returned another very capable programmer.  The first language he learned was C++, and it’s been interesting to watch someone who explored the basics of programming and overcame many of its hazards while working in a different paradigm.  The disadvantage is that with my past partners, the subtleties of C++ often frustrated both of us, and led to conversations that forced us to discuss and research solutions together.

I learned an interesting C++ nuance this week.  In order to give an assignment to a class reference variable, you must use syntax that looks foreign to a Java programmer such as myself.  If you have a class int reference variable r, and want to pass your constructor a reference to assign it, the following is valid:

MyClass(int arg&) : x(arg){

//other assignments


This is because an empty reference cannot be declared and later bound to the argument; you must specify to the compiler that you are initializing this reference as you declare it.

As I have learned about the intricacies of C++, my respect for Bjarne Stroustrup has increased.  I decided to read up on him and, finding his Wikipedia sparse, came upon this recent interview:

Bjarne: From C, C++ got its direct and efficient model of the machine, which I think has been and is essential for its success as a systems programming language.That is something I would not change in retrospect. From C, C++ also got the messy declarator syntax,the error-prone narrowing conversions, and arrays with array-to-pointer conversions that seriously complicates range checking.These three features would not be in my ideal language.The idea of declarators is sound enough,but the syntax is an artifact of ancient parser technology and the Draconian demands of really small memories.

It’s comforting to know that even the language’s own creator dislikes aspects of its syntax that I find confusing.