• 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

Reg. Assigning values

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When we assign a value to say a byte variable as below :
byte b = 3;
Here we see that the right side literal value should
be in the range of the byte i.e. ( -128 to 127 ) , so the
assignment is valid and code compiles and runs fine.
My doubt :
1. For the character variable assigining a int value should be in the
range of (0-65535).
Does it work the same way as mentioned above?
2. Is it that beyond the range of the variable to which we are assigning
it is considered as an int.?
( I am referring specifically to assigning to byte,short,char variables )
 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
you can create char = 655; it is compile and run time success
thanks
daniel
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Angela Narain:

When we assign a value to say a byte variable as below :
byte b = 3;
Here we see that the right side literal value should
be in the range of the byte i.e. ( -128 to 127 ) , so the
assignment is valid and code compiles and runs fine.
My doubt :
1. For the character variable assigining a int value should be in the
range of (0-65535).
Does it work the same way as mentioned above?
2. Is it that beyond the range of the variable to which we are assigning
it is considered as an int.?
( I am referring specifically to assigning to byte,short,char variables )


Hi Angela,
Small example here.

HIH,
Guru's correct me!
chandrashekar
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Integral literals are treated as ints. When they appear in an assignment, for example, in <code>byte b = 3</code> the '3' is an int but Java applies assignment conversion and because '3' is within the range of a byte, the assignment succeeds.
The same is true for <code>char c = 655</code>.
See JLS §5.2
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It will not recognize values which are not in this range.


Hi Jane Griscti,
As we can see from the example above, I am sure that the code will compile and run when we use
char c = 655. [Narrowing conversion involving int literals] But when we are trying to print out the value, why is it not printing any value. It is printing a '?' . what does that mean. Can you please clarify on that.
thanks
Chandrashekar!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,
I got the info after reading it from Khalid's book. (pg
"Not all Unicode Characters can be represented in other encoding schemes. In that case, the ? character is usually used to denote any such character in the resulting output, during translation from Unicode.
thanks
Chandra!
------------------
Where there is a Will, there is a Way.
 
You don't know me, but I've been looking all over the world for. Thanks to the help from 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