How java create objects implicitly? Like in case of String class

前端 未结 3 1494
悲哀的现实
悲哀的现实 2021-01-19 19:06

I can\'t understand how an object is created implicitly.

Example:

String s = \"implicit instantiation\";

Can I make my own class w

3条回答
  •  一整个雨季
    2021-01-19 19:41

    For every instance of an object you need a Constructor and a constructor its a a special method for construct and initialize methods. Example:

    String s;  // Is not initialized and it's nos constructed.
    

    So how do you construct a new object in java? Easy with the new operator you create a New Object!

    s = new String("qwe"); // New object constructed
    

    But here is something that a lot of newbies get confussed. Why i can do this:

    String s= "asdfasd;" 
    

    Because String is a special case in Java and you don't need to add a new operator like all the primitive variables that are classes. Example:

    Integer i = 3; 
    Double d = 3.3d;
    

    and So on.

提交回复
热议问题