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?