Asked • 04/09/23

why do I need the type and the new keyword with the type again in Java?

When creating an object why do I need the type and the new keyword in java, for example.


Scanner scan = new Scanner(System.in);


it seems repetitive to say Scanner twice.

3 Answers By Expert Tutors

By:

Nicholas D. answered • 04/09/23

Tutor
New to Wyzant

B.S. in Computer Science. personalized tutoring in Java, and more

Mark R.

tutor
Your point about Java's being a strongly typed language is true, and is a good thing to note! The comment that 2 + "hello" is not possible isn't exactly true, however. That will produce the Java String "2hello": it invokes the String concatenation operator ("+"), and that integer on the left (2) gets coerced to a String to perform the concatenation operation. In fact, a common Java trick to "stringify" a number -- convert an int, float, double, etc. to a String is to use the + concatenation operation with the empty string to perform the conversion: e.g. String stringifiedNumber = 2.5 + "";
Report

04/10/23

Nicholas D.

Correct! thank you for the correction. I chose the simplest mathematical operator for understanding and made a silly error but a proper example would be that 2 / "hello" is not possible since + is both a math operator and a concatenation operator.
Report

04/10/23

Mark R.

tutor
Makes sense!
Report

04/10/23

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.