Suppose you have this code:  static void Send(byte[] buffer, int offset, int count) {     while (count &gt; 0)     {         int size = sock.Send(buffer, offset, count);          if (size == 0) throw new EndOfStreamException();          offset += size;         count -= size;     } }   vs.  static void Send(byte[] buffer, int offset, int count) {     int size;      while (count &gt; 0)     {         size = sock.Send(buffer, offset, count);          if (size == 0) throw new EndOfStreamException();          offset += size;         count -= size;     } }   My teacher told me the second one would be more efficient, but is that true?  I did some testing and compared the assembly code and it was equal.  But will this always be the case?   What if instead of a primitive type you used a reference type? (assuming you don't allocate) What about using multiple variables? Will there ever be differences between the two possibilities?   Thanks in advance for any answers, I apologize if this has been asked before but I didn't really know how to call it.
1371
I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference?  A (quite pointless) example in Java:  a) declaration before loop:  double intermediateResult; for(int i=0; i &lt; 1000; i++){     intermediateResult = i;     System.out.println(intermediateResult); }   b) declaration (repeatedly) inside loop:  for(int i=0; i &lt; 1000; i++){     double intermediateResult = i;     System.out.println(intermediateResult); }   Which one is better, a or b?   I suspect that repeated variable declaration (example b) creates more overhead in theory, but that compilers are smart enough so that it doesn't matter. Example b has the advantage of being more compact and limiting the scope of the variable to where it is used. Still, I tend to code according example a.  Edit: I am especially interested in the Java case.
34146
This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations:  string hello = "hello";   vs.   string hello_alias = @"hello";   Printing out on the console makes no difference, the length properties are the same.
99917
If you declare variables of type byte or short and attempt to perform arithmetic operations on these, you receive the error "Type mismatch: cannot convert int to short" (or correspondingly "Type mismatch: cannot convert int to byte").   byte a = 23; byte b = 34; byte c = a + b;   In this example, the compile error is on the third line.
27673
I've been lately trying to learn more and generally test Java's serialization for both work and personal projects and I must say that the more I know about it, the less I like it. This may be caused by misinformation though so that's why I'm asking these two things from you all:  1: On byte level, how does serialization know how to match serialized values with some class?  One of my problems right here is that I made a small test with ArrayList containing values "one", "two", "three". After serialization the byte array took 78 bytes which seems awfully lot for such low amount of information(19+3+3+4 bytes). Granted there's bound to be some overhead but this leads to my second question:  2: Can serialization be considered a good method for persisting objects at all? Now obviously if I'd use some homemade XML format the persistence data would be something like this  &lt;object&gt;     &lt;class="java.util.ArrayList"&gt;     &lt;!-- Object array inside Arraylist is called elementData --&gt;     &lt;field name="elementData"&gt;         &lt;value&gt;One&lt;/value&gt;         &lt;value&gt;Two&lt;/value&gt;         &lt;value&gt;Three&lt;/value&gt;     &lt;/field&gt; &lt;/object&gt;   which, like XML in general, is a bit bloated and takes 138 bytes(without whitespaces, that is). The same in JSON could be   {     "java.util.ArrayList": {         "elementData": [             "one",             "two",             "three"         ]     } }   which is 75 bytes so already slightly  smaller than Java's serialization. With these text-based formats it's of course obvious that there has to be a way to represent your basic data as text, numbers or any combination of both.  So to recap, how does serialization work on byte/bit level, when it should be used and when it shouldn't be used and what are real benefits of serialization besides that it comes standard in Java?
6413
When programming interfaces, I've found I'm doing a lot of casting or object type conversion.  Is there a difference between these two methods of conversion?  If so, is there a cost difference or how does this affect my program?  public interface IMyInterface {     void AMethod(); }  public class MyClass : IMyInterface {     public void AMethod()     {        //Do work     }      // Other helper methods.... }  public class Implementation {     IMyInterface _MyObj;     MyClass _myCls1;     MyClass _myCls2;      public Implementation()     {         _MyObj = new MyClass();          // What is the difference here:         _myCls1 = (MyClass)_MyObj;         _myCls2 = (_MyObj as MyClass);     } }   Also, what is "in general" the preferred method?
13366
In , someone suggested in a  that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(int) * length);  rather than: int *sieve = (int *) malloc(sizeof(int) * length);  Why would this be the case?
89137
What would be better practice when giving a function the original variable to work with:  unsigned long x = 4;  void func1(unsigned long&amp; val) {      val = 5;             } func1(x);   or:  void func2(unsigned long* val) {      *val = 5; } func2(&amp;x);   IOW: Is there any reason to pick one over another?
23150
Can someone give an example of a good time to actually use "unsafe" and "fixed" in C# code? I've played with it before, but never actually found a good use for it.   Consider this code...  fixed (byte* pSrc = src, pDst = dst) {     //Code that copies the bytes in a loop }   compared to simply using...  Array.Copy(source, target, source.Length);   The second is the code found in the .NET Framework, the first a part of the code copied from the Microsoft website, .  The built in Array.Copy() is dramatically faster than using Unsafe code. This might just because the second is just better written and the first is just an example, but what kinds of situations would you really even need to use Unsafe/Fixed code for anything? Or is this poor web developer messing with something above his head?
11702
Some days ago I realized that  (as well as ) never throw an IOException when writing, flushing or closing.  Instead it sets an internal flag (trouble=true) when an error occurs. It's not possible to get the exact exception, but only if there was some exception ().  My question is: why would one want to have such behavior? Isn't that bad API design?
117969
I've been raised to believe that if multiple threads can access a variable, then all reads from and writes to that variable must be protected by synchronization code, such as a "lock" statement, because the processor might switch to another thread halfway through a write.  However, I was looking through System.Web.Security.Membership using Reflector and found code like this:  public static class Membership {     private static bool s_Initialized = false;     private static object s_lock = new object();     private static MembershipProvider s_Provider;      public static MembershipProvider Provider     {         get         {             Initialize();             return s_Provider;         }     }      private static void Initialize()     {         if (s_Initialized)             return;          lock(s_lock)         {             if (s_Initialized)                 return;              // Perform initialization...             s_Initialized = true;         }     } }   Why is the s_Initialized field read outside of the lock? Couldn't another thread be trying to write to it at the same time? Are reads and writes of variables atomic?
36269
In .NET why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision.
67471
Consider the following code:  void Handler(object o, EventArgs e) {    // I swear o is a string    string s = (string)o; // 1    //-OR-    string s = o as string; // 2    // -OR-    string s = o.ToString(); // 3 }   What is the difference between the three types of casting (okay, the 3rd one is not a casting, but you get the intent). Which one should be preferred?
86999
Admittedly I don't get it. Say you have a memory with a memory word of length of 1 byte. Why can't you access a 4 byte long variable in a single memory access on an unaligned address(i.e. not divisible by 4), as it's the case with aligned addresses?
47371
Let's say I have some Java code:  public class SomeClass {     static {         private final double PI = 3.14;         private final double SOME_CONSTANT = 5.76;         private final double SOME_OTHER_CONSTANT = 756.33;     }    //rest of class }   If a thread is initializing SomeClass's Class object and is in the middle of initializing the values in the static block when a second thread wants to load SomeClass's Class again, what happens to the static block?  Does the second thread ignore it assuming it's already initialized even though the first thread is not done? Or does something else happen?
92456
Alright, I have been doing the following (variable names have been changed):   FileInputStream fis = null; try {     fis = new FileInputStream(file);      ... process ...  } catch (IOException e) {     ... handle error ... } finally {     if (fis != null)         fis.close(); }    Recently, I started using FindBugs, which suggests that I am not properly closing streams.  I decide to see if there's anything that can be done with a finally{} block, and then I see, oh yeah, close() can throw IOException.  What are people supposed to do here?  The Java libraries throw too many checked exceptions.
109558
Are these two equivalent?  char * aString = "This is a string.";  char aString[] = "This is a string.";  From my understanding, C++ stores strings by their addresses, so the pointer is getting a valid address. I assume the strings are also stored as type char? But how does it know how much memory to allocate for the string? Normally, char arrays need their array size preset. I believe the [] type arrays are counted by the compiler before compilation. Is this what happens for the string literal pointer as well?  On to my second question, what is the difference between these two:  int someNumber;  int * someNumber = new int;  The second is supposed to be dynamic allocation. If I understand the concept right, it allocates the memory as soon as it reads the declaration. But what happens in the standard declaration? Basically, I need to understand how dynamic allocation is advantageous over standard declarations.
30943
I think there must be something subtle going on here that I don't know about. Consider the following:  public class Foo&lt;T&gt; {   private T[] a = (T[]) new Object[5];    public Foo() {     // Add some elements to a   }    public T[] getA() {     return a;   } }   Suppose that your main method contains the following:  Foo&lt;Double&gt; f = new Foo&lt;Double&gt;(); Double[] d = f.getA();   You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.  Can anyone tell me why? My understanding of ClassCastException is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:  Object o = new Double(3.); Double d = (Double) o; // Working cast String s = (String) o; // ClassCastException   And it seems I can do this. If a was just a T instead of an array T[], we can get a and cast it without a problem. Why do arrays break this?  Thanks.