473,498 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add two integers

1 New Member
i need add two integers in java codeing
Jul 24 '07 #1
18 2558
r035198x
13,262 MVP
i need add two integers in java codeing
Are you having any specific problems with it? Does the user have to supply the integers or are hardcoding the integers to be added in there.
Jul 24 '07 #2
JosAH
11,448 Recognized Expert MVP
i need add two integers in java codeing
For primitive 'int's your question is hilarious; for Integers have a look at autoboxing,
i.e. starting at Java 1.5 you can simply do this:

Expand|Select|Wrap|Line Numbers
  1. Integer a= new Integer(41);
  2. Integer b= new Integer(13);
  3. Integer c;
  4.  
  5. c= a+b;
  6.  
For Java < 1.5 you have to do this:

Expand|Select|Wrap|Line Numbers
  1. Integer a= new Integer(41);
  2. Integer b= new Integer(13);
  3. Integer c;
  4.  
  5. c= new Integer(a.intValue()+b.intValue());
  6.  
kind regards,

Jos
Jul 24 '07 #3
blazedaces
284 Contributor
For primitive 'int's your question is hilarious; for Integers have a look at autoboxing,
i.e. starting at Java 1.5 you can simply do this:

Expand|Select|Wrap|Line Numbers
  1. Integer a= new Integer(41);
  2. Integer b= new Integer(13);
  3. Integer c;
  4.  
  5. c= a+b;
  6.  
For Java < 1.5 you have to do this:

Expand|Select|Wrap|Line Numbers
  1. Integer a= new Integer(41);
  2. Integer b= new Integer(13);
  3. Integer c;
  4.  
  5. c= new Integer(a.intValue()+b.intValue());
  6.  
kind regards,

Jos
I didn't know this, just tested it out, but you can even do the following:

Expand|Select|Wrap|Line Numbers
  1. public class testIntegerAdding {
  2.  
  3.     public static void main(String args[]) {
  4.         Integer a = new Integer(13);
  5.         Double b = new Double(12.5);
  6.  
  7.         Double c = a + b;
  8.         System.out.println(c);
  9.     }
  10. }
  11.  
-blazed
Jul 24 '07 #4
JosAH
11,448 Recognized Expert MVP
I didn't know this, just tested it out, but you can even do the following:

Expand|Select|Wrap|Line Numbers
  1. public class testIntegerAdding {
  2.  
  3.     public static void main(String args[]) {
  4.         Integer a = new Integer(13);
  5.         Double b = new Double(12.5);
  6.  
  7.         Double c = a + b;
  8.         System.out.println(c);
  9.     }
  10. }
  11.  
-blazed
Autoboxing is a disease; have a look at this:

Expand|Select|Wrap|Line Numbers
  1. Integer a = 42, b = 42;
  2. Integer c = 129, d = 129;
  3. System.out.println( a == 42 ); // true
  4. System.out.println( a == b ); // true
  5. System.out.println( c == 129 ); // true
  6. System.out.println( c == d ); // false
  7.  
kind regards,

Jos
Jul 24 '07 #5
r035198x
13,262 MVP
Autoboxing is a disease; have a look at this:

Expand|Select|Wrap|Line Numbers
  1. Integer a = 42, b = 42;
  2. Integer c = 129, d = 129;
  3. System.out.println( a == 42); // true
  4. System.out.println( a == b ); // true
  5. System.out.println( c == 129 ); // true
  6. System.out.println( c == d ); // false
  7.  
kind regards,

Jos
Hey, I can't test this now but does a == 10 really give true?
Jul 24 '07 #6
JosAH
11,448 Recognized Expert MVP
Hey, I can't test this now but does a == 10 really give true?
Ten? I don't see no ten in my post! :-P seriously though, that was a typo; I
remembered that silly example and typed it in from the top of my head.
10 should be 42. The results still are surprising.

kind regards,

Jos
Jul 24 '07 #7
r035198x
13,262 MVP
Ten? I don't see no ten in my post! :-P seriously though, that was a typo; I
remembered that silly example and typed it in from the top of my head.
10 should be 42. The results still are surprising.

kind regards,

Jos
Yep, when both operands are reference types no autoboxing occurs.
Jul 24 '07 #8
JosAH
11,448 Recognized Expert MVP
Yep, when both operands are reference types no autoboxing occurs.
That's not the reason: auto(un)boxing only occurs when one of the types isn't
a reference type, e.g. Integer vs int. Here's a better example:

Expand|Select|Wrap|Line Numbers
  1. public boolean check(Integer x, Integer y) {
  2.  
  3.    if (!(x < y || y < x))
  4.       return x == y; // most certainly x == y
  5.    else
  6.       return false // most certainly x != y
  7. }
  8. ...
  9. Integer x= 42, y= 42;
  10. System.out.println(check(x, y)); // true;
  11.  
  12. Integer x= 142, y= 142;
  13. System.out.println(check(x, y)); // false;
  14.  
kind regards,

Jos ;-)
Jul 24 '07 #9
r035198x
13,262 MVP
That's not the reason: auto(un)boxing only occurs when one of the types isn't
a reference type, e.g. Integer vs int. Here's a better example:

Expand|Select|Wrap|Line Numbers
  1. public boolean check(Integer x, Integer y) {
  2.  
  3.    if (!(x < y || y < x))
  4.       return x == y; // most certainly x == y
  5.    else
  6.       return false // most certainly x != y
  7. }
  8. ...
  9. Integer x= 42, y= 42;
  10. System.out.println(check(x, y)); // true;
  11.  
  12. Integer x= 142, y= 142;
  13. System.out.println(check(x, y)); // false;
  14.  
kind regards,

Jos ;-)
I still don't see how my reason is different from the one you gave.
Jul 24 '07 #10
JosAH
11,448 Recognized Expert MVP
I still don't see how my reason is different from the one you gave.
It's not about autoboxing itself: the puzzle is about why is 42 == 42 true while
142 == 142 is false. for both cases (x < y || y < x) is false (so mathematically
speaking x == y should be true.

kind regards,

Jos
Jul 24 '07 #11
r035198x
13,262 MVP
Oh yeah, I've tested it now with

Expand|Select|Wrap|Line Numbers
  1. class Test {
  2.    public static void main(String[] args) {
  3.     Integer x= 127, y= 127;
  4.     System.out.println(check(x, y));
  5.     Integer a= 128, b= 128;
  6.     System.out.println(check(a, b)); 
  7.    }
  8.    public static boolean check(Integer x, Integer y) {
  9.     if (!(x < y || y < x))
  10.        return x == y; 
  11.     else
  12.        return false ;
  13.     }
  14. }
I should have known you were up to your tricks again.
Jul 24 '07 #12
JosAH
11,448 Recognized Expert MVP
Oh yeah, I've tested it now with

Expand|Select|Wrap|Line Numbers
  1. class Test {
  2.    public static void main(String[] args) {
  3.     Integer x= 127, y= 127;
  4.     System.out.println(check(x, y));
  5.     Integer a= 128, b= 128;
  6.     System.out.println(check(a, b)); 
  7.    }
  8.    public static boolean check(Integer x, Integer y) {
  9.     if (!(x < y || y < x))
  10.        return x == y; 
  11.     else
  12.        return false ;
  13.     }
  14. }
I should have known you were up to your tricks again.
Cute eh? That's why I wrote that autoboxing is a disease. ;-)

kind regards,

Jos
Jul 24 '07 #13
r035198x
13,262 MVP
Cute eh? That's why I wrote that autoboxing is a disease. ;-)

kind regards,

Jos
It has always looked suspicious.
I'm always suspicious of everything that begins with auto. Except for automobile of course.
Jul 24 '07 #14
blazedaces
284 Contributor
It's not about autoboxing itself: the puzzle is about why is 42 == 42 true while
142 == 142 is false. for both cases (x < y || y < x) is false (so mathematically
speaking x == y should be true.

kind regards,

Jos
I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...

-blazed
Jul 24 '07 #15
JosAH
11,448 Recognized Expert MVP
I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...

-blazed
Just give the example above a try and see for yourself.

kind regards,

Jos (old bag of old tricks ;-)
Jul 24 '07 #16
blazedaces
284 Contributor
Just give the example above a try and see for yourself.

kind regards,

Jos (old bag of old tricks ;-)
Ok, so I tried the following:

Expand|Select|Wrap|Line Numbers
  1. public class testSomething {
  2.  
  3.     public static void main(String args[]) {
  4.         Integer a = 42, b = 42;
  5.         Integer c = 129, d = 129;
  6.         System.out.println( a == 42 ); // true
  7.         System.out.println( a == b ); // true
  8.         System.out.println( c == 129 ); // true
  9.         System.out.println( d == 129 ); // true
  10.         System.out.println( c == d ); // false
  11.         System.out.println( c.intValue() == d.intValue() ); // true
  12.     }
  13. }
  14.  
I can only assume from the information thus far that after one byte's value (signed) or at a value greater then or equal to 128 Integer no longer stores its numbers the same way... I do not know why or how.

I'm trying to think of some other tests to try and understand this...

Perhaps "==" isn't as simple as I thought it was?

I don't know...

-blazed
Jul 24 '07 #17
mahammadseeraz
6 New Member
I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...

-blazed
check this code

Integer a = 42, b = 42;
Integer c = 3, d = 3;
System.out.println( a == 42);
System.out.println( a == b );
System.out.println( c == 3 );
System.out.println( c == d );
Jul 30 '07 #18
JosAH
11,448 Recognized Expert MVP
check this code

Integer a = 42, b = 42;
Integer c = 3, d = 3;
System.out.println( a == 42);
System.out.println( a == b );
System.out.println( c == 3 );
System.out.println( c == d );
Yes, now make a and b equal to 142 and try again.

kind regards,

Jos
Jul 30 '07 #19

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

Similar topics

6
1814
by: Dan Stromberg | last post by:
Does anyone have a python implementation (or python C/C+ extension) that would allow me to perform set operations (union, intersection, difference, number of elements, &c) over very large...
29
7427
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
13
2518
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
4
4178
by: Neal Becker | last post by:
I can do this with a generator: def integers(): x = 1 while (True): yield x x += 1 for i in integers():
13
6102
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
16
3359
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
1
3165
by: calvin | last post by:
Can anyone write a code for this? Searching a set of Integers You are given two sets of integers. S1 and S2. The size of S1 is less than sizeof S2, i.e. the number of integers in S1 is less...
7
12708
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
7
5541
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
9
2594
by: Mark Morss | last post by:
I would like to construct a class that includes both the integers and None. I desire that if x and y are elements of this class, and both are integers, then arithmetic operations between them,...
0
7125
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,...
0
7002
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
7165
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,...
1
6887
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
7379
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
5462
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
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.