• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Type Conversion

 
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?
 
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?
 
Marshal
Posts: 64658
225
  • 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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora 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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!