i need add two integers in java codeing
18 2558
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.
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: -
Integer a= new Integer(41);
-
Integer b= new Integer(13);
-
Integer c;
-
-
c= a+b;
-
For Java < 1.5 you have to do this: -
Integer a= new Integer(41);
-
Integer b= new Integer(13);
-
Integer c;
-
-
c= new Integer(a.intValue()+b.intValue());
-
kind regards,
Jos
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: -
Integer a= new Integer(41);
-
Integer b= new Integer(13);
-
Integer c;
-
-
c= a+b;
-
For Java < 1.5 you have to do this: -
Integer a= new Integer(41);
-
Integer b= new Integer(13);
-
Integer c;
-
-
c= new Integer(a.intValue()+b.intValue());
-
kind regards,
Jos
I didn't know this, just tested it out, but you can even do the following: -
public class testIntegerAdding {
-
-
public static void main(String args[]) {
-
Integer a = new Integer(13);
-
Double b = new Double(12.5);
-
-
Double c = a + b;
-
System.out.println(c);
-
}
-
}
-
-blazed
JosAH 11,448
Recognized Expert MVP
I didn't know this, just tested it out, but you can even do the following: -
public class testIntegerAdding {
-
-
public static void main(String args[]) {
-
Integer a = new Integer(13);
-
Double b = new Double(12.5);
-
-
Double c = a + b;
-
System.out.println(c);
-
}
-
}
-
-blazed
Autoboxing is a disease; have a look at this: -
Integer a = 42, b = 42;
-
Integer c = 129, d = 129;
-
System.out.println( a == 42 ); // true
-
System.out.println( a == b ); // true
-
System.out.println( c == 129 ); // true
-
System.out.println( c == d ); // false
-
kind regards,
Jos
Autoboxing is a disease; have a look at this: -
Integer a = 42, b = 42;
-
Integer c = 129, d = 129;
-
System.out.println( a == 42); // true
-
System.out.println( a == b ); // true
-
System.out.println( c == 129 ); // true
-
System.out.println( c == d ); // false
-
kind regards,
Jos
Hey, I can't test this now but does a == 10 really give true?
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
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.
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: -
public boolean check(Integer x, Integer y) {
-
-
if (!(x < y || y < x))
-
return x == y; // most certainly x == y
-
else
-
return false // most certainly x != y
-
}
-
...
-
Integer x= 42, y= 42;
-
System.out.println(check(x, y)); // true;
-
-
Integer x= 142, y= 142;
-
System.out.println(check(x, y)); // false;
-
kind regards,
Jos ;-)
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: -
public boolean check(Integer x, Integer y) {
-
-
if (!(x < y || y < x))
-
return x == y; // most certainly x == y
-
else
-
return false // most certainly x != y
-
}
-
...
-
Integer x= 42, y= 42;
-
System.out.println(check(x, y)); // true;
-
-
Integer x= 142, y= 142;
-
System.out.println(check(x, y)); // false;
-
kind regards,
Jos ;-)
I still don't see how my reason is different from the one you gave.
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
Oh yeah, I've tested it now with - class Test {
-
public static void main(String[] args) {
-
Integer x= 127, y= 127;
-
System.out.println(check(x, y));
-
Integer a= 128, b= 128;
-
System.out.println(check(a, b));
-
}
-
public static boolean check(Integer x, Integer y) {
-
if (!(x < y || y < x))
-
return x == y;
-
else
-
return false ;
-
}
-
}
I should have known you were up to your tricks again.
JosAH 11,448
Recognized Expert MVP
Oh yeah, I've tested it now with - class Test {
-
public static void main(String[] args) {
-
Integer x= 127, y= 127;
-
System.out.println(check(x, y));
-
Integer a= 128, b= 128;
-
System.out.println(check(a, b));
-
}
-
public static boolean check(Integer x, Integer y) {
-
if (!(x < y || y < x))
-
return x == y;
-
else
-
return false ;
-
}
-
}
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
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.
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
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 ;-)
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: -
public class testSomething {
-
-
public static void main(String args[]) {
-
Integer a = 42, b = 42;
-
Integer c = 129, d = 129;
-
System.out.println( a == 42 ); // true
-
System.out.println( a == b ); // true
-
System.out.println( c == 129 ); // true
-
System.out.println( d == 129 ); // true
-
System.out.println( c == d ); // false
-
System.out.println( c.intValue() == d.intValue() ); // true
-
}
-
}
-
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
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 );
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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():
|
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...
| |
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...
|
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...
|
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 !=...
|
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
|
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,...
|
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,...
| |
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |