aspose file tools*
The moose likes Beginning Java and the fly likes String vs Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String vs Object " Watch "String vs Object " New topic
Author

String vs Object

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 02, 2008
Posts: 576
No


please explain this
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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


Object Creation always in runtime
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 15, 2009
Posts: 537

String literals are compile time constants.

See here
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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

Joined: Jan 01, 2009
Posts: 874

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

Joined: Aug 26, 2008
Posts: 263
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.


SCJP 1.5(97%) My Blog
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Sorry for that. string object will be created at class loading time for string literal.
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Jul 15, 2009
Posts: 537

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

Joined: Jul 12, 2009
Posts: 11
What is mean by String Mutable and Immutable
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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

Joined: Nov 06, 2000
Posts: 177
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:
 
Consider Paul's rocket mass heater.
 
subject: String vs Object