Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

In Java, is a string an object? 1

Status
Not open for further replies.

wellas666

Programmer
Feb 10, 2006
23
GB
Hi,

This should be very simple, but I am a beginner.

1. An object is passed to a method by reference?
2. A string is an object?
3. Hence, a string is passed to a meshod by reference?

public class Try
{
public static void main(String[] args)
{
String s="222222";
System.out.println(s);
System.out.println(test(s));
System.out.println(s);
}
static String test(String s)
{
s += "11111111";
return s;
}
}
 
Thank you for the interesting links.

However, still need your explanation:

A string is an object?
In the above method, static String test(String s), s is the reference of a String? Any change of s in the method will change the value of s in the main?
 
A String is an Object.
Strings are immutable.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj,

Thank you for your response.

If a String is unchangeable, there must a copy of the string, not the copy of its reference, in the above method (so that it can be changed in the method while not affect the string in the main). That means an object (the string) is copied in the method!
 
Not exactly. Objects are always passed by value, understanding the value of an object as just a kind of pointer to it.

There's a nice characteristic of Java called encapsulation: that means you can just access the parts of the object the designer decided to.

If you have a String, you cannot change its content with the methods it has, you can just get the information. Of course, if your String is local, you can use the = operator to assign a new value.

But since you got a "pointer" to the String, if you change that reference, you lost the original object.

Cheers,
Dian

 
Hm.
1. A method gets a copy of a reference to the object in Java.
2. Strings are immutable, which leads easily to confusion.

Let's explain 1. first.
Code:
foo (Manager m)
{
     m.setSalary (0); 
/*
     since m is copy of the reference to a Manager-object, 
     that managers salary is set to 0.
     The calling context, who has its own reference to the          
     same object is affected.
*/
     m = new Manager ("Hugo", 5000); 
/*
     m is now a reference to a different manager-object.
     The calling context, who has its own reference to the          
     old object, is not affected.
*/
}
Now to something complete different: Strings.
Code:
String s = "foo";
s += "bar";
It looks as if s is modified.
But in fact, s in line two is new objekt of type String.
The first object is not reachable any more.

When you look at the modifying methods of class String, you'll see, they're all returning a (new) String with those modifications, leaving the original String unaffected.
Code:
String s = "foo";
String m = s.replaceAll ("o", "i");
// nothing is replaced in s itself.

seeking a job as java-programmer in Berlin:
 
Remember that the += operator used with String objects is a shorthand way of the following...
Code:
static String test(String s)
{
    s = s.concat("11111111");
    return s;
}
As mentioned above, String objects were designed to be immutable so that the object will never be modified.

Try some similar code using a StringBuffer object when exploring how Java passes objects to methods. But remember that the += shorthand & the s="222222" shorthand only work with the String class. e.g...
Code:
public class Try
{
    public static void main(String[] args)
    {
        StringBuffer s = new StringBuffer("222222"); [COLOR=grey]//edited[/color]
        System.out.println( s );
        System.out.println( test(s) );
        System.out.println( s );
    }
    static StringBuffer test(StringBuffer s) [COLOR=grey]//edited twice[/color]
    {
        s.append("11111111"); [COLOR=grey]//edited[/color]
        return s;
    }
}
 
OK, it passes the value of the reference, not the value of the object into a method. When the new reference (in the mothod) pointes to another string object the original reference remains pointing to the original object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top