Casting and Converting Objects and Primitive Types

One thing you discover quickly about Java is how finicky it is about the information it will handle. Like Morris, the perpetually dissatisfied cat on the 9-Lives cat food commercials, Java expects things to be a certain way and won't put up with alternatives.

When you are sending arguments to methods or using variables in expressions, you must use variables of the right data types. If a method requires an int, the Java compiler responds with an error if you try to send a float value to the method. Likewise, if you're setting up one variable with the value of another, they must be of the same type.


There is one area where Java's compiler is decidedly un-Morrislike: Strings. String handling ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.