-1

I'm very new to Java and have been experimenting with some example code. Most of it I understand (from implying knowledge of other languages) but something that I've never seen before is int in the variable tempfahr. To my understanding, tempTextField.gettext is converted into a double than used in the equation. The variable tempFahr is defined as an int so what is the need/use of the int.

int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))* 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit");
AlphaMc111
  • 47
  • 1
  • 7
  • to cast a double value to an integer value. Note: it rounds the value – Paul Samsotha Dec 14 '13 at 12:27
  • its casting!! double to integer datatype.. – Java Man Dec 14 '13 at 12:27
  • What is the datatype returned by `(Double.parseDouble(tempTextField.getText()))* 1.8 + 32` ? In what datatype do you want to store it ? – Alexis C. Dec 14 '13 at 12:27
  • this is called type casting see this http://guruzon.com/1/java-basics/variable-types-and-scopes/what-is-type-casting-in-java-tutorial-example-how-to-reference-variable – SpringLearner Dec 14 '13 at 12:27
  • I think a good strategy would be to make Language Specifications top secret or fake somehow that you need to pay for it - it would soon be leaked and pirated and everybody had one. – Ingo Dec 14 '13 at 12:28
  • No one is answering OP's question. He **KNOWS** what the cast does, he doesn't know why it's needed here as the type of the result is `int`. – Maroun Dec 14 '13 at 12:28
  • @MarounMaroun Yes. To show user a rounded data rather than, a mysterious double number like , you have `125.124589 ruppers in your account` :D – Suresh Atta Dec 14 '13 at 12:35
  • possible duplicate of [Assigning result of an expression to a primitive](http://stackoverflow.com/questions/13010878/assigning-result-of-an-expression-to-a-primitive) – Raedwald Feb 28 '14 at 13:15
  • possible duplicate of [How to cast a double to an int in Java?](http://stackoverflow.com/questions/2143476/how-to-cast-a-double-to-an-int-in-java) – johnchen902 Feb 28 '14 at 13:42

5 Answers5

2

(int) is trying fo force (casting) the result of your equation to become a int value so it could be assigned to int variable.

MaGnetas
  • 4,918
  • 4
  • 32
  • 52
0

You need the (int) to make the cast explicit, from your ((Double.parseDouble(tempTextField.getText()))* 1.8 + 32), which is a double to an int, to fit in your variable, since a double has 8 bytes in java and can not be stored in an 4 byte int.

Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
  • 1
    OP **KNOWS** that. He doesn't know why you need the cast here as `tempFahr` is an `int`. This **doesn't** answer his question. – Maroun Dec 14 '13 at 12:29
0

It's to cast the resultant double to int.

Probably to show the roundup data to the user, If the result is 12.122 in textbox it shows 12

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

its casting!! double to integer datatype..

Java Man
  • 1,854
  • 3
  • 21
  • 43
0

It is used to convert type to int

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63