Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

char question

 
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.

 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • 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."
 
Marshal
Posts: 69874
278
  • 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.
 
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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Bartender
Posts: 3323
86
  • 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"
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic