次の 2 つのクラス Example1 と Example2 があり、すべてのパフォーマンス特性を除外すると、これら 2 つのクラスはまったく同じように動作しますか。つまり、methodA または methodB がどれほど単純であるか複雑であるかに関係なく、これら 2 つのクラスをすべての可能な条件 (内部および外部の両方) で実行した結果は常にまったく同じになりますか?
例1
public class Example1
{
public static void main (String [] args)
{
try
{
// this will not compile since nextBoolean() is not static
// boolean t = java.util.Random.nextBoolean();
// changed to
java.util.Random r = new java.util.Random();
boolean t = r.nextBoolean();
if (t)
{
methodA();
methodB();
}
}
catch (Throwable t)
{
t.printStackTrace(System.out);
}
}
private static void methodB ()
{
// code goes here
}
private static void methodA ()
{
// code goes here
}
}
例2
public class Example2
{
public static void main (String [] args)
{
try
{
boolean t = java.util.Random.nextBoolean();
if (t)
{
methodA();
}
if (t)
{
methodB();
}
}
catch (Throwable t)
{
t.printStackTrace(System.out);
}
}
private static void methodB ()
{
// code goes here
}
private static void methodA ()
{
// code goes here
}
}