Forums Register Login

"new" keyword and objects in Java?

+Pie Number of slices to send: Send

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
In the following code:
str1 is another reference to the object referred by str ?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
@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.
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
@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.
+Pie Number of slices to send: Send
@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 .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1675 times.
Similar Threads
How to create object to a specific class by using its name as string variable with out using new.
scea - 1 experiences
Performance factor in using equals Vs comareTo
Question about JOptionPane
String objects
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:27:53.