Loading...
Logo
Processing Forum
Hey guys!

I am looking at some sample code and noticing that processing takes both (double) numb and double(numb) as if they are the same but something like below has to be like the second one. I've seen this type of thing a couple of times now and don't get what the difference is so I don't know when to use it...

*WordItem is a classm words is a HashMap, word is a string
WordItem item = WordItem(words.get(word));<--NO WORK
(WordItem) words.get(word) <--WORKS

I can give more examples if you want.

Thanks!
McK




Replies(2)

You use it when it doesn't work without it...
(double) is called a cast, it forces the compiler to see the content of the variable (or expression) as the specified type. If that's not the case (or if it cannot do the conversion, with primitives), you get a runtime error.
double() is a function doing something similar with primitives (int, float...) but accepting more types (eg. it works on an array).

The former is a standard Java syntax, the latter is created by Processing because it is simpler than the concept of cast, and, somehow, more powerful.