1.2.1.1 Differences between C++ and Java Data Types and Variables

Data Types

Number Representation: Like Java, C++ has theint and double types. However, with ints, the number of bytes an int variable takes in C++ depends on the machine, whereas in Java, an int variable takes exactly 4 bytes, no matter what hardware is used. For the purposes of this class, however, this difference is negligible, as most modern machines use 4-byte ints.

int  a; // this variable can range from -2³¹ to +2³¹ - 1
long b; // this variable can range from -2⁶³ to +2⁶³ - 1
char c; // this variable can range from -2⁷  to +2⁷  - 1

Unsigned Data Types: In C++, you can specify unsigned data types, whereas Java does not allow unsigned types. Recall that the first bit of an int, long, double, or even char is the "sign bit": it represents if the stored value is positive or negative. This is why, in both languages, a regular int (which contains 4 bytes, or 4*8 = 32 bits) ranges from -2³¹ to +2³¹-1 (31 bits to represent the magnitude of the number, and 1 bit to represent the sign). In C++, if you specify a variable to be an unsigned int instead of a regular int, its value will be able to range from 0 to +2³²-1.

unsigned int  a; // this variable can range from 0 to +2³² - 1
unsigned long b; // this variable can range from 0 to +2⁶⁴ - 1
unsigned char c; // this variable can range from 0 to +2⁸  - 1

Booleans: The equivalent of the Java boolean data type is simply bool. The usage of a C++ bool is the same as the usage of a Java boolean.

bool havingFun = true;

Strings: In C++, the string type is very similar to Java's String class. However, there are a few key differences. First, Java Strings are immutable. On the other hand, one can modify C++ strings. Next, the substring command in C++ is substr, where s.substr(i,n) returns the substring of s that starts at position i and is of length n. Also, in Java, String objects can be concatenated with any other object (and the other non-String object will automatically be converted to a String), but in C++, string objects can only be concatenated with other string objects (the conversion is not automatically done for you).

string message = "Hi, Niema!";
string name = message.substr(4,5); // name would have a value of "Niema"

Comparing Objects: To compare objects in C++, you simply use the relational operators == != < <= > >=. In Java, you only use the relational operators to compare primitives and you use .equals() to compare objects. But in C++, you can do something special. You can "overload" the relational operators, meaning you can specify custom methods that get called when the relational operators are applied to objects. Thus, in C++, you use relational operators to compare everything (including non-primitives). We'll talk about operator-overloading in more detail later.

string name1 = "Niema";
string name2 = "Liz";
bool sameName = (name1 == name2); // sameName would have a value of 0, or false

Variables

Next, we will discuss the differences between variables in Java and C++. Note that variables in C++ can actually be much more complex than what we cover here, but the more complicated details are out of the scope of this text. This is just to teach you the basics.

Variable Safety: In Java, if you declare a local variable but do not initialize it before you try to use it, the compiler will throw an error. However, in C++, if you declare a local variable but do not initialize it before you try to use it, the compiler will NOT throw an error! It will simply use whatever random "garbage data" happened to be at that location in memory! This can cause a LOT of headache when debugging your code, so always remember to initialize your variables in C++! Also, in Java, if you attempt to "demote" a variable (i.e., store it in a smaller datatype) without explicitly casting it as the lower datatype, the compiler will throw an error. However, in C++, the compiler will not complain! Below is perfectly valid C++ code in which we do both: we declare two variables and use them without initializing them, and we then demote to a smaller datatype without explicitly typecasting.

int harry; // dummy variable 1
int lloyd; // dummy variable 2
bool dumbAndDumber = (harry + lloyd); // C++ will allow this!

Global Variables: In Java, all variables must be declared either within a class or within a method. In C++, however, variables can be declared outside of functions and classes. These "global variables" can be accessed from any function in a program, which makes them difficult to manage, so try to avoid using them. (More about classes in C++ later.)

bool dummy = true;
class DummyClass {
    // some stuff here
};
int main() {
    cout << dummy; // this is valid
}

Constant Variables: In Java, a variable can be made so that it cannot be reassigned by using the final keyword. In C++, the equivalent keyword is const, though there are some subtle differences between Java's final and C++'s const. In Java, final simply prevents the variable from being reassigned. If the data itself is mutable, it can still be changed. In C++, for a variable that directly references (mutable) data, const will prevent that data from being changed. The C++ const keyword can be a bit tricky, and we'll discuss it in more detail later.

int main() {
    const string DUMMY_NAME = "Harry"; // DUMMY_NAME cannot be reassigned and "Harry" cannot be modified
    DUMMY_NAME = "Barry"; // This is not allowed! But if DUMMY_NAME were not const it would be OK
    DUMMY_NAME[0] = 'L';  // This is not allowed! But if DUMMY_NAME were not const it would be OK
}

Last updated