我正在尝试使用整数,输出让我感到害怕。
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);
}
}
现在,Integer作为 的包装类int,并count作为它的对象,当我count从 main 传递到 时returnBool,的值count变为 3,所以它返回true,因为 java 是按值传递的,count对象的值也应该在main方法中改变,但main count打印为 0。
我想了解为什么会这样?