What is the difference? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

What is the difference?

A a; A a=new A() ;

28th Feb 2018, 7:51 AM
Milan Srdić
Milan Srdić - avatar
4 Réponses
+ 8
A a; - just declare object A a = new A(); - declare and initialize object at the same time
28th Feb 2018, 11:55 AM
Vukan
Vukan - avatar
+ 7
A a; // you declare a variable a of type A with nothing in it A a = new A() ; // you declare a variable of type A and initialize it with a new Object of type A. a.dosomething() in the first case would throw a nullpointer exception and the second would execute the method dosomething
28th Feb 2018, 7:56 AM
Chrizzhigh
Chrizzhigh - avatar
+ 3
A a; declare object "null" A a = new A(); declare and initialize object "allocated memory"
28th Feb 2018, 12:11 PM
Ali Abdulbasit
Ali Abdulbasit - avatar
+ 2
A a creates the variable new A(); creates the object
28th Feb 2018, 8:32 AM
Kai Schlegel
Kai Schlegel - avatar