Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Conversion

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. When do we need type conversion?
2. When does implicitly narrowing primitve occurs??
3. Does explicit narrowing exist? If so, when?
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im sure we have to learn it....dont know why?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Kedar Nath

The official details are in the Java Language Specification, but it is not easy reading. If you do arithmetic with two different kinds of numbers you need to know what sort the result is, otherwise you might get erroneous results or a compiler error.

If you are passing objects around you might need to cast them to different types (but class casting is a hazardous and error-prone activity). Unfortunately I failed to find much about primitive casting in the Java Tutorials but you could try a search; you might have more luck than I did. There were 32 references to "cast" for objects, however.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Campbell's post should answer your queries.
Just to brief up,
When do we need type conversion?

Mostly when the type is not consistent with the operator like byte with addition.
2. When does implicitly narrowing primitve occurs??

It will never occur because the compiler wont let you lose information
3. Does explicit narrowing exist? If so, when?

Yes.Like when you typecast float into int.
[ September 02, 2008: Message edited by: Amit Ghorpade ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic