• 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

conversion pblm

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}}

in this example compiler error is thrown only atline 3 and 4

whynot at 7 and 8 please explain.

Thanks in advance
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because ...they (s1 and c1) are not compile time constants and so have to be explicitly casted

Here s2 and c2 are....

You can remove the error..by explicit casting..

There are certain rules for implicit casting...


[ December 07, 2005: Message edited by: A Kumar ]
 
sunitha thejaswi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you kumar for such a fast reply..
so if the the variables are compile constants you assign
a short variable which is final to a byte...doesn't this need explict casting..i am little confused in this topic please expalin..

thank you,
sunitha
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this thread...Hope it helps you..

https://coderanch.com/t/251301/java-programmer-SCJP/certification/byte
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implicit narrowing primitive conversions on assignment can occur in cases where all of the following conditions are fulfilled:

1. The source is a constant expression of either byte, short, char , or int type
2. The destination type is either byte, short, or char type.
3. the value of the source is determined to be in the range of the destination type at compile time.



class Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3 here value of source(s1) is in the range of destination(byte). but (s1) is not a constant ( not final),no implicit casting
byte b2 = c1; //4 same
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7 source s2 is a constant (final) and value of source(s2) is in the range of destination (b3-byte)
byte b4 = c2; //8 source c2 is a constant (final) and value of source (c2-char, promoted to int) is with in the range of destination (b4-byte)
}}

Hope this helps u
Regards
Naresh
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic