Is there any difference in declaring array argument in java like:   public void method(Type[] arg) { ... }    and in that way:   public void method(Type arg[]) { ... }    Just curiosity...
5148
I have recently been thinking about the difference between the two ways of defining an array:   int[] array int array[]   Is there a difference?
23909
Looking for reviewers to suggest optimization and improvements, and to evaluate coding practices.  final class Variables {     private final int x;     private final int y;      Variables(int x2, int y2) {         this.x = x2;         this.y = y2;     }      public int getX() {         return x;     }      public int getY() {         return y;     }      @Override public String toString()  {          return "x = " + x + " y = " + y;     } }  public final class FindMissing {      private FindMissing() {};      /**  * Takes an input a sorted array with two variables that repeat in the  * range. Remaining variable are consecutive. and returns two variables that repeats  *   * eg: for a range of 1,2,3,4, a valid input is [1, 4], [1, 3] etc.  *   * @param a : the sorted array.  */ /*  * Why is start needed ?  * If start is not given then if a1 is empty how do we know if missing numbers were  * [1, 2] or [4, 5] ??.   */ public static Variables sortedConsecutiveTwoMissing (int[] a, int startOfRange) {     if (a == null) throw new NullPointerException("a1 cannot be null. ");      int low = startOfRange;     // calculating the highest value of the range. it needs to be 2 more than length since all elememts are consecutive.     int high = startOfRange + (a.length - 1) + 2;      int i = 0;     boolean foundX = false;     int x = 0;       while (i &lt; a.length) {         if (a[i] != low) {             if (foundX) {                 return new Variables(x, low);             } else {                 x = low;                 foundX = true;             }         } else {             i++;         }         low++;     }      return new Variables(x, high);  }  public static void main(String[] args) {         // unsortedConsecutiveTwoMissing         int[] a6 = {1, 4};         System.out.println("Expected 2 and 3, Actual " + sortedConsecutiveTwoMissing(a6, 1));         // unsortedConsecutiveTwoMissing         int[] a7 = {-4, -1};         System.out.println("Expected -3 and -2, Actual " + sortedConsecutiveTwoMissing(a7, -4));         int[] a8 = {-4, -3, -2, -1};         System.out.println("Expected 0 and 1, Actual " + sortedConsecutiveTwoMissing(a8, -4));         int[] a9 = {5};         System.out.println("Expected 4 and 6, Actual " + sortedConsecutiveTwoMissing(a9, 4));         }      }
16584
would report a violation for:  ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();   The violation was "Avoid using implementation types like 'ArrayList'; use the interface instead".  The following line would correct the violation:  List&lt;Object&gt; list = new ArrayList&lt;Object&gt;();   Why should the latter with List be used instead of ArrayList?
32884
If I define an array in PHP such as (I don't define its size):  $cart = array();   Do I simply add elements to it using the following?  $cart[] = 13; $cart[] = "foo"; $cart[] = obj;   Don't arrays in PHP have an add method, for example, cart.add(13)?
13251
Do you think there is a big difference in for...in and for loops? What kind of "for" do you prefer to use and why?  Let's say we have an array of associative arrays:  var myArray = [{'key': 'value'}, {'key': 'value1'}];   So we can iterate:  for (var i = 0; i &lt; myArray.length; i++)   And:  for (var i in myArray)   I don't see a big difference. Are there any performance issues?
48229
what about using "this" with methods in Java? Is it optional or there are situations when one needs to use it obligatory?  The only situation I have encountered is when in the class you invoke a method within a method. But it is optional. Here is a silly example just to show what I mean:  public class Test {      String s;      private String hey() {         return s;     }      public String getS(){         String sm = this.hey();         // here I could just write hey(); without this         return sm;     } }
26439
Is there any performance difference between the for loops on a primitive array?   Assume:  double[] doubleArray = new double[300000];   for (double var: doubleArray)     someComplexCalculation(var);   or :  for ( int i = 0, y = doubleArray.length; i &lt; y; i++)    someComplexCalculation(doubleArray[i]);   Test result  I actually profiled it:  Total timeused for modern loop= 13269ms Total timeused for old loop   = 15370ms   So the modern loop actually runs faster, at least on my Mac OSX JVM 1.5.
330
The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself).  Although it doesn't really matter at the end, I want to know what is the correct way to present the arguments passing conventions. Is there an excerpt from JavaScript specification, which defines what should be the semantics regarding this?
86340
It's known that you should declare events that take as parameters (object sender, EventArgs args). Why?
29653
I know Googling I can find an appropriate answer, but I prefer listening to your personal (and maybe technical) opinions. What is the main reason of the difference between Java and C# in throwing exceptions? In Java the signature of a method that throws an exception has to use the "throws" keyword, while in C# you don't know in compilation time if an exception could be thrown.
84232
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,  void deleteForMe(int* pointer) {      delete[] pointer; }   The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let's assume that we are indeed passing an array pointer,  int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; }   My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn't even know whether arr was dynamically created. Yet, if I do the following instead,  int main() {      int* num = new int(1);      deleteForMe(num);      return 0; }   The OS is smart enough to only delete one int and not go on some type of 'killing spree' by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string -- it will keep going until it hits 0).  So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the 'killing spree' scenario doesn't happen, so therefore in the practical world someone is remembering.)
2571
What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments? If int main() then return 1 or return 0?  There are numerous duplicates of this question, including:       — For C++, with a very good answer indeed.     Related:
12184
I'm trying to get an understanding of what the the java keyword this actually does. I've been reading Sun's documentation but I'm still fuzzy on what this actually does.
81333
Is there any performance reason to declare method parameters final in Java?  As in:  public void foo(int bar) { ... }   Versus:  public void foo(final int bar) { ... }   Assuming that bar is only read and never modified in foo().
107003
I'm working on a sparse matrix class that needs to use an array of LinkedList to store the values of a matrix. Each element of the array (i.e. each LinkedList) represents a row of the matrix. And, each element in the LinkedList array represents a column and the stored value.  In my class, I have a declaration of the array as:  private LinkedList&lt;IntegerNode&gt;[] myMatrix;   And, in my constructor for the SparseMatrix, I try to define:  myMatrix = new LinkedList&lt;IntegerNode&gt;[numRows];   The error I end up getting is     Cannot create a generic array of LinkedList&lt;IntegerNode&gt;.   So, I have two issues with this:   What am I doing wrong, and Why is the type acceptable in the declaration for the array if it can't be created?   IntegerNode is a class that I have created. And, all of my class files are packaged together.
14488
I saw this in , in reference to shortcomings of the Java spec:  There are more shortcomings and this is a subtle topic. Check  out: public class methodOverloading{      public static void hello(Integer x){           System.out.println(&quot;Integer&quot;);      }       public static void hello(long x){           System.out.println(&quot;long&quot;);      }       public static void main(String[] args){          int i = 5;          hello(i);      } }  Here &quot;long&quot; would be printed (haven't checked it myself), because the compiler chooses widening over auto-boxing. Be careful when using auto-boxing or don't use it at all!  Are we sure that this is actually an example of widening instead of autoboxing, or is it something else entirely? On my initial scanning, I would agree with the statement that the output would be &quot;long&quot; on the basis of i being declared as a primitive and not an object.  However, if you changed hello(long x)  to hello(Long x)  the output would print &quot;Integer&quot; What's really going on here?  I know nothing about the compilers/bytecode interpreters for java...
66573
I know that in JavaScript, creating a for loop like this: for(int i = 0; i &lt; arr.length; i++) is costly as it computes the array length each time. Is this behavior costly in c# for lists and arrays as well. Or at compile-time is it optimized?  Also what about other languages such as Java, how is this handled?