给定以下 2 个类 Example1 和 Example2 并排除所有性能特征,这两个类的操作方式是否完全相同。也就是说,无论 methodA 或 methodB 多么简单或复杂,在所有可能的条件下(内部和外部)运行这两个类的结果是否总是完全相同?
示例 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
}
}