This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String vs Object

 
jacob deiter
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No


please explain this
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals are compile time constants.

See here
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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 ...
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 580
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
shaiksha vali
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is mean by String Mutable and Immutable
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
Max Rahder
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic