William M. answered 05/28/23
STEM Tutor (Ph.D, Northwestern) | Algebra–Calc, Bio, Chem, Physics, CS
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.
William M. answered 05/28/23
STEM Tutor (Ph.D, Northwestern) | Algebra–Calc, Bio, Chem, Physics, CS
It's true that it seems very redundant to repeat the type in a line such as
Scanner scan = new Scanner(System.in);
And that "new" can also seem redundant.
In fact, in some languages, both can go away, but that's just not how Java's language/compiler works. Kotlin is a cool language that is a sort of sequel to Java with a goal of being more succinct without being less readable; it removes the need to use "new" to call a constructor _and_ it even will do type inference to keep you from having to restate the type when declaring the variable. So in Kotlin, you might have something like
val scan = Scanner(System.in)
Actually, due to a quirk ("in" is a keyword in Kotlin), it has to be
val scan = Scanner(System.`in`)
but the point is that the "new" is missing (it's not even in the Kotlin language) and the type on the variable scan is optionally omitted. That "val" is a different feature of declaring variables in Kotlin, but let's ignore it.
But even then, in Kotlin, you still might want to specify the type of the variable and not rely on the compiler's type inference. I don't know if you've learned about polymorphism in Java, but the use of interfaces or inheritance hierarchies make it desirable to sometimes declare your variable of a different type than what you're calling "new" on. There are rules about this, and that's a larger discussion, but I'd like to point out that actually, this is a legal Java statement
Closeable scanner = new Scanner(System.in);
Note that scanner here is declared as typed as a Closeable. This is allowed because Scanner implements the Closeable interface.
So you see that some language authors have had the same insight you just had: maybe it would be nicer to leave off the "new" and the redundant typing! But even then, in a language that supports polymorphism, typing is likely a good thing when you need it, but not always.
Great question and insight!
Nicholas D. answered 04/09/23
B.S. in Computer Science. personalized tutoring in Java, and more
[updated to ensure that no matter where you look, you get a full answer]
Type:
The type is needed because java is a strongly typed language. When your code is being compiled it checks the type of every operation. This will essentially tell the compiler that 2 / "hello" is not possible, and thus your code will not compile, leading to a compile time error and not a run time error. This makes debugging fairly friendly.
[As Mark pointed out in the comments, 2 + "hello" is possible however this is not because you can add an integer and a string but instead it is because the + sign means both "add" and "concatenate" in java. When you say 2 + "hello" it treats the "2" as if it is a String NOT an integer. Java will implicitly convert datatypes to strings and even objects to strings according to their toString method]
New Keyword:
You need the new keyword because when you call the constructer of a class you are trying to create an instance of a class (in other words you are trying to create an object). The constructer of a class however is just a method, if you were to call a method you would execute a series of tasks but never create an object. But if you call the constructer method with the new keyword, it tells the program that you want to call this method with the intention of creating an object. Lastly the reason why you have to repeat the type, which in your example is Scanner, is because the name of the constructer method is the same name as the type you are creating. It is important to note that the new keyword is not needed in every language, however, in Java, due to how its compiler works, this is why it's necessary. Also, you will see examples of the type and the class being instantiated (the object which is created) not matching due to inheritance and polymorphism. For instance, you might have a class "Animal", and a class "Dog" that extends the animal class. In this case you can say Animal woofy = new Dog();
p.s.
You cannot call a constructer without the new keyword. I referred to it as "just a method" but keep in mind that it is not a normal method, it is a constructer method that has the purpose of doing a set of tasks and then creating an object based on the values that have been set by the constructer, and it also has the important role of allocating memory for the new object.
Also, keep in mind that as with most things in computer science the root of what is happening is always deeper. We could dive into Java's autoboxing for primitive datatypes, we could go deeper into allocation or inheritance and polymorphism, the history of java, etc. But these are the things that are necessary for most people, especially at this level.
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.04/10/23
Mark R.
04/10/23
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Mark R.
04/10/23