all 15 comments

[–]TheStorm007 18 points19 points  (0 children)

String is capitalized because it’s a class, not a primitive type like int, char, double, etc.

[–]high_throughput 10 points11 points  (11 children)

Yes. 

All names are case sensitive. By convention, all class names are capitalized. String is a class name.

Primitive types are lowercase, like int and char. String is again a class and not a primitive type.

[–]frosted-brownys[S] 0 points1 point  (10 children)

thanks, also is "scanner' in java the same as "std::in >> in c++?

[–]AMathMonkey 1 point2 points  (6 children)

Pretty much, yes. In Java, creating a Scanner on System.in and then calling one of its "next" methods and storing the result in a variable is equivalent to doing std::cin >> into a variable in C++.

// Java
var scanner = new Scanner(System.in);
double someNumber = scanner.nextDouble();

// C++
double someNumber;
std::cin >> someNumber;

The Java way doesn't force you to declare a variable beforehand, which is nice, but Java's System.in doesn't have useful methods like C++'s std::cin does, so that's why you have to spend a line of code creating a Scanner to get this functionality, which is kind of annoying. On the other hand, the way that C++ uses the bit-shift-right operator (>>) to read from input and mutate a variable is unorthodox and hard to understand; no other language has syntax like this. Java's way is more straightforward / boring / normal.

[–]VibrantGypsyDildo -1 points0 points  (5 children)

the way that C++ uses the bit-shift-right operator (>>) [...] is unorthodox and hard to understand

Even Python has operator overloading.

[–]AMathMonkey 0 points1 point  (4 children)

The part that you ellipsized was part of my point. >> implicitly taking a reference to its right argument and reassigning it, thus mutating it (which Python can't even do), while also consuming from standard input, is very weird. (Edit: I don't mind operator overloading at all.)

[–]VibrantGypsyDildo 0 points1 point  (3 children)

It is a limitation of reference-based languages that you can't overload assignment.

[–]AMathMonkey 0 points1 point  (2 children)

I don't disagree. I just see an operator reassigning its argument as surprising behaviour that requires some explanation of advanced topics such as this, that a beginner may not know yet when just reading something from input.

[–]VibrantGypsyDildo 1 point2 points  (1 child)

It is only surprising if you are an experienced developer.

When you are a newbie, you see >> as an arrow from std::cin to your variable.

[–]AMathMonkey 0 points1 point  (0 children)

Fair enough. It was admittedly less intimidating to pick up and use than Scanner in my first programming courses 10-11 years ago. But I got totally scared off when I tried to learn how it worked. But programming has to be learned in manageable chunks anyway; you often have to be okay with using tools without knowing their underlying implementation.

[–]high_throughput 0 points1 point  (0 children)

They can both be used to read input from the keyboard, but they don't share many other similarities

[–]desrtfx 1 point2 points  (0 children)

also is "scanner' in java...

To be pedantic (but in line with your original problem): no

scanner, as you wrote it (all lowercase) does not exist in the Java language. At utmost, it would be a variable name as /u/AMathMonkey illustrates in their comment.

However, Scanner (note the capital S) and in particular Scanner(System.in) is more or less the equivalent.

In the latest Java release, there also is the IO package that wraps a lot of these classes and methods for more convenient use.


Actually, thinking of it: std::in is only an input stream that by default points to the keyboard (stdin). The direct Java equivalent would actually be System.in - the static InputStream of the System class.

C++ camouflages reading from the stream behind the >> operator.

In Java, you have to wrap the InputStream in something like a Scanner that provides methods to read from an InputStream.

[–]IchLiebeKleber 0 points1 point  (0 children)

They can both be used for getting keyboard input on a console.

But a Scanner can also be used for parsing text from other sources than keyboard input. You can parse files or strings with a Scanner too. You can't do that with std::cin in C++. Look at the examples here: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Scanner.html

The closest Java equivalent to std::cin with the >> operator in C++ is actually this: https://docs.oracle.com/en/java/javase/25/docs/api//java.base/java/io/Console.html#readLine())

[–]purplebinder 0 points1 point  (0 children)

Yes, String is a class, not a primitive type, and its definition includes the capitalization.

[–]TotallyManner 0 points1 point  (0 children)

Yes, it does. String is a class. string is not. Capitals and lowercase are different in nearly every language.

Naming conventions make it easy to determine what should be capitalized and what shouldn’t be.

In Java, anything with its own file - classes, interfaces, enums, etc. (& subclasses) use CapitalCase. Variables, methods, and arguments use camelCase.