473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. Class Sample{
  2.     public static void main(){
  3.         //Assigning value to object reference itself.
  4.         Integer i = 10;
  5.         System.out.println(i.intValue());
  6.     }
  7. }
But i think it initialise an objects and assigns the given value to object.
How it is implemented ?
Sep 24 '07 #1
8 6217
r035198x
13,262 8TB
Perhaps you should explain what you mean by
... In java we dont have access to any object reference. ...
Sep 24 '07 #2
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.
Sep 24 '07 #3
Nepomuk
3,112 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. Integer i = new Integer(10);
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
Sep 24 '07 #4
dmjpro
2,476 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. Class Sample{
  2.     public static void main(){
  3.         //Assigning value to object reference itself.
  4.         Integer i = 10;
  5.         System.out.println(i.intValue());
  6.     }
  7. }
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.
Sep 24 '07 #5
Laharl
849 Expert 512MB
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.
Sep 24 '07 #6
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".
Sep 25 '07 #7
dmjpro
2,476 2GB
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 misunderstanding your Question.

Kind regards,
Dmjpro.
Sep 25 '07 #8
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Autoboxing {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Object foo = new Long(0xcafebabedeadbeefL);
  8.         List<Object> bars = new ArrayList<Object>();
  9.  
  10.         bars.add(bool() ? (Long)foo : (Number)foo);
  11.         bars.add(bool() ? (Long)foo : (Long)foo);
  12.         bars.add(bool() ? (Long)foo : (Double)foo);
  13.         bars.add(bool() ? (Long)foo : ((Long)foo).longValue());
  14.  
  15.         System.out.print("==    :");
  16.         for (Object bar : bars)
  17.             System.out.print(" "+(foo == bar));
  18.         System.out.println();
  19.  
  20.         System.out.print("equals:");
  21.         for (Object bar : bars)
  22.             System.out.print(" "+foo.equals(bar));
  23.         System.out.println();
  24.     }
  25.  
  26.     private static boolean bool() {
  27.         return true;
  28.     }
  29. }
Output:

Expand|Select|Wrap|Line Numbers
  1. ==    : true true false false
  2. equals: true true false true
  3.  
That's why I don't like autoboxing very much ...

kind regards,

Jos
Sep 25 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. ...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: eBob.com | last post by:
I know that this must be a really dumb question but I just can't find an answer. I want to associate some information with a RichTextBox. The Tag property seems to be the intended way to "hang"...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
17
by: Summercool | last post by:
I wonder which language allows you to change an argument's value? like: foo(&a) { a = 3 } n = 1 print n
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
5
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.