File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does (int) mean?

 
J Winton
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Debian Fedora Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch .... I prefer Head First JAVA would be a great Book to get started ..
 
Bobby Smallman
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Thinking in Java book is really amazing one, you can have a look at it!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic