-4

I searched on SO. Saw one that said there are four ways in total.

The first of which is using the new keyword. The rest is more complicated.

But say you wanna construct a String object:

String object1= String("Bob")

If we leave the parameter the blank object will be null.

However the point is: can't we also do it in this way which is way simpler:

String object1="Bob";
  • Also I will like to know why sometimes when constructing a object, we can set our classes? – most venerable sir Apr 03 '15 at 22:04
  • You mean my questions in comment? Both are Java – most venerable sir Apr 03 '15 at 22:05
  • A blank (i.e. no) parameter in the `String` constructor does not create a null object - see the [documentation](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String%28%29). Your first line of code won't compile until you add the `new` keyword. – DNA Apr 03 '15 at 22:07
  • DNA, so why we can make up a class type of our own for constructors? String is a legit class. But it can also be momSavings. – most venerable sir Apr 03 '15 at 22:10
  • 2
    Not sure what you mean. Being able to create your own classes (with constructors) is kind of the point of an Object-Oriented language. Not sure how this relates to your question (and yes, you can create a `String` object with a literal such as `"Bob"`) – DNA Apr 03 '15 at 22:12
  • 1
    That's not a new object, that's a literal... – Willem Van Onsem Apr 03 '15 at 22:18
  • CommuSoft, so you mean any object has to be created with the new keyword. So in my example, basically I am just placing a new value/ a literal into object1, which was previously constructed with new keyword. Right? – most venerable sir Apr 04 '15 at 01:23

1 Answers1

4

There are at least 6 ways

  • using new keyword
  • using reflection (using newInstance method of Class class)
  • to create a copy of an existing object use clone method
  • using object deserialization to create from a serialized object typically used objectInputStream.readObject() is used
  • using getClassLoader
  • create using factory methods...

Anyways, regarding your example String object1 = new String("Bob"); it is equal to String object1 = "Bob";

Whenever it encounters a string literal (as in String object1 = "Bob";) in your code, the compiler creates a String object with its value, so in this case, String object1 = new String("Bob");, which is why they are equal.

abRao
  • 2,787
  • 1
  • 25
  • 37
  • `new String("Bob")` is not the same as `obj = "Bob"`. You can test it by printing result of `new String("Bob")==new String("Bob")` and `"Bob"=="Bob"` – Alexandru Severin Apr 03 '15 at 22:33
  • And at least most of the methods are forms of encapsulated *new*. Like `clone` and *factory*... – Willem Van Onsem Apr 03 '15 at 22:44
  • @Alexandru: When I run `String object1 = new String("Bob"); String object2 = "Bob"; if (object1.equals(object2)) System.out.println(" yes ");` - it does print 'yes'. I was trying to explain an object does get createdwhen you assign the literal. Maybe the word 'equal is better instead of 'same' - I will make that change – abRao Apr 03 '15 at 23:00
  • @CommuSoft: yes- most of them kind of wrapper (or encapsulate in your words) around `new`. Factory methods use internally` getInstance` which uses `new`; similarly `newInstance` uses `new` operator too.. The point here is what does the developer get to use to create an object – abRao Apr 03 '15 at 23:05
  • I didnot realize there's that many. Maybe my book is outdated. Where do you get info. on the latest version of Java? – most venerable sir Apr 04 '15 at 01:25
  • I'm having a confusion, isn't literal just whatever you assign into a variable of primitive type? – most venerable sir Apr 04 '15 at 01:31
  • regarding the info - I am not sure there is one place - but you can search the web - here is one http://crunchify.com/what-are-all-the-different-ways-to-create-an-object-in-java/ – abRao Apr 04 '15 at 01:40
  • A literal is a notation for representing a fixed value / atomic value. Even if you provide a string literal it has to be stored in a String Object. A quick search showed this link - http://java67.blogspot.com/2014/08/difference-between-string-literal-and-new-String-object-Java.html – abRao Apr 04 '15 at 01:50
  • I read the whole article the idea is that objects create from literal get auto-store in the string pool, which store strings. But what confuses is that it mention that java by default doesn't put all string object into string pool. You can if you want with .intern() method. And new objects does not get stored by default in the pool. So where do they get stored? – most venerable sir Apr 04 '15 at 20:06
  • String pool is JVM implementation. The literal assignment `"Bob"`, creates a string object that is stored in the pool. So the next time you do `"Bob"` - you get the *same* object from the pool, whereas if you do `new String("Bob")` - even if the pool has `"Bob"` a new explicit allocation outside the String pool occurs. You can think of String Pool as a cache - it is meant to help the JVM increase performance and decrease memory requirements. Also check this http://stackoverflow.com/questions/2486191/java-string-pool if my answer and comments have helped you - please accept my answer. – abRao Apr 05 '15 at 16:22