BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Object to String Conversion

By Chaitanya Singh | Filed Under: java

In this guide, we will learn how to convert Object to String in Java. We will also see program for StringBuffer and StringBuilder object to String conversion.

Java Program to convert an object of a class to String

Here, we are using toString() and valueOf() methods to convert the object of Student class to a String.

class Student {
  // A Student class
}
class JavaExample {
  public static void main(String[] args)
  {
    // Object of Student class
    Student obj = new Student();

    // converting the object of Student class to String
    // using toString() method
    String str1 = obj.toString();

    // converting object of Student class to String
    // using valueOf() method
    String str2 = String.valueOf(obj);
    
    System.out.println("Conversion using toString() Method: "+str1);
    System.out.println("Conversion using valueOf() Method: "+str2);
  }
}

Output:

Java convert object to string example output

Java Program to convert object of StringBuilder/StringBuffer to String

StringBuilder and StringBuffer are used to represent mutable Strings, while the String is an immutable sequence of characters. In this example, we will see how to convert StringBuilder/StringBuffer object to String.

Methods used in this program:

  • StringBuilder.toString()
  • StringBuffer.toString()
class JavaExample {
  public static void main(String[] args)
  {
    //sb1 is an object of StringBuilder class
    StringBuilder sb1 = new StringBuilder("BeginnersBook");

    //sb2 is an object of StringBuffer class
    StringBuilder sb2 = new StringBuilder("Best Tutorial Website");

    //Conversion using toString() method
    String str1 = sb1.toString();
    String str2 = sb2.toString();

    // Prints Strings after conversion
    System.out.println("StringBuilder to String: " + str1);
    System.out.println("StringBuffer to String: " + str2);
  }
}

Output:

StringBuilder/StringBuffer object to String Output

Recommended Posts

  • Java String to Object Conversion
  • Java boolean to Boolean object Conversion
  • Java String Object to Boolean Object Conversion

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap