• Post Reply Bookmark Topic Watch Topic
  • New Topic

how many ways to create an object in java  RSS feed

 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to know how many ways we can create object in Java?
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • with the new operator

  • List<String> them = new ArrayList<String>();

  • with the + operator

  • String tolog = (new java.util.Date()) + " " + getLogLevelDescrition() + " " + myObjectToLog;

  • with serialization

  • Object read = stream.readObject();

  • by cloning



  •  
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16057
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is a frequently asked question - every now and then somebody asks the very same question. I guess this is a question that some company uses for job interviews. You'll find previous answers with a search:

    What are the different ways to create an object in Java?
    In how many ways we can create an object? could you Explain with example if possible.
    How many ways of creating objects in Java?
    Ways to create an object?

    Ivan Jozsef Balazs wrote:
  • with the + operator

  • String tolog = (new java.util.Date()) + " " + getLogLevelDescrition() + " " + myObjectToLog;

    I don't agree with this one, it's not the + operator that creates the object in this case. Note that the above syntax is just syntactic sugar for something like this:

    You'll see that when you compile the code and then decompile it with javap -c
     
    Jaikant Singh
    Greenhorn
    Posts: 16
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    String str="Jai"+""+"Singh";

    In above statement according to me 3 objects will be create because "" does not create object.
    correct me If I am wrong?

    Thanks in advance.
     
    Jaikant Singh
    Greenhorn
    Posts: 16
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I know five ways to create objects in Java

    i)using new Operator
    ii)using Class.forName() method
    iii)using clone() method
    iv)using object deserialization
    v)using getClassLoader().loadClass()

    am i missing some other ways?
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jaikant Singh wrote:String str="Jai"+""+"Singh";

    In above statement according to me 3 objects will be create because "" does not create object.
    correct me If I am wrong?



    Personally, I wouldn't worry about it. Java is a bit more complex than that -- with lots of optimizations to worry about, if you go down this rabbit hole.

    ... but ... to answer your question. The java compiler is smart enough to do the concat at compile time -- basically the concatenation of two compile time constants is a compile time constant. It will be as if you coded the line like this...



    Henry
     
    Jaikant Singh
    Greenhorn
    Posts: 16
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Henry,

    String str="Jai"+""+"Singh";


    Total no. of objects in this case?

    Thanks.



     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Only one String is stored for this in the constant pool because the expression is evaluated during compilation, akin to this:


     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jaikant Singh wrote:Note that the above syntax is just syntactic sugar for something like this:



    Well, if taken so, the operator "new" is also a piece of syntactic sugar for some gory operations under the hood.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ivan Jozsef Balazs wrote:
    Jaikant Singh wrote:Note that the above syntax is just syntactic sugar for something like this:



    Well, if taken so, the operator "new" is also a piece of syntactic sugar for some gory operations under the hood.



    Actually, I don't think so. I believe something is "syntactic sugar" if it can be completely removed (and replaced with other code that is more complex) and the compiler will generate the exact same binary. There is nothing that you can replace the "new" operator with to generate the exact same binary.

    Henry
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Henry: you are right.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!