Assigning value to object reference Java 1.5

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • getmeidea
    New Member
    • Feb 2007
    • 36

    Assigning value to object reference Java 1.5

    Hi,

    I am using JDK 1.5.
    I have a program like this. Here i am directly assigning value to one object.
    It does'nt give me any compile time or run time error. In java we dont have access to any object reference. Still this kind of assignment is allowed. What is the reason behind this.
    Code:
    Class Sample{
    	public static void main(){
    		//Assigning value to object reference itself.
    		Integer i = 10;
    		System.out.println(i.intValue());
    	}
    }
    But i think it initialise an objects and assigns the given value to object.
    How it is implemented ?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Perhaps you should explain what you mean by
    Originally posted by getmeidea
    ... In java we dont have access to any object reference. ...

    Comment

    • getmeidea
      New Member
      • Feb 2007
      • 36

      #3
      Originally posted by r035198x
      Perhaps you should explain what you mean by
      when i say, Integer i = 10;

      here the reference value of i is not set by the value 10. Still it is allowed to write without giving any compile error.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by r035198x
        Perhaps you should explain what you mean by...
        I think what the OP means is, that 10 is an int, not an Integer and therefore it should be [CODE=java]Integer i = new Integer(10);[/CODE]Wasn't that something about automatic matching, introduced in JRE 1.5? (I tried it with compilance level 1.3 - it won't work. No problem with compilance level 6.0 however.)

        Greetings,
        Nepomuk

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by getmeidea
          Hi,

          I am using JDK 1.5.
          I have a program like this. Here i am directly assigning value to one object.
          It does'nt give me any compile time or run time error. In java we dont have access to any object reference. Still this kind of assignment is allowed. What is the reason behind this.
          Code:
          Class Sample{
          	public static void main(){
          		//Assigning value to object reference itself.
          		Integer i = 10;
          		System.out.println(i.intValue());
          	}
          }
          But i think it initialise an objects and assigns the given value to object.
          How it is implemented ?
          You can't assign an object reference like this........ :-(
          Here what is done ... it is Auto Boxing, introduced in Java 1.5.

          Kind regards,
          Dmjpro.

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Originally posted by dmjpro
            You can't assign an object reference like this........ :-(
            Here what is done ... it is Auto Boxing, introduced in Java 1.5.

            Kind regards,
            Dmjpro.
            Basically, in Java 1.5, they added direct comparison between Integer and int (called Auto Boxing), so you don't have to use intValue() and lots of casting anymore, unless you really want to.

            Comment

            • getmeidea
              New Member
              • Feb 2007
              • 36

              #7
              Originally posted by Laharl
              Basically, in Java 1.5, they added direct comparison between Integer and int (called Auto Boxing), so you don't have to use intValue() and lots of casting anymore, unless you really want to.
              By refereng "Autoboxing " i came to know how this statement works.
              Thanks to all who made me reply and telling the term "Autoboxing ".

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by getmeidea
                By refereng "Autoboxing " i came to know how this statement works.
                Thanks to all who made me reply and telling the term "Autoboxing ".
                Sorry for misunderstandin g your Question.

                Kind regards,
                Dmjpro.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  I don't like autoboxing very much; below you'll see an example derived from an
                  original example by someone else posted in Sun's Java forum quite a while ago:

                  [code=java]
                  import java.util.*;

                  public class Autoboxing {

                  public static void main(String[] args) {

                  Object foo = new Long(0xcafebabe deadbeefL);
                  List<Object> bars = new ArrayList<Objec t>();

                  bars.add(bool() ? (Long)foo : (Number)foo);
                  bars.add(bool() ? (Long)foo : (Long)foo);
                  bars.add(bool() ? (Long)foo : (Double)foo);
                  bars.add(bool() ? (Long)foo : ((Long)foo).lon gValue());

                  System.out.prin t("== :");
                  for (Object bar : bars)
                  System.out.prin t(" "+(foo == bar));
                  System.out.prin tln();

                  System.out.prin t("equals:");
                  for (Object bar : bars)
                  System.out.prin t(" "+foo.equals(ba r));
                  System.out.prin tln();
                  }

                  private static boolean bool() {
                  return true;
                  }
                  }[/code]

                  Output:

                  Code:
                  ==    : true true false false
                  equals: true true false true
                  That's why I don't like autoboxing very much ...

                  kind regards,

                  Jos

                  Comment

                  Working...