-9

Good Day,

My program requires a function as such: I need to check for a string on the server, and if it was successful, it will return a boolean true, and modify data, and if not place an error message in data and return false. This is the C++ way of doing things.

boolean getStringFromServer(String& data)

However, I need to do this in Java. Take not, I am working in a highly multi threaded environment. I heard you can use StringBuffer to pass in data and modify it. Can I actually get an example code?

soulslicer0
  • 69
  • 2
  • 8
  • you have a high negative because you show nothing that you tried and use an irrelevant example. String is immutable in java. you can not change it. There is no 'direct' pass by reference in java. – DwB Jul 01 '13 at 19:19
  • @Raaj You have _-8_, not _-6_ because your question is very low quality. – ST3 Aug 28 '13 at 19:25
  • how can i stop this question then? it keeps getting negative votes everyday? – raaj Sep 04 '13 at 19:36

3 Answers3

2
boolean getStringFromServer(StringBuilder sb)
{
  if(sb.indexOf("some magic string")!=-1)
  {
     //string found
     return true;
  }
  //not found
  //modify sb
  return false;
}

How you modify sb is up to you. Take a look at the java doc.

By the way, you cannot modify a String in Java because it is immutable.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • Yeah, the inability to modify a String parameter has nothing to do with the reference/value parm thing -- it's simply because Strings are immutable. StringBuffers and several other String-like objects are not immutable. – Hot Licks Jul 01 '13 at 17:44
  • Okay, lets say the above function exists in a static class/method in java. I acccess it via a thread call, i can have multiple threads calling the above method yes? this means different Strinbuilder objects will be passe din – soulslicer0 Jul 01 '13 at 17:54
  • @Raaj: If you pass different StringBuilder-Objects to the static method, then yes. – Burkhard Jul 01 '13 at 18:47
  • does stringbuilder get destroyed at the end of a function? Sorry I come from a C background this whole java thing is new to me – soulslicer0 Jul 02 '13 at 04:14
  • No it does not. It has been declared outside of the scope of this method. – Burkhard Jul 02 '13 at 05:05
0

In response to what some say, Java is entirely pass by value. Even when people say arrays and objects pass by reference, what they really mean is that arrays' and objects' references are passed by value. For example,

   public static void main(String[] args)
   {
        int[] numbers = new int[]{1,2,3};
        change(numbers);
        System.out.println(Arrays.toString(numbers)); //prints [1,2,3]
   }

   public static void change(int[] ra)
   {
        ra = new int[]{4,5,6};
   }

Nonetheless, that's not the issue here. Strings are immutable. This is because you have no way to access their indices to change change them (you can use charAt() to see what char is at a specific index, but have no way to change the char), since the char[] that the String class uses hold the characters is private and there are no setter methods. Additionally, that private char[] is also final so it can never be assigned an additional reference.

As another user suggested, you can use a StringBuilder to accomplish what you want.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
0

Here are a few things to remember:

  1. In java, you can not change the value of a String.
  2. In java, you can change the value of a reference to a String.
  3. In java, all references are passed by value. This means that you can change the value of a String parameter inside a method, but it reverts to the original value when the method exits.

This is a bad technique, but here is a simple solution using said technique:

in file named HootBerry

public class HootBerry
{
  private String value;

  HootBerry(final String initialValue)
  {
    value = initialValue;
  }

  public String getValue()
  {
    return value;
  }

  public void setValue(final String newValue)
  {
    value = newValue;
  }
}

in some other file

public boolean blammo(final HootBerry data)
{
  if *data is blammo* // use approparite java code here
  {
    return true;
  }
  else
  {
    blammo.setValue("data is not blammo");
    return false;
  }
}

A "better" solution is to return an object that has both the boolean and the error message.

DwB
  • 37,124
  • 11
  • 56
  • 82