473,804 Members | 3,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigning value to object reference Java 1.5

36 New Member
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 6243
r035198x
13,262 MVP
Perhaps you should explain what you mean by
... In java we dont have access to any object reference. ...
Sep 24 '07 #2
getmeidea
36 New Member
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 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
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 Top Contributor
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 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.
Sep 24 '07 #6
getmeidea
36 New Member
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 Top Contributor
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.
Sep 25 '07 #8
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:

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
3653
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).
11
9281
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();
0
1014
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.
20
7017
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?
2
1377
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...
7
2533
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...
17
1689
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
4660
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 ); }
5
8089
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
9705
marktang
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...
0
10567
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, 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...
0
10323
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 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...
1
10310
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,...
0
10074
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 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...
0
9138
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, 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...
1
7613
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 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...
1
4291
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
3
2983
bsmnconsultancy
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...

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.