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.
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: 32654
4
posted
0
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: 32654
4
posted
0
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: 622
posted
0
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: 32654
4
posted
0
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: 622
posted
0
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: 32654
4
posted
0
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.
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