aspose file tools*
The moose likes Beginning Java and the fly likes TypeCasting or TypeConversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

TypeCasting or TypeConversion

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
I was reading head first java and I was wondering if I am gettin it right into my brain. Can someone please tell me if what I understood below is correct?

1).To convert a String to an Integer we use Integer.parseInt() method

2).For the rest of the datatype conversions.. like from long to int, long to short, float to int, we use (int) (short) etc.. This we can not do for a string coz we only use Integer.parseInt() for it
like say

String s = "1234";
int i= (int) s;

we cannot do this.

3).we cannot type convert a boolean value.

4). Type Conversion or Type Casting both are the same in java.

Thanks
[ December 28, 2007: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
npk chowdary
Greenhorn

Joined: Dec 09, 2007
Posts: 21
yes you are right we cannot type cast String into int but we can type cast int into String.
coming to parseInt.there are two overloaded methods taking string as parameter in one method and another one takes String and int as parameters.check api for this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
1: Yes, you can use Integer.parseInt() to convert a String to an int. There are similar methods in the Boolean Byte Double Float Long and Short classes.

2: You are correct that you cannot cast a String to an int. You can cast objects, but only to their own type.

2�: Go and look in the Java Language Specification and find sections 5.1.2 and 5.1.3. The so-called widening conversions do not need the cast to be written. Note what it says about loss of precision. The narrowing conversions have a tendency to lose precision and also to change their value. There are examples given there of what happens.

3: You are right that you can't cast to or from booleans.

4: We try to be as specific as possible about the meaning of words; casting is a kind of conversion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Type cast int into String???

The nearest you can get is that the + operator is overloaded so when you have a String, eg String numberString = "Number: ";
you can concatenate a number, eg int number = 123;
into it giving a new String (numberString + number) with the value "Number: 123".
[ December 28, 2007: Message edited by: Campbell Ritchie ]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Thanks guys for all the replies.. so I guess Type conversion is String to Integer, Integer to String and Rest all is Type Casting. Also, I noticed type casting a char gives the Ascii value of it.

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
No, I am afraid you haven't got it yet.

You can't convert an Integer to a String nor a String to an Integer by casting or conversion.

You can turn a String to an int or an Integer by using parsing methods in the Integer class (and some other classes). You can turn an Integer to a String with its toString method.

When you get the ASCII value (actually it's a Unicode value, but Unicode is built around ASCII) of a char, you are not changing the value at all. back to the Java Language Specification (see link in my earlier post) and find section 4.2, where it says quite clearly that chars are unsigned numbers. The fact that one usually prints chars as characters is neither here nor there. Try this:You should get 26. This should give 26 too:You haven't dne any casting or type conversion here at all.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Thanks Ritchie for taking the effort to reply again. I got it now. Thanks guys. This forum is really active and fast in clearing doubts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Well done getting it sorted out. We are only too pleased to help, and those of us who are more experienced still learn lots from this forum. I agree about how helpful people are here.
Andrew Han
Greenhorn

Joined: Aug 22, 2007
Posts: 4
This forum is really good!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Also please don't confuse "int" with "Integer" because the former is a built in datatype and the later is a corresponding Wrapper class.

Since Java 5.0 it may not make a difference because of Auto boxing and unboxing, the earlier versions had a significant effect atleast in the way they are used in programs


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Anshul Singhal
Greenhorn

Joined: Dec 14, 2009
Posts: 18
I wrote a program....

import java.io.*;


class anshul
{
public static void main()
{
int x;
float y =7.5f;
double z=3.0;
x=y/z;
System.out.println(x);
}

}

but compiler is giving me an error :possible loss of precision

why cant compiler do an automatic type conversion..as it is usually done..!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Please don't post a new question on an old thread, which is called hi-jacking.

Closing thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TypeCasting or TypeConversion