Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

String vs Object

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

String k="f";

is same as

String k=new String("f"); ??


If that is the case ,if a class is declared as follow

Class A
{

String z="z";
String y="y";
String x="x";


}

It says that Class A contain three String Object?

Class A
{

Object z="z";
Object y="y";
Object x="x";


}


which one is best if I do not want any String operation like concatation,substr,...etc
 
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

jacob deiter wrote:
String k="f";
is same as
String k=new String("f"); ??



No.

jacob deiter wrote:
If that is the case ,if a class is declared as follow
Class A
{
String z="z";
String y="y";
String x="x";
}

It says that Class A contain three String Object?

Class A
{

Object z="z";
Object y="y";
Object x="x";


}

which one is best if I do not want any String operation like concatation,substr,...etc



if you dont want string operation,then why you create it?
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No



please explain this
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = "x" creates string object at compile time and the object is referenced from the string pool.
String s = new String("x") create string object at runtime.

Well Object o = "x" is allowed in terms of polymorphism. You can use such types if you want to use one type only and then later can downcast when you really want to use that specific methods.
 
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

Nitish Bangera wrote:String s = "x" creates string object at compile time



Object Creation always in runtime
 
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

jacob deiter wrote:

No



please explain this



example,

when you say


there is only one object "abc" created in heap and two reference for the objects are placed in the string constant pool.

but here


there are 2 objects "abc" and "abc" are created in the heap though there are meaningfully same.

and s and t are pointing to the different object .

so always good practice is use String s = "abc" definition

Hope this helps
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals are compile time constants.

See here
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:String s = "x" creates string object at compile time



Well, actually all objects are created at runtime for the String literals it's when the class gets loaded by the JVM.
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:.. two reference for the objects are placed in the string constant pool.



No. SCP contains references to unique String literals. So whenever your program make a reference to that String object it is replaced with the reference to the object referenced from SCP. Check StringLiterals.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:String literals are compile time constants.

See here



Yes literals are compile constants , that doesn't mean String objects are created at compile time.

Literals are just "abc" or 'c' 1 or 133.9 ...
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

doesn't mean String objects are created at compile time



If you are creating string as literal then certainly string object will be created at compile time. Please refer this http://www.javaranch.com/journal/200409/Journal200409.jsp#a1 for further clarification.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shivendra tripathi wrote:

doesn't mean String objects are created at compile time



If you are creating string as literal then certainly string object will be created at compile time. Please refer this http://www.javaranch.com/journal/200409/Journal200409.jsp#a1 for further clarification.



Can you point out the bit in this article that says instances of objects are created a compile time? How are any instances of an object created at a point in the procedings where there is no JVM involved, and hence no heap to store these on?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for that. string object will be created at class loading time for string literal.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:

jacob deiter wrote:

No



please explain this



example,

when you say


there is only one object "abc" created in heap and two reference for the objects are placed in the string constant pool.

but here


there are 2 objects "abc" and "abc" are created in the heap though there are meaningfully same.

and s and t are pointing to the different object .

so always good practice is use String s = "abc" definition

Hope this helps




but the below code




return the same hash code value???
 
Marshal
Posts: 74069
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jacob deiter wrote:but the below code
. . .
return the same hash code value???

Read Object#equals(), Object#hashCode(), String#equals() and String#hashCode(), which should explain why you get the same hash code.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings and Wrappers have overridden equals and hashcode methods. This is the reason when we use Strings and Wrappers in maps, we don't need to do implement any methods.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is mean by String Mutable and Immutable
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shaiksha vali wrote:What is mean by String Mutable and Immutable



I don't get that statement . Strings are immutable
Now this is a bit off topic. Mutable means that you can change the data contained in the object once created. For String once you create an object of String you cannot change the value it holds (i.e: Immutable).
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get back to the original question:

You can reference methods of an object based on the variable type. For example, if you store a String in a String variable, you can run String methods on it. If you store the String in a variable of type Object, then you can only run Object methods.

For example:
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic