• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

char c = 32 //Why is it legal?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
char is "narrower" than int. Then how come the following does not give any error. (Note that no casting has been done.)
char c = 32;
Regards
Vineet
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to Programmer Certification Study forum...
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Yes. char is narrower than an int. But the lower order 16 bits in the int are stored in char.
For example, something like this will print 'A'
class Test {
static char a = 65;
public static void main(String args[]) {
System.out.println(a);
}
}
Hope this is correct. Please correct me.
Cheers ,
Kapil
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
The narrowing conversion would normally not be allowed without a explicit cast. The exception is when we assign a literal to a variable. In this case the compiler calculates the target's size and "implicitly casts" the literal i fhere is enougth room in the target.
It is exactly the same as when you do:
byte b = 40; // this is ok
byte b = 4000; // compile error : possible loss of precission
Both 40 and 4000 are of size int. The compiler checks that 40 could be represented by a byte and automatically casts it.
I know I am not being very clear, but I hope it still helps.

 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char is a 16 bit unsigned integer. There is no problem in fitting 32 into the space that a 16 bit integer can account for.
Marcus
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mafalda Alabort:
Hi all,
The narrowing conversion would normally not be allowed without a explicit cast. The exception is when we assign a literal to a variable. In this case the compiler calculates the target's size and "implicitly casts" the literal i fhere is enougth room in the target.
It is exactly the same as when you do:
byte b = 40; // this is ok
byte b = 4000; // compile error : possible loss of precission
Both 40 and 4000 are of size int. The compiler checks that 40 could be represented by a byte and automatically casts it.
I know I am not being very clear, but I hope it still helps.


Mafalda is correct but to add to his statement.....you should remember that this implicit casting is done only with literals. The following will require an explicit cast.
int b = 32;
char c = b;//will not compile without explicit (char)cast
This is because implicit narrowing conversions are only done on literals.
 
reply
    Bookmark Topic Watch Topic
  • New Topic