• Post Reply Bookmark Topic Watch Topic
  • New Topic

operations on wrapper classes  RSS feed

 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was taking this mock test at : jchq.net
came across this question

what is the output if you compile this :


I thought output was 19 follwed by 11..

but when I checked the answer, it said it is not possible to treat wrapper classes like primitives..

someone please tell me the answer is wrong..
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is nothing wrong in the code.

the output you ve provided here is correct .

avi sinha
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avi sinha wrote:there is nothing wrong in the code.

the output you provide here is correct .

avi sinha


thank you
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jchq.net/mockexams/exam1.htm#Question%2025)

This refers to java old version (1.2, 1.3 and 1.4)
if you compile the above code with 1.2, 1.3 or 1.4, then the answer "3) Compile time error " is correct.
But if you run it using 1.5 or 1.6 then, it works as you said.

Test this:

public class WrapperDemo {

public static void main(String[] args) {
Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);
}
}

Compile using 1.5 or 1.6
-------------------------------
javac WrapperDemo.java -source 1.5
javac WrapperDemo.java -source 1.6
compiles fine:

Compile using 1.4 or 1.3 or 1.2
--------------------------------------

javac WrapperDemo.java -source 1.4
javac WrapperDemo.java -source 1.3
javac WrapperDemo.java -source 1.2

WrapperDemo.java:10: operator + cannot be applied to java.lang.Integer,java.lang.Long
System.out.println(ten + nine);
^
WrapperDemo.java:12: operator + cannot be applied to int,java.lang.Integer
System.out.println(i + ten);
^
2 errors
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridhar Gudipalli wrote:http://www.jchq.net/mockexams/exam1.htm#Question%2025)

This refers to java old version (1.2, 1.3 and 1.4)
if you compile the above code with 1.2, 1.3 or 1.4, then the answer "3) Compile time error " is correct.
But if you run it using 1.5 or 1.6 then, it works as you said.

Test this:

public class WrapperDemo {

public static void main(String[] args) {
Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);
}
}

Compile using 1.5 or 1.6
-------------------------------
javac WrapperDemo.java -source 1.5
javac WrapperDemo.java -source 1.6
compiles fine:

Compile using 1.4 or 1.3 or 1.2
--------------------------------------

javac WrapperDemo.java -source 1.4
javac WrapperDemo.java -source 1.3
javac WrapperDemo.java -source 1.2

WrapperDemo.java:10: operator + cannot be applied to java.lang.Integer,java.lang.Long
System.out.println(ten + nine);
^
WrapperDemo.java:12: operator + cannot be applied to int,java.lang.Integer
System.out.println(i + ten);
^
2 errors



you are right.. I ran it and its true

thanks for your taking trouble
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when we add two wrapper objects:

Long x = 10L;
Integer y = 10;
System.out.print(x+y);

What's going on?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!