This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What does (int) mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does (int) mean?" Watch "What does (int) mean?" New topic
Author

What does (int) mean?

J Winton
Greenhorn

Joined: Oct 03, 2010
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

Joined: Jan 10, 2002
Posts: 61306
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Welcome to Javaranch .... I prefer Head First JAVA would be a great Book to get started ..
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
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.


Everyday in every way, we get a little better.
J Winton
Greenhorn

Joined: Oct 03, 2010
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

Joined: Oct 03, 2010
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

Joined: Oct 13, 2005
Posts: 39044
    
  23
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.
J Winton
Greenhorn

Joined: Oct 03, 2010
Posts: 4
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.
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
The Thinking in Java book is really amazing one, you can have a look at it!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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

Joined: May 05, 2010
Posts: 50
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 ).


1. Make it run - 2. Make it run correctly - 3. Make it pretty OR fast/small - 4. ??? - 5. Profit
 
 
subject: What does (int) mean?