i need add two integers in java codeing
add two integers
Collapse
X
-
For primitive 'int's your question is hilarious; for Integers have a look at autoboxing,Originally posted by sridhar19i need add two integers in java codeing
i.e. starting at Java 1.5 you can simply do this:
[code=java]
Integer a= new Integer(41);
Integer b= new Integer(13);
Integer c;
c= a+b;
[/code]
For Java < 1.5 you have to do this:
[code=java]
Integer a= new Integer(41);
Integer b= new Integer(13);
Integer c;
c= new Integer(a.intVa lue()+b.intValu e());
[/code]
kind regards,
JosComment
-
I didn't know this, just tested it out, but you can even do the following:Originally posted by JosAHFor 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:
[code=java]
Integer a= new Integer(41);
Integer b= new Integer(13);
Integer c;
c= a+b;
[/code]
For Java < 1.5 you have to do this:
[code=java]
Integer a= new Integer(41);
Integer b= new Integer(13);
Integer c;
c= new Integer(a.intVa lue()+b.intValu e());
[/code]
kind regards,
Jos
[code=java]
public class testIntegerAddi ng {
public static void main(String args[]) {
Integer a = new Integer(13);
Double b = new Double(12.5);
Double c = a + b;
System.out.prin tln(c);
}
}
[/code]
-blazedComment
-
Autoboxing is a disease; have a look at this:Originally posted by blazedacesI didn't know this, just tested it out, but you can even do the following:
[code=java]
public class testIntegerAddi ng {
public static void main(String args[]) {
Integer a = new Integer(13);
Double b = new Double(12.5);
Double c = a + b;
System.out.prin tln(c);
}
}
[/code]
-blazed
[code=java]
Integer a = 42, b = 42;
Integer c = 129, d = 129;
System.out.prin tln( a == 42 ); // true
System.out.prin tln( a == b ); // true
System.out.prin tln( c == 129 ); // true
System.out.prin tln( c == d ); // false
[/code]
kind regards,
JosComment
-
Hey, I can't test this now but does a == 10 really give true?Originally posted by JosAHAutoboxing is a disease; have a look at this:
[code=java]
Integer a = 42, b = 42;
Integer c = 129, d = 129;
System.out.prin tln( a == 42); // true
System.out.prin tln( a == b ); // true
System.out.prin tln( c == 129 ); // true
System.out.prin tln( c == d ); // false
[/code]
kind regards,
JosComment
-
Ten? I don't see no ten in my post! :-P seriously though, that was a typo; IOriginally posted by r035198xHey, I can't test this now but does a == 10 really give true?
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,
JosComment
-
Yep, when both operands are reference types no autoboxing occurs.Originally posted by JosAHTen? 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,
JosComment
-
That's not the reason: auto(un)boxing only occurs when one of the types isn'tOriginally posted by r035198xYep, when both operands are reference types no autoboxing occurs.
a reference type, e.g. Integer vs int. Here's a better example:
[code=java]
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.prin tln(check(x, y)); // true;
Integer x= 142, y= 142;
System.out.prin tln(check(x, y)); // false;
[/code]
kind regards,
Jos ;-)Comment
-
I still don't see how my reason is different from the one you gave.Originally posted by JosAHThat'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:
[code=java]
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.prin tln(check(x, y)); // true;
Integer x= 142, y= 142;
System.out.prin tln(check(x, y)); // false;
[/code]
kind regards,
Jos ;-)Comment
-
It's not about autoboxing itself: the puzzle is about why is 42 == 42 true whileOriginally posted by r035198xI still don't see how my reason is different from the one you gave.
142 == 142 is false. for both cases (x < y || y < x) is false (so mathematically
speaking x == y should be true.
kind regards,
JosComment
-
Oh yeah, I've tested it now with
[CODE=java]class Test {
public static void main(String[] args) {
Integer x= 127, y= 127;
System.out.prin tln(check(x, y));
Integer a= 128, b= 128;
System.out.prin tln(check(a, b));
}
public static boolean check(Integer x, Integer y) {
if (!(x < y || y < x))
return x == y;
else
return false ;
}
}[/CODE]
I should have known you were up to your tricks again.Comment
-
Cute eh? That's why I wrote that autoboxing is a disease. ;-)Originally posted by r035198xOh yeah, I've tested it now with
[CODE=java]class Test {
public static void main(String[] args) {
Integer x= 127, y= 127;
System.out.prin tln(check(x, y));
Integer a= 128, b= 128;
System.out.prin tln(check(a, b));
}
public static boolean check(Integer x, Integer y) {
if (!(x < y || y < x))
return x == y;
else
return false ;
}
}[/CODE]
I should have known you were up to your tricks again.
kind regards,
JosComment
-
I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...Originally posted by JosAHIt'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
-blazedComment
Comment