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

前端 未结 3 1496
悲哀的现实
悲哀的现实 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:32

    Unfortunately you just can not do that!

    opposite to C or C++ you can not overload any operator in java language, so there is no possible way to do something like

    Foo myFoo = 1
    

    in the case of the string class:

    String s = "implicit instantiation"
    

    that is sugar sintax for the developers, behind the scenes is the compiler doing the "dirty" work and doing something like (remember there is a string pool):

    String s = new String("implicit instantiation")
    

    The same applies for some other Types like Arrays, or wrapper for numbers...

提交回复
热议问题