File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Type Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type Conversion" Watch "Type Conversion" New topic
Author

Type Conversion

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

1. When do we need type conversion?
2. When does implicitly narrowing primitve occurs??
3. Does explicit narrowing exist? If so, when?


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
Im sure we have to learn it....dont know why?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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 ]

SCJP, SCWCD.
|Asking Good Questions|
 
Consider Paul's rocket mass heater.
 
subject: Type Conversion