Introduction to strings and user input in C++ vs Java

You may be wondering about how to store text in variables. The closest that we’ve gotten to storing text is storing a single character. But what if you want to store multiple characters, say, a word or a sentence? Let’s learn how.

String – a primitive data type?

The reason that the word ‘string’ has been deliberately ignored in the previous lesson about data types is because it is not a primitive data type. It is essential that the programmer knows about how it is different from a primitive data type. Both C++ and Java treat it differently. We will discuss strings in detail in a later lesson. For now, let’s look at both languages one by one and see what they have to offer in a nutshell.

Strings in C++

In C++, string is a class defined in the <string> header file. The concept of a class will be explained in detail in a later tutorial. For now it will suffice for you to know that when the keyword string is being used before an identifier, we are not simply creating a variable but also allocating memory that a set of functions in that class can fully manipulate as per our liking.

Strings are declared and initialized in C++ as follows:

Strings in Java

Java also supports the use of strings. Strings in Java are objects of the String class. For now, all you need to know is that you can type,

to initialize a string without using any explicit import/include statements. Take note of the fact that the word ‘String’ starts with an upper case ‘S’ in Java.

Taking input from user in C++

Take a look at the simple C++ code below:

As you should have figured out, the program simply adds 2 numbers. However, from a user’s point of view, this program isn’t very useful. It always add 2 and 3 to store 5 in the variable total. A more useful program would ask the user for any two integers and then calculate the sum. Moreover, we won’t always write code where all values of variables will be known to us in advance. In such cases we will prompt the user for input and save the value that the user enters into a variable. In C++, the following statement prompts the user for input:

The following program adds two integers provided by the user:

Taking input from user in Java

In Java, the Scanner class is used in order to take input from the user. Unlike C++ where cin>> can be used to read input of any data type, the expected data type of the input must be specified in Java. Hence, the syntax completely depends on the expected data type.

Similarly, we can use sc.nextFloat(), sc.nextDouble()  or sc.nextLine()  to read a float, double or string value respectively.

sc.next() will only read a string until a whitespace is encountered whereas sc.nextLine() will read the entire line. So if the user inputs, “My name is Hedgy Monster” name1 will store just “My” whereas name2 will store “My name is Hedgy Monster”.

Conclusion

Now that you’ve learnt to read user input in C++ and Java, you can finally write interactive programs. Exciting, isn’t it?

You may also like: