The Artima Developer Community
Sponsored Link

Java Answers Forum
Type Casting

4 replies on 1 page. Most recent reply: May 12, 2004 3:57 AM by Antony

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Kalpana

Posts: 3
Nickname: kalps
Registered: May, 2004

Type Casting Posted: May 7, 2004 1:03 AM
Reply to this message Reply
Advertisement
Hi All,
Iam reading Java2 Complete Reference.
I can't able to understand type casting
properly.can anyone help me out??


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Type Casting Posted: May 7, 2004 6:17 AM
Reply to this message Reply
Type casting is a little different depending on whether you are dealing with primitives or doubles. With primitives, it is needed when you trying to store a value in a data type that is smaller than the data type that it is currently stored in.

For example, imagine that you were creating a program to graph math functions. The actual x and y values from the functions would need to be doubles or floats, since those values aren't always going to be whole numbers. But when you draw a graph on the screen, you will need to convert those values into pixels. You can't have half a pixel, so you may need to convert data from a double to an int.

In the case of objects, typecasting tends to involve telling the compiler what an object really is. For example, Data structures like Vectors store objects as instances of the class Object. Let's say that you store a JButton object in a Vector. While it will stay a JButton, the Vector will store it as an Object, since Object is the granddaddy of all classes.

If you access that JButton later, all the compiler will know for sure is that you are accessing an Object. Since you know that it is really a JButton, you can use a typecast to let the compiler know what it really is.

I hope this helps a little.
tom

Sushil Srivastava

Posts: 6
Nickname: sushilsri
Registered: May, 2004

Re: Type Casting Posted: May 10, 2004 3:34 AM
Reply to this message Reply
Straight and simple way to remember when to Typecast is

BaseClass= DerivedClass // Derived can be assigned to base without type casting.

DerivedClass=(DerivedClass)BaseClass // This needs typecasting.

Kalpana

Posts: 3
Nickname: kalps
Registered: May, 2004

Re: Type Casting Posted: May 12, 2004 1:11 AM
Reply to this message Reply
hey thanx for ur reply.
i still a have a doubt.
for eg. if i'm retrieving a int
data from a database,how should i
cast it to String class type.i used toString(),but still
i was getting some errors.can u help me??

Antony

Posts: 2
Nickname: antonyms
Registered: May, 2004

Re: Type Casting Posted: May 12, 2004 3:57 AM
Reply to this message Reply
Hi,

Actually int is the primitive datatype and also that is not a java object. So U cant use the toString() method .
if u want to create string usin the primitive datatypes
the easiest way is just add "" to the value.
ie
int i = 0;
String s = i+"";

U cant directly assign the int value to String object but u can do this by converting the int into String.
This is not a type casting.

Flat View: This topic has 4 replies on 1 page
Topic: Need Help please. Previous Topic   Next Topic Topic: Please please help me with a assignment!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use