Help coderanch get a
new server
by contributing to the fundraiser
  • 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

assigning an int to a char

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings all!
Just landed here. Like to know why the following code snippets behave differently.
{
final int i = 127;
char ic = i;
}

{
int j =127;
char jc = j; //this line complains and don't know why
}
thanks.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because int is not final.
At compile time integer should be final then only you can assign without casting.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abu,
In the 1st block int varaible i is declared final.It means i is constant n its value can't be changed.so compilor don't raise any problen when i is assigned to char type varaible.
But in the 2nd block, int variable is not declared final.so we can change it's value and the new value can be out of range of char type.So compilor give error here.
regds
Arpana
 
Abu Yoosuf
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why final behaves differently? int (32 bits) is assigned to a char (16 bits) - a narrow conversion - shouldn't it require a cast to char before the assignment on both snippets.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since i is final the compiler knows its value, and can determine whether or not the value will fit.
If you change it to
final int i = 70000;
the compiler will complain, because 70000 is
out of range for a character.
 
Abu Yoosuf
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi and Arpana,
Thanks for the replies.
Arpana,
I tried to use your explanation to find out whether it works for objects.
class A {}
class B extends A {}
class C
{
public static void main (String [] args)
{
//according to you, at this point the compiler knows a is pointing to a B instance
final A a = new B();
//so I expect the following assignment to work without a cast

B b = a; //but compiler complains ???
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure this only works for primitives, not objects. Also, it only works for compile-time constants. This, for instance, won't work:
final int i = Math.round(2.4f);
char c = i;
[ November 14, 2002: Message edited by: Ron Newman ]
 
Abu Yoosuf
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ron.
 
Maybe he went home and went to bed. And took this tiny ad with him:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic