class - Are these java classes equivalent? -


given following 2 classes example1 , example2 , excluding performance characteristics, these 2 classes operate exact same way. is, regardless of how simple or complex either methoda or methodb or can be, result of running these 2 classes, under possible conditions (both internal , external), absolutely same?

example1

public class example1 {     public static void main (string [] args)     {         try         {                // not compile since nextboolean() not static             // boolean t = java.util.random.nextboolean();              // changed             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     } } 

example2

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     } } 

the results not guaranteed absolutely same, although in general be. specifically, can write methoda , methodb implementation yield different results when run in example1 , example2, if class names of main program made same before execution.

one way accomplish generate stack trace , introspect on line number execution of methodb, different in example1 , example2.

for example, below methodb result in different output when run in example1 , example2.

public static void methodb() {     int count = 0;      stacktraceelement[] elements = thread.currentthread().getstacktrace();     (stacktraceelement element : elements)     {         count += element.getlinenumber();     }      system.out.println(count); } 

however, in general programs yield same results since type of logic based on stack traces or other such aspects unusual.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -