Home » Java programming language

Java Byte class toString() method with example

Short class toString() method: Here, we are going to learn about the toString() method of Short class with its syntax and example.
Submitted by Preeti Jain, on October 24, 2019

Syntax:

    public String toString();
    public static String toString(byte value);

Short class toString() method

  • toString() method is available in java.lang package.
  • toString() method is used to represent String denoted by this Byte object.
  • toString(byte value) method is used to represent String denoted by the given argument of byte type.
  • These methods don't throw an exception at the time of String representation.
  • toString() is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • toString(byte value) is a static method, it is accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.

Parameter(s):

  • In the first case toString(), we don't pass any parameter or value.
  • In the second case toString(byte value), we pass only one parameter of the byte type it represents the byte value to be converted.

Return value:

In the first case, the return type of this method is String - it returns the String representation of this Byte object.

In the second case, the return type of this method is String - it returns the String representation of the given argument is of byte type.

Example:

// Java program to demonstrate the example 
// of toString () method of Byte class

public class ToStringOfByteClass {
    public static void main(String[] args) {
        byte b1 = 100;
        byte b2 = 50;

        // Object initialization
        Byte ob1 = new Byte(b1);
        Byte ob2 = new Byte(b2);

        // Display ob1,ob2 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);

        // It represents the string of this Byte object
        String value1 = ob1.toString();

        // It represents the string of the given byte parameter
        String value2 = Byte.toString(ob2);

        // Display result values
        System.out.println("ob1.toString(): " + value1);
        System.out.println("Byte.toString(ob2): " + value2);
    }
}

Output

ob1: 100
ob2: 50
ob1.toString(): 100
Byte.toString(ob2): 50





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.