• 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

Understanding valueOf() and toString()

 
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading about about valueOf() and toString() methods for a while but the author has not explained properly.

Can you please tell me "what are they and why are they used for" ?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What class are you referring to?
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both primitive and Object. Is this okay for you to understand ? Or do i need to put some more light ?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Primitives do not have methods.

I believe you are referring to Boolean, Byte, Short, Integer, Long, Float and Double classes. Those are not primitives.

Integer.valueOf(int i) is a static method that converts an int (a primitive) to an Integer (an object).
Integer.valueOf(String s) is a static method that converts a String (text) to an Integer (an object).
Integer.toString() is an instance method that converts an Integer (an object) to a String representation of the object (text).
etc...

Read javadoc of Integer and other classes to learn more about their methods.

What book are you referring to?
What does the book say and what do you not understand about it?
Do you have some specific question?
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,

I'm following The Complete Reference Schildt 9th edition,

the book says, valueOf() is overloaded for for all the primitive types and for type Object. what does that mean ?
for primitive types returns a string that contains the human readable equivalent of the value with which it is called. (I learned from the teacher that it returns only primitive types) it's so confusing.
For, objects, valueOf() calls the toString() method on the object.

please clarify.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, you are talking about String.valueOf. You should have said that at the beginning.
You can read what it means to overload a method here.
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, i just want the exact meaning of valueOf() and toString() methods and what do they do ?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RahulRise Das wrote:No, i just want the exact meaning of valueOf() and toString() methods and what do they do ?


Here you are:
String.valueOf(boolean b)
String.valueOf(char c)
String.valueOf(char[] data)
String.valueOf(char[] data, int offset, int count)
String.valueOf(double d)
String.valueOf(float f)
String.valueOf(int i)
String.valueOf(long l)
String.valueOf(Object obj)
Object.toString()

In general String.valueOf() are static and they convert its arguments into instances of String class.
Object.toString() is an instance method (which of cource can be overriden) that returns a representation af an object as a text.

What else do you need?
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's enough for the time being, gonna try to code with them,

Thanks.
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i've understood what you've have mentioned above but i'm gettting no errors in my program but there is no output:




And my main program is:




When i enter this command in command prompt: java Test.OmitCharDemo
the following image shows it more clear.
OutputJava.PNG
[Thumbnail for OutputJava.PNG]
Output Screenshot
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything works fine. You have not executed any printing method so there is no output

If you wish to print out some output use System.out.println method.

Also, please read this PostTextNotScreenshots (← this is a link).
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh thanks so much, Pawel

Ok, no more screenshots form now on.

 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But is there a way to print the output without method ?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you are basically asking if there is a way to print output without using a method that is designed to print output.

Why you don't want to use System.out.println()?

You can use System.out.print() or System.out.printf()

Or if you really don't want to use System.out (again, why?), take a look at java.io.Console class. You can get an instance by using System.console().
Warning. This method returns null when executed using some IDEs.
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why you don't want to use System.out.println()?



I got my output using but now i'm trying to get the output using "return" statement.
 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote: . . . Warning. This method returns null when executed using some IDEs.

It also usually returns null if you execute a .jar file with the javaws/javaw tools. If you have a launcher for an executable .jar file and click that, it usually defaults to using javaw or javaws, and the console will be null.
You can show the output on a message dialogue, but it is pretty daft to try to

print output without using a method that is designed to print output

… as Paweł would say.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RahulRise Das wrote:but now i'm trying to get the output using "return" statement.


The purpose of println() method and return are completely different.
println() method is used to print something on the console.
return is used to explicitly return from a method to the caller.
Can you post your code and explain what is your requirement if I am missing something.
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tapas Chand wrote:The purpose of println() method and return are completely different.
println() method is used to print something on the console.
return is used to explicitly return from a method to the caller.
Can you post your code and explain what is your requirement if I am missing something.



You understood my problem between return statement and print method but i have some doubts, a return statement cannot give output to the console?

Can you explain this line more elaborately : return is used to explicitly return from a method to the caller.
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
You can show the output on a message dialogue, but it is pretty daft to try to



I haven't started swing yet, i'll be reaching swing soon and i think i'll try that too.
 
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
We describe things as being as different as chalk and cheese. But return and println are even more different than that. The Java® Language Specification (=JLS) defines return as one of the forms of abrupt completion of a statement. Beware: the JLS can be difficult to read.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

RahulRise Das wrote:Can you explain this line more elaborately : return is used to explicitly return from a method to the caller.


I will try to put it through a simple example.

When this will be executed, first an instance of Test class will be created. Then myMethod() method will be called.
So in this case main() method is the caller.
Now check the myMethod() statements. In this particular case any statement after line no 7 will not get executed because of the "return" statement.
So it will return to the caller (main() method in this case) at line no 14. Upto this there is no printing on the console.
Printing on console will happen when line no 15 will get executed.
So the statement in line no 7 means: explicitly returning from the method to the caller.
I hope this will clear your doubt about return and println() a little.
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you've cleared most of the cloud in my mind, and also from JLS of course by understanding the definition of the return statement which indeed returns the control to the invoker of the method for example, declare String object and call the method and evaluate the expression and the return statement returns to the object with the value from the method which has been completed abruptly.

Please correct me if I'm wrong.

But I'm not getting at this : where is the value returned to: Java Virtual Machine Stack or Heap ?
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I have found it in -> http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5

Thanks.
 
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

RahulRise Das wrote: . . . where is the value returned to: Java Virtual Machine Stack or Heap ?

Neither. It goes back to the method which called that method. Possibly as a local variable. But does it matter whether it is on the heap or the stack?
 
Ray Anderson
Ranch Hand
Posts: 98
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the local variable goes to the stack, i just want to be sure as heap only stores the object reference only.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
References to local variables are always on the stack. The object reference is probably a local variable so that would be on the stack. It would usually link to a location on the heap where the object itself is to be found.
 
reply
    Bookmark Topic Watch Topic
  • New Topic