• Post Reply Bookmark Topic Watch Topic
  • New Topic

char question  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was looking over Java - Streams, Files and I/O on the tutorialspoint.com website and have a
question about one of their examples. On the code below, line 13, why do they put (char) after the equals sign? I've never seen that and
don't know what it means.

Thanks.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:
I was looking over Java - Streams, Files and I/O on the tutorialspoint.com website and have a
question about one of their examples. On the code below, line 13, why do they put (char) after the equals sign? I've never seen that and
don't know what it means.



It's a type cast. The read() method returns an int, and an int does not implicitly cast to a char, so you need to explicitly cast it.

Henry
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The read() method of an InputStreamReaderObject returns an int. Certain ints stand for chars (i.e. ASCII). But in this case, we're not really interested in the integer value. We want the character. So we cast the int as a char, meaning that the int takes on its character "equivalent."
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it say on that tutorial what a dreadful method System.in.read() is? It declares Exceptions which never occur, but still have to be caught. It reads one keystroke at a time and it doesn't terminate naturally. You would need a ctrl‑D (not Windows) or ctrl‑Z (Windows) to terminate reading.
 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c = (char) cin.read();

in the above code, the (char) is given to "cast" the variable value in to that of a character. i dont clearly have an insight to the casting operation. But however i can give you an example to explain. Database has a datatype called date. When we read values from a database, in this case, a date, we can simply "cast" it into a string value and do whatever. For more detailed information you'll have to go and look for details in the Oracle api.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sooraj Rajagopalan wrote: c = (char) cin.read();

in the above code, the (char) is given to "cast" the variable value in to that of a character. i dont clearly have an insight to the casting operation. But however i can give you an example to explain. Database has a datatype called date. When we read values from a database, in this case, a date, we can simply "cast" it into a string value and do whatever. For more detailed information you'll have to go and look for details in the Oracle api.


Can you give more clarification on what you mean by a date (from a database), and a string value? Or perhaps, you have a different definition of cast (as you quoted it)? Because if you mean what is traditionally returned from a database via JDBC, as a date (java.sql.Date) and strings in java (java.lang.String), there is no way to cast between them.

Henry
 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry, my knowledge on java is very limited, i don't really have an explanation for CASTING. Sorry if my answer is in anyway misleading.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sooraj Rajagopalan wrote:Hi Henry, my knowledge on java is very limited, i don't really have an explanation for CASTING. Sorry if my answer is in anyway misleading.

I know you are trying to be helpful, for which we are grateful, but please restrict yourself to answering questions where you fully understand the problem and are confident the answer you give is correct or you will just confuse the OP. If you think you know the answer but aren't 100% sure then say that in your answer ie "I'm not 100% sure but I think your problem can be fixed by doing XYZ"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!