• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusion on casting

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following code works

but the following code doesn't compile


Any ideas on why this happens?
Thanks,
Mike
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first program s1 is compile time constant and therefore s2=s1+11 doesn't give an error.

While in the second one s1 is not compile time constant and hence when u add s1+11 it will result in an int which is not implicitly casted to short.
 
Michael Rocha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alpana,

Now another related question.


Why doesn't this work?

Thanks,
Mike
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI..


From Aplana's post

While in the second one s1 is not compile time constant and hence when u add s1+11 it will result in an int which is not implicitly casted to short.




s2 = s1 + (short) 11;

Again here ...the result is not getting casted to short.

try this..

s2 = (short)(s1 + 11);

Regards

[ November 08, 2005: Message edited by: A Kumar ]
[ November 08, 2005: Message edited by: A Kumar ]
 
Michael Rocha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I'm being a bit sticky on this but I just want to clear my concepts. Please bear with me...

Doesn't the statement


imply that everything is a short?

Thanks,
Mike
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"The result of an expression involving anything int-sized or smaller is always int".

eg.
byte b=3;
byte c=4;
byte d= b+c; //Error
byte d1=byte(b+c);//compiles fine.

Same is the case with short.

now if you code like

short s1= 10 + 11;//compiles fine

as 10 & 11 are compile time constant.

but

short s1=10;
shot s2= s1 + short(11); //here s1 is not compile time constant.it's value can change at runtime.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When ever you do any aithmatic operation Result Types of Arithmetic Operations are converted to the bigger primitive.

say, x + y = r

Here operand of the arithmatic operation x & y;

Now the result will be the primitive type among x & y which ever is the largest;

In your example,



class trythis {static final short s1=10;public static void main(String args[]) {short s2;s2 = s1 + 11;System.out.println(s2);}}


The operand of arithmatic operation is short s1 & int 11;
so the result will be int type, so you need to explicitly convert int to short.
so you need to write
s2 = (short)(s1 + 11);

For details,
visit this URL

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arithmetic.html
 
Michael Rocha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alpana and A Kumar,

That really cleared things up for me.

Regards,
Mike
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,all
Look the follow code,It can complie correctly.

class trythis {
static final short s1=10;

public static void main(String args[]) {
short s2;
s2 = (int)s1 + 11;
System.out.println(s2);
}
}

what's the "static final" do?
 
A Enter
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the final short(any var) can't be casting to int.
and in this case system cast int to short.
hehe.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When ever you do any aithmatic operation Result Types of Arithmetic Operations are converted to the bigger primitive.

say, x + y = r

Here operand of the arithmatic operation x & y;

Now the result will be the primitive type among x & y which ever is the largest;



Hi Purujith,I hope u know this...But remember once more...
"The result of arith operation is not ALWAYS the largest of two operands.."



 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic