
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java - Character.Subset toString() Method
The Java Character.Subset toString() method retrieves the name of a given subset.
But what is the name of a subset and how is it defined? Let us learn below −
To instantiate a subset object, we will be making use of the inheritance concept in Java. The Character.Subset class is extended or inherited from any super class, say CharacterSubsetDemo. The constructor of the CharacterSubsetDemo class is invoked when the object of this class is instantiated using a String. This String is treated as the name of the subset object.
Note − This method uses the inheritance concept to create a Subset object as the constructor in the Character.Subset class has protected access. Therefore, Character.Subset class is extended from another public class whose constructor uses super() method to invoke the Object class that creates an object.
Syntax
Following is the syntax for Java Character.Subset toString() method
public final String toString()
Parameters
This method does not accept any parameters.
Return Value
This method returns a string representation of the object.
Example
The following example shows the usage of Java Character.Subset toString() method. To create an object, the constructor of the CharacterSubsetDemo class invokes the constructor of the Object class with the given String argument using the super() method. Hence, a new object is instantiated each time the constructor is called. The method Java Character.Subset.toString() is then called on these objects to obtain the string representation of them each.
package com.tutorialspoint; import java.lang.*; public class CharacterSubsetDemo extends Character.Subset { // constructor of super class CharacterSubsetDemo(String s) { super(s); } public static void main(String[] args) { CharacterSubsetDemo str1 = new CharacterSubsetDemo("admin"); CharacterSubsetDemo str2 = new CharacterSubsetDemo("webmaster"); CharacterSubsetDemo str3 = new CharacterSubsetDemo("administrator"); // returns a string representation of the object System.out.println("String1 = " + str1.toString()); System.out.println("String2 = " + str2.toString()); System.out.println("String3 = " + str3.toString()); } }
Output
Let us compile and run the above program, this will produce the following result −
String1 = admin String2 = webmaster String3 = administrator
Example
The string name can be of any character, even symbols; therefore, in this example we will see the usage of the Java Character.Subset toString() method on Subset object that is instantiated using a string containing symbol characters −
import java.lang.*; public class CharacterSubsetDemo extends Character.Subset { CharacterSubsetDemo(String s) { super(s); //invokes the immediate super class: object } public static void main(String[] args) { CharacterSubsetDemo str = new CharacterSubsetDemo("*@&^#($"); System.out.println("String name = " + str.toString()); } }
Output
Let us compile and run the program given, so the output will be achieved as follows −
String name = *@&^#($
Example
We will see another example which instantiates a Subset object using digits as string name characters and see how the toString() method works −
import java.lang.*; public class CharacterSubsetDemo extends Character.Subset { CharacterSubsetDemo(String s) { super(s); //invokes the immediate super class: object } public static void main(String[] args) { CharacterSubsetDemo str = new CharacterSubsetDemo("121393"); System.out.println("String name = " + str.toString()); } }
Output
The output is obtained after compiling and executing the program given above −
String name = 121393
Example
In this example, if the string name used to declare and initialize an object is null, the program is executed as follows after invoking the toString() method −
import java.lang.*; public class CharacterSubsetDemo extends Character.Subset { // constructor of super class CharacterSubsetDemo(String s) { super(s); //invokes the immediate super class: object } public static void main(String[] args) { CharacterSubsetDemo str = new CharacterSubsetDemo(null); //no string name given // returns a string representation of the object System.out.println("String name = " + str.toString()); } }
Exception
A NullPointerException is thrown as the string name is given null; hence, the output will be displayed as follows −
Exception in thread "main" java.lang.NullPointerException: nameat java.lang.Character$Subset.<init>(Character.java:615) at CharacterSubsetDemo.<init>(CharacterSubsetDemo.java:7) at CharacterSubsetDemo.main(CharacterSubsetDemo.java:12)