This class ds has two fields, x and y  public class ds {  private int x; private int y;  public ds(int xx, int yy) {     x = xx;     y = yy; }  public int getX() {     return x; }  public int getY() {     return y; }  public void setX(int xx) {     x = xx; }  public void setY(int yy) {     y = yy; }  }   This class ptrs uses ds to print results. I note that the printouts are the same whether int or Integer is used in ds.  public class ptrs { public static void main(String[] args)     {     ds d = new ds(1,2);      System.out.println("d.x:" + d.getX()); //1     System.out.println("d.y:" + d.getY()); //2      //t is an object reference (a pointer) which points to d, which in turn      points to the ds object     ds t = d;      System.out.println("t.x:" + t.getX()); //1     System.out.println("t.y:" + t.getY()); //2      t.setX(3);     t.setY(4);      System.out.println("");      System.out.println("t.x:" + t.getX()); //3     System.out.println("t.x:" + t.getY()); //4       System.out.println("d.x:" + d.getX()); //3     System.out.println("d.y:" + d.getY()); //4      d.setX(5);     d.setY(6);      System.out.println("");      System.out.println("d.x:" + d.getX()); //5     System.out.println("d.x:" + d.getY()); //6      System.out.println("t.x:" + t.getX());  //5     System.out.println("t.x:" + t.getY());  //6     } }   When I call the set methods on either d or t, calling the get methods on either pointer results in the updated values. Why is there apparently different behavior in the next example?  public class main     {     public static void main(String[] args)         {         Integer i = new Integer(5);         Integer a = i;          System.out.println("i is " + i ); //5         System.out.println("a is " + a ); //5          i = new Integer(10);          System.out.println("i is " + i ); //10          System.out.println("a is " + a ); //5         }     }   If I set an Integer object reference i to point to an Integer object with value 5, and then make another reference a refer to i, why does a still point to 5 even after I make i reference another Integer object with value 10?  Is the difference because in the first example I change fields of the same object, but in the second example I point to a new object? But why would that cause a difference, if that is the reason..?
121
I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, ) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation?
7653
public class doublePrecision {     public static void main(String[] args) {          double total = 0;         total += 5.6;         total += 5.8;         System.out.println(total);     } }   The above code prints:  11.399999999999   How would I get this to just print (or be able to use it as) 11.4?
4641
I have a question about the primitive type short in Java. I am using JDK 1.6.   If I have the following:   short a = 2; short b = 3; short c = a + b;   the compiler does not want to compile - it says that it "cannot convert from int to short" and suggests that I make a cast to short, so this:  short c = (short) (a + b);   really works. But my question is why do I need to cast? The values of a and b are in the range of short - the range of short values is {-32,768, 32767}. I also need to cast when I want to perform the operations -, *, / (I haven't checked for others).  If I do the same for primitive type int, I do not need to cast aa+bb to int. The following works fine:  int aa = 2; int bb = 3; int cc = aa +bb;   I discovered this while designing a class where I needed to add two variables of type short, and the compiler wanted me to make a cast. If I do this with two variables of type int, I don't need to cast.   A small remark: the same thing also happens with the primitive type byte. So, this works:  byte a = 2; byte b = 3; byte c = (byte) (a + b);   but this not:  byte a = 2; byte b = 3; byte c = a + b;   For long, float, double, and int, there is no need to cast. Only for short and byte values.
120685
It seems to me that a lot of my debugging time is spent chasing down null-reference exceptions in complex statements. For instance:  For Each game As IHomeGame in _GamesToOpen.GetIterator()   Why, when I get a NullReferenceException, can I get the line number in the stack trace, but not the name of the object that equals null. In other words, why:  Object reference not set to an instance of an object.   instead of  _GamesToOpen is not set to an instance of an object.   or  Anonymous object returned by _GamesToOpen.GetIterator() is null.   or  game was set to null.   Is this strictly a design choice, meant to protect the anonymity of the code or is there a compelling reason in compiler design not to include this information in the debug-time exception?
10255
If I am passing an object to a method, why should I use the ref keyword? Isn't this the default behaviour anyway?  For example:  class Program {     static void Main(string[] args)     {         TestRef t = new TestRef();         t.Something = "Foo";          DoSomething(t);         Console.WriteLine(t.Something);     }      static public void DoSomething(TestRef t)     {         t.Something = "Bar";     } }   public class TestRef {     public string Something { get; set; } }   The output is "Bar" which means that the object was passed as a reference.
120453
I would like to print the "object reference" of an object in Java for debugging purposes. I.e. to make sure that the object is the same (or different) depending on the situation.  The problem is that the class in question inherits from another class, which has overriden both toString() and hashCode() which would usually give me the id.  Example situation: Running a multi-threaded application, where I (during development) want to check if all the threads use the same instance of a resource object or not.
86715
I'm looking at some GXT code for GWT and I ran across this use of Generics that I can't find another example of in the Java tutorials. The class name is  if you want to look at all of the code.  Here are the important parts:  private RpcMap map;  public &lt;X&gt; X get(String property) {   if (allowNestedValues &amp;&amp; NestedModelUtil.isNestedProperty(property)) {     return (X)NestedModelUtil.getNestedValue(this, property);   }   return map == null ? null : (X) map.get(property); }   X is defined nowhere else in the class or anywhere in the hierarchy, and when I hit "go to declaration" in eclipse it just goes to the &lt;X&gt; in the public method signature.  I've tried to call this method with the following two examples to see what happens:  public Date getExpiredate() {     return  get("expiredate"); }  public String getSubject() {     return  get("subject"); }   They compile and show no errors or warnings.  I would think at the very least I would have to do a cast to get this to work.  Does this mean that Generics allow a magic return value that can be anything and will just blow up at runtime?  This seems counter to what generics are supposed to do.  Can anyone explain this to me and possibly give me a link to some documentation that explains this a little better?  I've went through Sun's 23 page pdf on generics and every example of a return value is defined either at the class level or is in one of the parameters passed in.
38591
class someclass {};  class base {     int a;     int *pint;     someclass objsomeclass;     someclass* psomeclass; public:     base()     {         objsomeclass = someclass();         psomeclass = new someclass();         pint = new int();          throw "constructor failed";         a = 43;     } }  int main() {     base temp(); }   In the above code, the constructor throws. Which objects will be leaked, and how can the memory leaks be avoided?  int main() {     base *temp = new base(); }   How about in the above code? How can the memory leaks be avoided after the constructor throws?
24554
I have a lookup relationship.  If I just do the following query it works fine:  Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId]; system.debug('c:' + c);   However with the following I get an error:  Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId]; String c2 = (String) r.getSobject('field__r').get('fieldchild__c');   Error message:     FATAL_ERROR System.NullPointerException: Attempt to de-reference a   null object
100509
I want to know about the setter and getter. I know that it is a properties of declaring variable but some time I get confused. Presently I am using a sample of code that is below:  public with sharing class hello {     public String temp{set;get;}      public hello(){         temp = 'first' ;     }      public String gettemp(){         return 'second' ;     } }   So I want to know that when I use temp on VFP than what value will show on page, what is reason for that, and what is the difference between temp and gettemp?  Thanks Ashlekh Gera
355
I know references are syntactic sugar, so code is easier to read and write. But what is the difference between a pointer variable and a reference variable?
26112
Given this class  class Foo {     // Want to find _bar with reflection     [SomeAttribute]     private string _bar;      public string BigBar     {         get { return this._bar; }     } }   I want to find the private item _bar that I will mark with a attribute.  Is that possible?    I have done this with properties where I have looked for an attribute, but never a private member field.  What are the binding flags that I need to set to get the private fields?
80088
When referencing class variables, why do people prepend it with this? I'm not talking about the case when this is used to disambiguate from method parameters, but rather when it seems unnecessary.  Example:  public class Person {             private String name;      public String toString() {         return this.name;     } }   In toString, why not just reference name as name?  return name;   What does this.name buy?   a stackoverflow question whose code has this pre-pending.
68925
I'm having trouble understanding the solution presented below (It's from a textbook). I tried to get something similar, but to no avail. Help me find the way he derived those a,b,x2 and y2 expressions.   Point p is a point which we want to reflect (It's a simple structure with two integers x and y representing coordinates on a 2d plane); (x0,y0) and (x1,y1) are coordinates of endpoints of a line.  Expected result is a new point with reflected coordinates.   Point mirror(Point p, int x0, int y0, int x1, int y1) {     double dx,dy,a,b;    long x2,y2;    Point p1; //reflected point to be returned      dx  = (double) (x1 - x0);    dy  = (double (y1 - y0);     a   = (dx * dx - dy * dy) / (dx * dx + dy*dy);    b   = 2 * dx * dy / (dx*dx + dy*dy);     x2  = Math.round(a * (p.x - x0) + b*(p.y - y0) + x0);     y2  = Math.round(b * (p.x - x0) - a*(p.y - y0) + y0);     p1 = Point((int)x2,(int)y2);      return p1;  }
7396
Why the following example prints "0" and what must change for it to print "1" as I expected ?  #include &lt;iostream&gt; struct base {    virtual const int value() const {       return 0;    }    base() {       std::cout &lt;&lt; value() &lt;&lt; std::endl;    }    virtual ~base() {} };  struct derived : public base {    virtual const int value() const {       return 1;    } };  int main(void) {    derived example; }
72830
Are the string literals we use inside functions automatic variables? Or are they allocated in heap which we have to free manually?  I've a situation like the code shown below wherein I'm assigning a string literal to a private field of the class (marked as ONE in the code) and retrieving it much later in my program and using it (marked as TWO). Am I assigning a variable in the stack to a field in ONE? Can the code be referencing to a dangling pointer which in this case worked because the program was small enough?  I've compiled and ran it, it worked fine but I'm having a strange crash in my actual program where I'm assigning string literals to fields of the class like this and I suspect the case I mentioned above.  #include &lt;iostream&gt;  using namespace std;  class MemoryLeak { private:     char *s; public:     MemoryLeak() {}      void store()     {         s = "Storing a string"; // ONE     }      char *retrieve()     {         return s;     } };  int main() {     MemoryLeak *obj = new MemoryLeak();     obj-&gt;store();     cout &lt;&lt; obj-&gt;retrieve() &lt;&lt; endl; // TWO     delete obj;     return 0; }   Should I be declaring the variable "s" as a char array instead of a pointer? I'm planning to use std::string, but I'm just curious about this.  Any pointers or help is, as always, much appreciated :) Thanks.
117860
Given a struct like this:  public struct SomeStruct {     public SomeStruct(String stringProperty, Int32 intProperty)     {         this.StringProperty = stringProperty;         this.IntProperty = intProperty;     }      public String StringProperty { get; set; }     public Int32 IntProperty { get; set; } }   Of course, a compiler error is generated that reads The 'this' object cannot be used before all of its fields are assigned to.  Is there a way to assign values to the backing fields or the properties themselves, or do I have to implement properties the old-fashioned way with my own explicit backing fields?