wood burning stoves 2.0*
The moose likes Java in General and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark ""new" keyword and objects in Java?" Watch ""new" keyword and objects in Java?" New topic
Author

"new" keyword and objects in Java?

Momen Travolta
Greenhorn

Joined: Mar 19, 2010
Posts: 8

I thought that only an instance of a class "is" an object; until I came across this sentence "the String literal is an object in its own right."

So,
1)What are other "things" considered to be an object in java?
2) Why is a string literal considered an object?
Vedha Vishali
Greenhorn

Joined: Mar 24, 2010
Posts: 6
Hi,

An object is cretead by the following statement:



In case of String:

There are two ways to create a String object in Java:



Each string literal is a reference to an instance of class String.

To cut down the number of String objects created in the JVM, the String class keeps a pool of strings.
Each time your code create a string literal, the JVM checks the string literal pool first.
If the string already exists in the pool, a reference to the pooled instance returns.
If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.

But, when you use


A String object is created out of the String literal pool, even if an equal string already exists in the pool.

Thanks,
VedhaVishali.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Momen Travolta wrote:1)What are other "things" considered to be an object in java?

Everything that is not a primitive (boolean, byte, short, char, int, long, float, double) or null. That includes strings. These will also all pass the "instanceof Object" test.

2) Why is a string literal considered an object?

If String literals did not exist the only way to create a new String would be using a char[]:
You see how 1) nasty that looks, and 2) tedious it is to write. Imagine having a String of 50 characters!
Because of this, and because String is used so very frequently, the String class has been given special treatment - you can declare it as a string literal. But in the end that's still an instance of the String class, and therefore an object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
In the following code:
str1 is another reference to the object referred by str ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Vedha Vishali wrote: . . .There are two ways to create a String object in Java:

. . .
Welcome to the Ranch
There are actually several other ways to create a String object, but your example as shown actually creates two (identical) String objects.
Vedha Vishali
Greenhorn

Joined: Mar 24, 2010
Posts: 6
@Campbell Thanks.

The two ways I actually meant without the new operator also we can create a String object.

@Yogesh



1.Creates a new String object "Java" in the String literal pool.
2.Creates a another new String Object out of the String literal pool and the reference variable str points to it.



1.In this step no String object creates.
2.The reference varaible str1 is made to point to the already available String Object("Java") in the literal pool.

Please correct me if am wrong.

Thanks,
VedhaVishali.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
I am confused

In the above code, according to what Campbell has said there are two identical objects ?

According to Vedha, Only one object with two references ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Vedha Vishali wrote:
1.Creates a new String object "Java" in the String literal pool.
2.Creates a another new String Object out of the String literal pool and the reference variable str points to it.

Step 1 is correct, but step 2 is not. Using the new operator will always create a new object. In this case, you create a new String object and the contents of the literal string in the pool is copied to that new object. The variable str will not point to the string in the pool, but to a separate String object that is not in the pool (the one that's created by calling new).

Yogesh Gnanapraksam wrote:In the above code, according to what Campbell has said there are two identical objects ?
According to Vedha, Only one object with two references ?

Campbell is right, Vedha is wrong...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jesper Young wrote: you create a new String object and the contents of the literal string in the pool is copied to that new object.


If I remembered correct, *String Literal Pool is collection of reference*. so in this case the Object ("pool") always creates on Heap . but when you say new String("Java")

1. create a object ("Java") on heap and which is referenced by StringLiteralPool

2. and new Operator create object which is the Identical copy of the StringLiteralPool reference Objcet agian which is lies on heap

<edit>so, there are 2 objects in heap. "Java"--->referenced by StringLiteral Pool , "Java"----> which is directly referenced by *str* variable[it may be in stack or heap]</edit>

Correct me , if I am wrong.
Vedha Vishali
Greenhorn

Joined: Mar 24, 2010
Posts: 6
@Jesper Young:

Even I explained the same.

2.Creates a another new String Object out of the String literal pool and the reference variable str points to it (Points to the new String Object created outside the literal pool).


Thanks,
VedhaVishali.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
@Vedha
1.In this step no String object creates.
2.The reference varaible str1 is made to point to the already available String Object("Java") in the literal pool.


I think you gave this explanation for a String initialization where 'new' is not used.
That is why I found your's and Campbell's to be contradictory .
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
I am quoting from HeadFirst Java
Whenever you make a new String,he JVM puts it into a special part of memory called 'String Pool'. If there is already a String
in the String Pool with the same value ,the JVM doesn't create a duplicate ,it simply refers your reference variable to the existing entry.
The JVM can get away with this because Strings are immutable;one reference variable can't change a String's value out from under another reference
variable referring to the same String.

The other issue with String pool is that Garbage Collector doesn't go there.


In my understanding of the above lines,the manner is which the String is initialized (using new or without new) does not make any difference.
If there is an identical object in the String Pool then a new String object is not created.

This is justified by the fact that the Garbage Collector doesn't go there. So if there are String objects without any references they still remain in memory and will be referenced when an identical String object reference is required.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Yogesh Gnanapraksam wrote:
In my understanding of the above lines,the manner is which the String is initialized (using new or without new) does not make any difference.
.


No. read this FAQ . specially look at the *Storage of Strings - The String Literal Pool*

hth
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Ok.Thanks Seetharaman,that cleared all my doubts


The above code creates two identical string objects.'String pool' has a a reference for the String literal object forever and so that literal object will not be available for garbage collection.


This has no effect for garbage collection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "new" keyword and objects in Java?
 
Similar Threads
Question about JOptionPane
How to create object to a specific class by using its name as string variable with out using new.
scea - 1 experiences
String objects
Performance factor in using equals Vs comareTo