• Post Reply Bookmark Topic Watch Topic
  • New Topic

question on primitive data types

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Maroon {
public static void main (String[] args) {
int a = 1; // 1
short b = 1; // 2
long c = 1; // 3
a = c + a; // 4
c = b + a; // 5
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above

i felt the correct answer is b and d.
the correct answer is only d.
yy?

short b = 1-> storing an int in a variable of short type. doesnt require an explicit cast?

 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question doesn't have anything to do wit IO, but I'll field it.

This is permissible because the value is a literal. The compiler does a check that the value is within the range of the data type on the left. Note that if you did something like this:

The compiler would flag it as it has no way to insure x will be within the range of a short.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!