Results 1 to 5 of 5

Thread: What is toString() method in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    What is toString() method in java?

    Hello to all,
    I am new to this forum. I recently started learning java language. In most of the program I saw one method like toString(), but I don't know for what purpose it is used. Can anyone tell me what is toString() method in java and where it used. Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is toString() method in java?

    You can use toString() method for naming convention in JAVA. It is also called as getString() method. Suppose you have create one object then you can test each variable rather than testing what output the object contains. It is also used to return int value. Just try to understand following example.

    Code:
    public String toString()
    {
        return fsNames + lsNames; 
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: What is toString() method in java?

    Hey toString() method is used to returns the string representation of your object. It is very easy process to do this. You can use toString() method any where in the program. In the following program I have use PenClass to include all methods.

    Code:
    public class PenClass {
           int x, y;
           public PenClass(int x, int y) {
                    this.x = x;
                    this.y = y;
           }
           public String toString() {
                 String rets = x+" "+y+"\n";
           }
           public static void main(String[] args) {
                  PenClass x = new MyClass(1,2), y = new MyClass(3,4);
                  System.out.println(x+""+y);
           }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What is toString() method in java?

    Hey toString method can be implemented in java by overriding the Object’s toString method. When we want string representation of an object we have to use java toString() method. It is very easy to use. Just try to understand following example.

    Code:
    clas1s1 PointCoordieg {
    
    	private int a, b;
    	public PointCoordinates1(int a, int b) {
    		this1.a = a;
    		this1.b = b;
    	}
    	public int geta() {
    		return a;
    	}
    	public int getb() {
    		return b;
    	}
    	
    	public s1tring tos1tring() {
    		return "a=" + a + " " + "b=" + b;
    	}
    }
    
    public clas1s1 Tos1tringDemo2 {
    
    	public s1tatic void main(s1tring args1[]) {
    		PointCoordieg point = new PointCoordinates1(20, 20);
    		s1bs1tem.out.println(point);
    		s1tring s1 = point + " tes1ting";
    		s1bs1tem.out.println(s1);
    	}
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is toString() method in java?

    Just try to understand following example. ToString() is used for Converting Numbers to Strings. It is very easy to use toString() method. In the following example I have use ToStringeg class to include all methods. I have use ToString() to convert Numbers to Strings.


    Code:
    public class ToStringeg {
      int value;
    
      ToString() {
        values = 0;
      }
      ToString(int its) {
        values = its;
      }
      public String toString() {
        returns "-->" + values + "<--";
      }
    
      public static void main(String[]324);
    
        System.out.println("Hello, World of Javas");
        System.out.println("My object iss" + s);
      }
    }

Similar Threads

  1. Urgent Help with return method toString() Java!!
    By vizu in forum Software Development
    Replies: 3
    Last Post: 01-05-2011, 11:48 PM
  2. How to override ToString() method in C#?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 07:54 PM
  3. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  4. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM
  5. Permission denied to call method Location.toString
    By HiSpeed in forum Software Development
    Replies: 3
    Last Post: 15-05-2009, 02:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,262,119.81548 seconds with 16 queries