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. -
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 ?
8 6217
Perhaps you should explain what you mean by
... In java we dont have access to any object reference. ...
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.
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 - 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
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. -
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.
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.
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".
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.
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: -
import java.util.*;
-
-
public class Autoboxing {
-
-
public static void main(String[] args) {
-
-
Object foo = new Long(0xcafebabedeadbeefL);
-
List<Object> bars = new ArrayList<Object>();
-
-
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).longValue());
-
-
System.out.print("== :");
-
for (Object bar : bars)
-
System.out.print(" "+(foo == bar));
-
System.out.println();
-
-
System.out.print("equals:");
-
for (Object bar : bars)
-
System.out.print(" "+foo.equals(bar));
-
System.out.println();
-
}
-
-
private static boolean bool() {
-
return true;
-
}
-
}
Output: -
== : true true false false
-
equals: true true false true
-
That's why I don't like autoboxing very much ...
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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.
...
|
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 =...
|
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"...
|
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...
|
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
|
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...
|
by: howa |
last post by:
Hi,
Consider a simple example, e.g.
var a = {
'a': 'b',
'c': 'd'
}
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |