Forums Register Login

What does (int) mean?

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Welcome to Javaranch .... I prefer Head First JAVA would be a great Book to get started ..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.

By the way: your thread title ought to tell us more of what the thread is about, please.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:
By the way: your thread title ought to tell us more of what the thread is about, please.



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.

 
+Pie Number of slices to send: Send
The Thinking in Java book is really amazing one, you can have a look at it!
+Pie Number of slices to send: Send
 

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?

 
+Pie Number of slices to send: Send
 

J Winton wrote:Is it because the switch wont work with a float and only an int?



Almost correct. http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer (discussed in Simple Data Objects ).

 
I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 23069 times.
Similar Threads
From Java in 21 days (newest edition covering Java 8)
numeric promotion understanding for oca exam
Parentheses in Expression
is my understanding of the remainder operator correct?
Overload
MixFor5 second case confusion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:51:52.