• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"new" keyword and objects in Java?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code:
str1 is another reference to the object referred by str ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You got style baby! More than this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic