File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes what is string   an object or literal ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is string   an object or literal ?" Watch "what is string   an object or literal ?" New topic
Author

what is string an object or literal ?

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi..

can any body tell me what is String an object or literal ?

i mean what is the difference when we declare string as

String a = new String("abc text");
String b= "xyz text";

???


and are call to functions in java are "call by refernce" or "call by value"

i mean if i pass say

someobject.someMethod("abc_text");
someobject.someMethod(a); // a declared above
someobject.someMethod(b); // b declared above

and here

someMethod(String s)
{
s="change in text"; // is that will change the original value ?
}


is that will change the original value ?

thanx and regards
amit


pls give answer in details so all dought of will be clear ....


Thanks and Regards, Amit Taneja
Mike DeStefano
Ranch Hand

Joined: May 24, 2002
Posts: 36
String is an Object


-Mike Destefano<br />"Sometimes you feel like a nut..."
Mike DeStefano
Ranch Hand

Joined: May 24, 2002
Posts: 36
Whether you declare it formally by saying

new String("blah blah blah")

or by saying

x = "blah blah blah"

This is the same thing. Java did this for convenience.

String is referenced by address and is not like and int or double (that is, not a primitive)... char is a primitive. String is simply an array of chars for the most part.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

There is a difference between String s = "abc" and String s = new String("abc"). Please see the following JavaRanch article regarding String literals...

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
[ December 29, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In Java, when an object reference is passed as an argument, what's really being passed is a copy of the reference. This copy becomes local to the method (so in that sense, it's a value); but it still points to the same object (so in that sense, it's a reference).


But remember, Strings are immutable. (See link above.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is string an object or literal ?
 
Similar Threads
Using the String
strings, string pool and garbage collection
Difference between s = "" and new String("a")
String literal comparisons - help!
A GC Problem in Mock Exam