• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Number to String ?

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

My question is related with converting a NUMBER TO STRING..

Say, i have a variable of type Double as shown:

double t=42.5;

now , i make use of a static method "toString" of class Double to convert this number to a string...

String mydouble=Double.toString(t);
System.out.println(mydouble); //this statement outputs the value as : 42.5

My question is since the output is also a number, what is the meaning of 'converting a number to string' since i do have a impression of string representation...

Where as...System.out.println(t); this also outputs the same value i.e 42.5.


At the end of the day, my question is when both println statments gives the same value i.e 42.5, what is the actual role played by the method toString() method.

Thank You





:roll: :roll:
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every class implements toString() because it is defined by Object.

toString() allows you to get something, meaningful representation of a given object.

All of the wrapper classes provide an overloaded toString() (static) method that takes a primitive value of the correct type and returns a String.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your number comes out in 1s and 0s which the processor can work out mean 42.5. It is stored in a format which represents a number. There is a method something like "toBinaryString" in the Double class which lets you see the 1s and 0s, but it omits leading 0s; remember a double is 64 bits.

Your Double.toString(d) comes out as a "4" then a "2" then a "." then a "5". It is stored in a format which represents writing. There are methods which allow you to see those 4 characters, eg System.out.println(). They don't omit leading 0s and a String is however long it happens to be.

Remember the age-old question: How long is a String?
 
sameer khazi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
It is stored in a format which represents writing. There are methods which allow you to see those 4 characters, eg System.out.println(). They don't omit leading 0s and a String is however long it happens to be.

Remember the age-old question: How long is a String?[/QB]

 
sameer khazi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sameer khazi:
[QB][/QB]



Hi Mr Cambell..

Well you have made the concept clear as how the numbers are actually represented when output.

Can you please elaborate a bit on the following..." represents a writing".
This may clear my remaining doubt.

Thank you
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anything which can be put in writing can be put into a String. This posting contains a String. You can't change to bold or italics in the middle of a String, but anything you can write on paper can be put into a String.
 
sameer khazi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mr Shekhar and Mr Campbell for your inputs.

When i find no one nearby to clear my doubts i simply log-in to JavaRanch.
It seems i have found a good tutor in "it".
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome and it is good to see we are doing a good job. Thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic