This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
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

what is string an object or literal ?

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ....
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is an Object
 
Mike DeStefano
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic