0

I was trying something with Integer, and output is freaking me out.

public class CountB
{

  public static boolean returnBool(String w, Integer count)
  {
     for (int i = 0; i < w.length(); i++)
     {
         if (w.charAt(i) == 'B' || w.charAt(i) == 'b')
        {
       count++;
        }
     }

    if (count > 0)
      return true;
    return false;
   }

  // write the static method “isThisB” here
  public static void main(String[] args)
  {
    //  Scanner keyboard = new Scanner(System.in);
   //   System.out.println("Enter a string: ");
   String w = "fgsfbhggbB";//keyboard.nextLine();
   Integer count = new Integer(0);
   System.out.println(returnBool(w, count));
   System.out.println("Number of B and b: " + count);
  }
}

Now, Integer being a wrapper class of int, and count being its object, when I pass count from main to returnBool, value of count becomes 3, so it returns true, as java is pass by value count object value should change in main method also, but in main count prints 0.

I want to understand why this is happening?

codingenious
  • 8,385
  • 12
  • 60
  • 90
  • 5
    Java is always [**pass by value**](http://stackoverflow.com/q/40480/2024761) (Period) – Rahul Nov 08 '13 at 09:37
  • 4
    `Integer` objects are immutable. Whenever you think you might be changing the value of an `Integer`, what you're _really_ doing is getting a new one. – Dawood ibn Kareem Nov 08 '13 at 09:40
  • You should just return the count. Java doesn't support pass by reference, never has. You can simulate it but it is ugly and I suggest you read the answers given in the last question which shows you how you should do this. – Peter Lawrey Nov 08 '13 at 09:40
  • Sorry I confused you for this person who ask about the same homework http://stackoverflow.com/questions/19855327/searching-for-specific-character-in-string-with-static-method/19855458 – Peter Lawrey Nov 08 '13 at 09:42
  • Yeah I know, java is pass by value, somehow reference was in mind and I typed pass by ref. @Marko has explained what I was looking for. :) – codingenious Nov 08 '13 at 10:04

3 Answers3

11

count++ is just a convenience for

count = Integer.valueOf(count.intValue()+1)

After this operation, your local variable count (in returnBool) refers to another object, and the local variable in your main method keeps pointing to the initial object. You haven't achieved pass-by-reference.

As for Java semantics, there are two similar concepts which are easy to confuse: Java's pass-by-value of object references (in essence, pointers), and true pass-by-reference. Your example stresses this difference.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Java passes the class Integer by Value and not by Reference. If you want to pass it by Reference you'll need an other class like org.apache.commons.lang.mutable.MutableInt from Apache Commons library

bluelDe
  • 395
  • 8
  • 17
0

There is no pass-by-reference in java. Method parameters are passed by value but that value may be a reference to an object. If the passed object is mutable the changes on it will affect the object outside the method since the object is the same in and out. Integer objects are immutable. You can pass int[1] or AtomicReference or AtomicInteger or any other object that contains a mutable integer value.

This is your code adapted to AtomicInteger

public class CountB
{

  public static boolean returnBool(String w, AtomicInteger count)
  {
     for (int i = 0; i < w.length(); i++)
     {
         if (w.charAt(i) == 'B' || w.charAt(i) == 'b')
        {
       count.incrementAndGet();
        }
     }

    if (count.intValue() > 0)
      return true;
    return false;
   }

  // write the static method “isThisB” here
  public static void main(String[] args)
  {
    //  Scanner keyboard = new Scanner(System.in);
   //   System.out.println("Enter a string: ");
   String w = "fgsfbhggbB";//keyboard.nextLine();
   AtomicInteger count = new AtomicInteger(0);
   System.out.println(returnBool(w, count));
   System.out.println("Number of B and b: " + count);
  }
}
aalku
  • 2,860
  • 2
  • 23
  • 44