Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

What is the purpose of "new" in Java?

Hey guys,

What is the purpose of using "new" when we instantiate a variable? I can't seem to find any resources online that can explain it to me in a succinct way... any help would be greatly appreciated! :)

3 Answers

The new keyword is a Java operator that creates the object. For more info follow this official oracle doc : Creating Objects : The Java™ Tutorials

Thank you so much for that :) However I am still confused with this example:

System.out.println("Enter cost of meal in US Dollars: "); Scanner keyboard = new Scanner(System.in); double answer = keyboard.nextDouble();

Why is scanner an object when it is simply asking for the cost of meal?

Thanks :)

System.out.println("Enter cost of meal in US Dollars: "); // it is print the text in paretesis

Scanner keyboard = new Scanner(System.in); // this object is for receive input "System.in"

double answer = keyboard.nextDouble(); // nextDouble is a method of Sanner object that will save you input ins answer in double format