java - method that returns char with int -


can explain method me , why returning char instead of int??

public static int fgetc(inputstream stream) {     char toret = '\0';     if (instream.isempty())     {          try          {               toret = (char) stream.read();               if (toret == cr)                     toret = (char) stream.read();               if ((int) toret == 0xffff)                     return eof;          }          catch (eofexception eof)          {                return eof;          }          catch (ioexception ioe)          {                 writeline ("unexpected io exception caught!\n", system.out);                 writeline (ioe.tostring(),system.out);          }      }      else            toret = ((mylibcharacter) instream.pop ()).charvalue(); return toret; } 

and lets want store value user input character until newline detected,

int index = 0; while (message[index] != '\n\) {     message[index] = fgetc(system.in);     index++; } 

why wrong?? tips , appreciated.

sorry might little messy, feel free edit or ask me questions regarding post.

char (and other primitive types) in java int's.

as in jls says

a narrowing primitive conversion may used if type of variable byte, short, or char, , value of constant expression representable in type of variable.

so in my previous answer your previous question, must cast or change return type, because:

char c = 'a' + 1; char c = 45; 

or

char c = 'a'; c = c++; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -