Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-19-2008, 09:30 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
[SOLVED] Java operator overloading
Hello everyone.

I have an interesting question to ask. I have seen on a few forums that operator overloading is not possible in Java. So then, why can we use the following?
Code:
Integer a = new Integer(1); Integer b = new Integer(2); Integer c = a + b;
I've tested this and it seems to work. Could this be due to Java's auto-boxing? In this case a, b and c are all instances of Integer. And the + means the addition of two Integer objects, which then returns an Integer object. Could it be that the objects, a and b, are converted to their primitive type equivalents, added and then converted back to an Integer object?

Just a strange thing I picked up. Thank you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-19-2008, 09:42 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I believe but could be wrong, but operator overloading refers like in C++ where you change what an operator does. In C++ I could make the + really mean multiply or concatenate two strings. Java has no such function other that what is already defined in java.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-19-2008, 10:00 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Hello Zosden

Can you explain why my code snippet compiles and runs successfully?

Thank you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-19-2008, 11:52 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Because I do believe correct me if I'm wrong but Integer can be made into int form in java. They are almost the same to a programmer in many aspects.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-26-2008, 07:39 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Based on the JavaDoc's first line:
Quote:
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
I would then deduce that a resolves to 1, b resolves to 2 and c resolves to the value of 1 + 2, or 3.

Code:
Integer a = new Integer(1); Integer b = new Integer(2); Integer c = a + b; System.out.println(c);
Output:
3

__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-26-2008, 09:35 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Thank you CaptainMorgan and Zosden
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with operator in case jimJohnson New To Java 2 03-21-2008 10:22 PM
Overloading? padutch2 New To Java 2 12-31-2007 05:26 AM
Method/Operator Overloading Java Tip Java Tips 0 12-01-2007 10:33 PM
Overloading methods dynamically Syed.muddasir New To Java 6 07-31-2007 04:34 PM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 05:19 PM


All times are GMT +3. The time now is 02:25 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org