Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# What does (int) mean?

J Winton
Greenhorn
Posts: 4
I was very happy to find these forums and see that not only was it newbie friendly but had 170k+ posts in the beginner section. I'm glad to have this resource in my pocket.
That being said I just, last week, picked up a copy of a Sams Teach Yourself JAVA in 21 Days at a used book store in an effort to get a head start since i'm going to school soon for Comp Sci.

I'm taking my time as I go through each chapter to make sure I absorb it all and there is a Certification Practice question at the end of the 4th chapter that has me a bit confused. Any help deciphering it would be fantastic.

Here is the block of code:

I understand everything except the "(int)" in line 5.

int z = (x\y) would set int z = 1.8 (which would need to be a float?)

but the "int z = (int) (x/y)" leaves me at a loss.

I'm sure this is a very easy question for you all and hopefully I haven't made it harder to understand than I need to.

Thanks!

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65111
89
This is known as a "cast". For primitives, it can convert between one type and the other. In this case, the result of x/y is a float, and in order to put the result into an int, it must be cast or the compiler will complain.

Note, this causes truncation because ints can contain only integer values.

Arun Giridharan
Ranch Hand
Posts: 290
Welcome to Javaranch .... I prefer Head First JAVA would be a great Book to get started ..

Bobby Smallman
Ranch Hand
Posts: 107
Welcome to Java Ranch! I'm sure you will find your time here well spent.

An easy next step to think about with this piece of code since Bear taught you about the cast. Would you need the cast if instead of saying (x / y) you said (9 / 5) and why or why not? Just food for thought!

Finally, when naming a topic be sure to give some indication of what the problem is in the name, can be hard to do sometimes when you are very new to Java but it helps not only the people on the forums but it forces you to narrow down exactly what your problem is.

J Winton
Greenhorn
Posts: 4
I suppose my next question would be why wasn't the code in the question written float z = (x\y) ?

Is it because the switch wont work with a float and only an int?

J Winton
Greenhorn
Posts: 4
Bobby Smallman wrote:
An easy next step to think about with this piece of code since Bear taught you about the cast. Would you need the cast if instead of saying (x / y) you said (9 / 5) and why or why not? Just food for thought!

I would say "yes" I still need the cast because the division returns a decimal in either case.

Bobby Smallman wrote:Finally, when naming a topic be sure to give some indication of what the problem is in the name, can be hard to do sometimes when you are very new to Java but it helps not only the people on the forums but it forces you to narrow down exactly what your problem is.

I am very curious what you would have labelled this question. I thought about it beforehand but other than "int z = (int) (x \ y), whats the (int) for?" I was at a loss.

Campbell Ritchie
Sheriff
Posts: 49733
69
Arun Giridharan wrote:Welcome to Javaranch ....
Agree
I prefer Head First JAVA . . .
Agree SAMS (is that Cadenhead and Lemay?) is one of those books which put all the code into the main method, so you never have a change to see mutliple method in use. In fact I think putting all the code into the main() method doesn't constitute object-oriented programming (OOP). You will see OOP in other books (eg Deitel and Deitel, Horstmann and Cornell, Sierra and Bates [Head First]). My favourite of that selection would be Horstmann and Cornell, but I don't think it is intended for beginners.

J Winton
Greenhorn
Posts: 4
Campbell Ritchie wrote:

J Winton wrote:
I am very curious what you would have labelled this question. I thought about it beforehand but other than "int z = (int) (x \ y), whats the (int) for?" I was at a loss.

Mina Daoud
Ranch Hand
Posts: 88
The Thinking in Java book is really amazing one, you can have a look at it!

Campbell Ritchie
Sheriff
Posts: 49733
69
J Winton wrote: . . . "int z = (int) (x \ y), whats the (int) for?"
That would have been a much better title. How about
What does (int) mean?

Stephan Mueller
Ranch Hand
Posts: 50
J Winton wrote:Is it because the switch wont work with a float and only an int?