• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Data Type Conversion Just beginning

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Im just beginning Java Programming and have read up a little and been doing tutorials well i'll just cut to the chase here.

I have done a simple tutorial which involves an Applet and adds or subtracts two numbers depending on the button clicked.
There is just a small part of the tutorial which isn't explained very well and thats where the conversion from float to string takes place. I just have a question. (Ill post the entire program below everything)

My question is why is what is happening here?



I understand "msg1 = txt1.getText();" is assigning whatever I type into my first text field to my string variable "msg1" but what is actually happening on the next line? Is this line, "Float num1 = new Float(msg1);", saying take my float from num1 and put it in my string variable msg1? And once in there convert it to a String with this line "msg1 = Float.toString(num1);"?


Thanks to anybody who can understand that question! I really appreciate the help!
Below is the entire program!

 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This line uses the Float constructor to create a new Float object referenced by num1. The value of the Float is the conversion of msg1 to numeric. msg1 itself is unaffected.

See the Javadoc for java.lang.Float (⇐ click that) for more info.
 
Saloon Keeper
Posts: 13281
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Lauren!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic