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 6243
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.
Nepomuk 3,112
Recognized Expert Specialist
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.
Laharl 849
Recognized Expert Contributor
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 misunderstandin g your Question.
Kind regards,
Dmjpro.
JosAH 11,448
Recognized Expert MVP
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 within my control to change bar_t. Furthermore, I
need to be able to update the contained bar_t at runtime (hence the
set_bar() method seen below).
|
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 C++. I find my self sometimes, trying
Object app = Object();
Object *app = Object();
Object app = new Object();
|
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.
Source Error:
Line 553: AppDTO.zip = sZip
Line 554: AppDTO.dob = sDOB
'** line 555 is the highlighted row with the problem.
|
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 = document.getElementById('hometab');
Has anyone ever seen anything like this before, or am I dreaming?
|
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" some additional information on a
Control. I've created a structure, rtbAuxInfo, for the information. I
think that I have found the right syntax for assigning a pointer to the
structure to the Tag property of the RichTextBox, but I can't figure...
| |
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 several methods that are working fine. The
problem is if I want to dynamically assign an event handler to the object,
the event handler is not called. What am I doing wrong? Below is a sample
HTML doc with everything in place.
<!DOCTYPE HTML PUBLIC...
|
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 temporary but it was stated that
it would not. This has come up in an irc channel but I can not find the
original thread, nor can I get any code to work.
Foo& Bar( int Val )
{
return Foo( Val );
}
|
by: howa |
last post by:
Hi,
Consider a simple example, e.g.
var a = {
'a': 'b',
'c': 'd'
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |