Why is type casting different in Java and Python?

In summary, in Java, you can't cast a string to a number. To do so, you must parse the string for numbers and use the Integer.parseInt() function. In Python, you can cast strings by using the eval() function.
  • #1
Mr Davis 97
1,462
44
I am writing code in both Java and Python, and am a little confused about one thing: casting. In Python, we cast data types by using int(), str(), float(), etc. These are functions which convert one data type to its representation as another data type. However, in Java, it doesn't work like this. If I use (int) "25", the 25 is not converted to an integer 25. Basically, my question is, why doesn't this work in Java, and how do I cast in the same way as shown in Python?
 
Technology news on Phys.org
  • #2
You mean
Java:
long a = 25;
int b = (int)a;
does not work?
 
  • #3
DaveC426913 said:
You mean
Java:
long a = 25;
int b = (int)a;
does not work?
That works, but

Java:
String a = "5";
int b = (int) a

I'm wondering how I can convert in this way, as is done in Python.
 
  • #4
An int can't be directly converted (even in Python), it must be parsed for numbers, so:

Code:
int b = Integer.parseInt(a);
 
  • #5
Mr Davis 97 said:
I am writing code in both Java and Python, and am a little confused about one thing: casting. In Python, we cast data types by using int(), str(), float(), etc. These are functions which convert one data type to its representation as another data type. However, in Java, it doesn't work like this. If I use (int) "25", the 25 is not converted to an integer 25. Basically, my question is, why doesn't this work in Java, and how do I cast in the same way as shown in Python?
In Python, use the eval() function.
Python:
x = eval("25")
After the above runs, x will be set to 25, an integer value. I'm pretty sure you can't cast a string in Python into any of the numeric types.
 
  • #6
Mark44 said:
In Python, use the eval() function.
Python:
x = eval("25")
eval is a very dangerous function.

If a user has access to this input, they could literally wipe your drive.
Code:
eval("os.system('rm -rf /')")
Mark44 said:
I'm pretty sure you can't cast a string in Python into any of the numeric types.
http://stackoverflow.com/questions/379906/parse-string-to-float-or-int

b = float(a);
c = int(a);
 
  • #7
DaveC426913 said:
eval is a very dangerous function.
Didn't know that -- I'm pretty new at python. I think I remember a problem with eval() in JavaScript. Probably the same sort of thing.
 
  • #8
Mark44 said:
Didn't know that -- I'm pretty new at python. I think I remember a problem with eval() in JavaScript. Probably the same sort of thing.
Yup. Javascripter myself.
Didn't know Python had an eval till I looked it up, but its 4-letter horror is burned in my brain like a leaping cougar in the brain of Early Man.
 
  • #10
DaveC426913 said:
An int can't be directly converted (even in Python), it must be parsed for numbers, so:

Code:
int b = Integer.parseInt(a);

Alright, I parse the string for integers. However, now I am a little confused. What is the difference between parsing and casting in Java? And what's the difference between parsing and casting in Python?
 
  • #11
This could get a bit lengthy, you'd better google it, too.

Many years ago, I went back to one of the community colleges to learn Java; it was not for me, with Fortran and Python, I am doing just fine, thank you very much. So, basically, I don't know Java.

I personally find it unfortunate that, in Java, they chose the term "parse" for this minimal, limited type conversion where the string contains nothing else but numbers (which, I presume is a condition for Integer.parseInt() to work).

For me, parsing consists of just looking for a character or sequence of characters from among a larger sequence of characters; it does not include conversion of any kind just yet, returns text, too.

Parsing is that kind of action you expect to happen in C functions like "strchr", "strstr", and the like; and in "replace()" and the 'in' operator in Python.

More generally and usefully, parsing is done with regular expressions. I don't think this is part of ANSI C, but it is available via the POSIX regular expression library; in Python, "import re" will do. A quick google search revealed pretty good regular expression capabilities in Java: s.matches(), s.replaceAll(), Pattern.compile(), etc. That's parsing!

If short, the way I see it, when talking about type casting the concept of parsing shouldn't really come into play. When I hear 'parsing', I most often think regular expressions.

It seems part of the problem is what "type casting" means in different programming languages, as they offer different capabilities.

Some say "casting" means interpreting the same memory byte sequence with a different "mold".

More generally, "casting" means data type conversion and, so, it requires interpreting the original memory byte sequence according to its own type, converting it and storing the new value in its own manner. Even implicit type casting that everyone takes for granted when adding an integer and a float requires the integer to be up-converted and re-stored in a different manner.

So, when working in a higher language and not worrying about memory representation, when I as a human see with my own eyes a float '123.0' and need it to be an integer '123', I ask to type cast it. In Python, they have gone the extra step of saying that if you see a string "123", you can type cast it to an integer '123'...it is no different as both transactions required data type conversion.

Anyway, you can google type casting or type conversion and find quite a few discussions/explanations/examples.
 

1. What is the main difference between casting in Java and Python?

In Java, casting refers to the process of converting a variable from one data type to another. However, in Python, there is no such concept as casting because it is a dynamically typed language, meaning that the data type of a variable is determined at run time.

2. Can you provide an example of casting in Java and Python?

Sure. In Java, casting can be seen in the following code snippet:
int num = 5;
double decimal = (double) num;
Here, the variable "num" of type int is being casted to a double data type.
In Python, the equivalent code would simply be:
num = 5
decimal = float(num)
Since Python is dynamically typed, the float() function converts the variable "num" to a float data type without the need for explicit casting.

3. Is there a performance difference between casting in Java and Python?

Yes, there is a performance difference between the two. Since Java is a statically typed language, casting is done at compile time, which can improve performance. In Python, since casting is done at run time, it can slow down the execution of the program.

4. Are there any limitations to casting in Java and Python?

In Java, there are some limitations to casting, such as you cannot cast an object to a type that is not related to it. For example, you cannot cast a string to an integer.
In Python, since there is no concept of casting, there are no limitations. However, you can use functions like int(), str(), and float() to convert a variable to a different data type.

5. Which language should I choose for better casting capabilities?

It depends on the specific needs and requirements of your project. If you prefer a more strict and structured approach, Java may be a better choice. However, if you prefer a more flexible and dynamic approach, Python may be a better option.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
Replies
6
Views
643
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
7
Views
422
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
273
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
Back
Top