How to use the toString method in Java? - java

Can anybody explain to me the concept of the toString() method, defined in the Object class? How is it used, and what is its purpose?

From the Object.toString docs:
Returns a string representation of the
object. In general, the toString
method returns a string that
"textually represents" this object.
The result should be a concise but
informative representation that is
easy for a person to read. It is
recommended that all subclasses
override this method.
The toString method for class Object
returns a string consisting of the
name of the class of which the object
is an instance, the at-sign character
`#', and the unsigned hexadecimal
representation of the hash code of the
object. In other words, this method
returns a string equal to the value
of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
Example:
String[] mystr ={"a","b","c"};
System.out.println("mystr.toString: " + mystr.toString());
output:- mystr.toString: [Ljava.lang.String;#13aaa14a

Use of the String.toString:
Whenever you require to explore the constructor called value in the String form, you can simply use String.toString...
for an example...
package pack1;
import java.util.*;
class Bank {
String n;
String add;
int an;
int bal;
int dep;
public Bank(String n, String add, int an, int bal) {
this.add = add;
this.bal = bal;
this.an = an;
this.n = n;
}
public String toString() {
return "Name of the customer.:" + this.n + ",, "
+ "Address of the customer.:" + this.add + ",, " + "A/c no..:"
+ this.an + ",, " + "Balance in A/c..:" + this.bal;
}
}
public class Demo2 {
public static void main(String[] args) {
List<Bank> l = new LinkedList<Bank>();
Bank b1 = new Bank("naseem1", "Darbhanga,bihar", 123, 1000);
Bank b2 = new Bank("naseem2", "patna,bihar", 124, 1500);
Bank b3 = new Bank("naseem3", "madhubani,bihar", 125, 1600);
Bank b4 = new Bank("naseem4", "samastipur,bihar", 126, 1700);
Bank b5 = new Bank("naseem5", "muzafferpur,bihar", 127, 1800);
l.add(b1);
l.add(b2);
l.add(b3);
l.add(b4);
l.add(b5);
Iterator<Bank> i = l.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
... copy this program into your Eclipse, and run it... you will get the ideas about String.toString...

The toString() method returns a textual representation of an object. A basic implementation is already included in java.lang.Object and so because all objects inherit from java.lang.Object it is guaranteed that every object in Java has this method.
Overriding the method is always a good idea, especially when it comes to debugging, because debuggers often show objects by the result of the toString() method. So use a meaningful implementation but use it for technical purposes. The application logic should use getters:
public class Contact {
private String firstName;
private String lastName;
public Contact (String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {return firstName;}
public String getLastName() {return lastName;}
public String getContact() {
return firstName + " " + lastName;
}
#Override
public String toString() {
return "["+getContact()+"]";
}
}

It may optionally have uses within the context of an application but far more often it is used for debugging purposes. For example, when you hit a breakpoint in an IDE, it's far easier to read a meaningful toString() of objects than it is to inspect their members.
There is no set requirement for what a toString() method should do. By convention, most often it will tell you the name of the class and the value of pertinent data members. More often than not, toString() methods are auto-generated in IDEs.
Relying on particular output from a toString() method or parsing it within a program is a bad idea. Whatever you do, don't go down that route.

toString() returns a string/textual representation of the object.
Commonly used for diagnostic purposes like debugging, logging etc., the toString() method is used to read meaningful details about the object.
It is automatically invoked when the object is passed to println, print, printf, String.format(), assert or the string concatenation operator.
The default implementation of toString() in class Object returns a string consisting of the class name of this object followed by # sign and the unsigned hexadecimal representation of the hash code of this object using the following logic,
getClass().getName() + "#" + Integer.toHexString(hashCode())
For example, the following
public final class Coordinates {
private final double x;
private final double y;
public Coordinates(double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Coordinates coordinates = new Coordinates(1, 2);
System.out.println("Bourne's current location - " + coordinates);
}
}
prints
Bourne's current location - Coordinates#addbf1 //concise, but not really useful to the reader
Now, overriding toString() in the Coordinates class as below,
#Override
public String toString() {
return "(" + x + ", " + y + ")";
}
results in
Bourne's current location - (1.0, 2.0) //concise and informative
The usefulness of overriding toString() becomes even more when the method is invoked on collections containing references to these objects. For example, the following
public static void main(String[] args) {
Coordinates bourneLocation = new Coordinates(90, 0);
Coordinates bondLocation = new Coordinates(45, 90);
Map<String, Coordinates> locations = new HashMap<String, Coordinates>();
locations.put("Jason Bourne", bourneLocation);
locations.put("James Bond", bondLocation);
System.out.println(locations);
}
prints
{James Bond=(45.0, 90.0), Jason Bourne=(90.0, 0.0)}
instead of this,
{James Bond=Coordinates#addbf1, Jason Bourne=Coordinates#42e816}
Few implementation pointers,
You should almost always override the toString() method. One of the cases where the override wouldn't be required is utility classes that group static utility methods, in the manner of java.util.Math. The case of override being not required is pretty intuitive; almost always you would know.
The string returned should be concise and informative, ideally self-explanatory.
At least, the fields used to establish equivalence between two different objects i.e. the fields used in the equals() method implementation should be spit out by the toString() method.
Provide accessors/getters for all of the instance fields that are contained in the string returned. For example, in the Coordinates class,
public double getX() {
return x;
}
public double getY() {
return y;
}
A comprehensive coverage of the toString() method is in Item 10 of the book, Effective Java™, Second Edition, By Josh Bloch.

Whenever you access an Object (not being a String) in a String context then the toString() is called under the covers by the compiler.
This is why
Map map = new HashMap();
System.out.println("map=" + map);
works, and by overriding the standard toString() from Object in your own classes, you can make your objects useful in String contexts too.
(and consider it a black box! Never, ever use the contents for anything else than presenting to a human)

Correctly overridden toString method can help in logging and debugging of Java.

Coding:
public class Test {
public static void main(String args[]) {
ArrayList<Student> a = new ArrayList<Student>();
a.add(new Student("Steve", 12, "Daniel"));
a.add(new Student("Sachin", 10, "Tendulkar"));
System.out.println(a);
display(a);
}
static void display(ArrayList<Student> stu) {
stu.add(new Student("Yuvi", 12, "Bhajji"));
System.out.println(stu);
}
}
Student.java:
public class Student {
public String name;
public int id;
public String email;
Student() {
}
Student(String name, int id, String email) {
this.name = name;
this.id = id;
this.email = email;
}
public String toString(){ //using these toString to avoid the output like this [com.steve.test.Student#6e1408, com.steve.test.Student#e53108]
return name+" "+id+" "+email;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email=email;
}
}
Output:
[Steve 12 Daniel, Sachin 10 Tendulkar]
[Steve 12 Daniel, Sachin 10 Tendulkar, Yuvi 12 Bhajji]
If you are not used toString() in Pojo(Student.java) class,you will get an output like [com.steve.test.Student#6e1408, com.steve.test.Student#e53108].To avoid these kind of issue we are using toString() method.

Apart from what cletus answered with regards to debugging, it is used whenever you output an object, like when you use
System.out.println(myObject);
or
System.out.println("text " + myObject);

The main purpose of toString is to generate a String representation of an object, means the return value is always a String. In most cases this simply is the object's class and package name, but on some cases like StringBuilder you will got actually a String-text.

/**
* This toString-Method works for every Class, where you want to display all the fields and its values
*/
public String toString() {
StringBuffer sb = new StringBuffer();
Field[] fields = getClass().getDeclaredFields(); //Get all fields incl. private ones
for (Field field : fields){
try {
field.setAccessible(true);
String key=field.getName();
String value;
try{
value = (String) field.get(this);
} catch (ClassCastException e){
value="";
}
sb.append(key).append(": ").append(value).append("\n");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return sb.toString();
}

If you learn Python first and then Java. I think it plays the same role as __str__() method in Python, it is a magic method like __dict__() and __init__() but to refer to a string representing the the object.

the toString() converts the specified object to a string value.

Related

Java printing Class objects in ArrayList

Sailor class
public class Sailor {
private String name;
private String email;
public Sailor(String name, String email) {
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Crew class
import java.util.ArrayList;
public class Crew {
private ArrayList<Sailor> sailorList = new ArrayList<>();
public Crew() {
}
public void addCrewMember(Sailor sailor) {
sailorList.add(sailor);
}
public String toString() {
String str = "";
for(int i = 0; i < sailorList.size(); i++) {
str = sailorList.get(i).getName() + sailorList.get(i).getEmail();
}
return str;
}
}
Main program
public class ObjectsSailorProgram {
public static void main(String[] args) {
Sailor firstSailor = new Sailor("Frank", "frank#mail.com");
Sailor secondSailor = new Sailor("Susan", "susan#mail.com");
Sailor thirdSailor = new Sailor("John", "john#sailors.com");
Sailor fourthSailor = new Sailor("Ann", "ann#sailors.com");
Crew firstCrew = new Crew();
Crew secondCrew = new Crew();
firstCrew.addCrewMember(firstSailor);
firstCrew.addCrewMember(secondSailor);
firstCrew.addCrewMember(fourthSailor);
secondCrew.addCrewMember(thirdSailor);
secondCrew.addCrewMember(secondSailor);
System.out.println("=== First crew ===\n" + firstCrew);
System.out.println("=== Second crew ===\n" + secondCrew);
secondSailor.setEmail("Susan#sailors.com");
System.out.println("=== Second crew ===\n" + secondCrew);
}
}
I am having trouble printing the crews and I'm not sure if the addCrewMember is correct.
I've tried reading other similar posts but i haven't been able to use the solutions here. So i need help with the addCrewMember and toString methods
You aren't setting the variables neither in constructor nor by calling setters in your main.
You can change the constructor as below:
public Sailor(String name, String email) {
this.name = name;
this.email = email;
}
In your toString() method, you're overwriting the str. Use StringBuilder to append strings into the resulting string. You can modify your toString() method as follows:
public String toString() {
StringBuilder str = new StringBuilder();
for(int i = 0; i < sailorList.size(); i++) {
str.append(i+1)
.append(". ")
.append(sailorList.get(i).getName())
.append(" ")
.append(sailorList.get(i).getEmail())
.append("\n");
}
return str.toString();
}
And your program will give the output:
=== First crew ===
1. Frank frank#mail.com
2. Susan susan#mail.com
3. Ann ann#sailors.com
=== Second crew ===
1. John john#sailors.com
2. Susan susan#mail.com
=== Second crew ===
1. John john#sailors.com
2. Susan Susan#sailors.com
0> sailor's constructor is wrong-> you're not initializing the data
public Sailor(String name, String email) {
setEmail(email); //or this.email=email;
this.name=name; //you may make a setter also for this
}
1> private ArrayList<Sailor> sailorList = new ArrayList<Sailor>();
2> the to-string is also wrong: it prints just the last sailor, use += instead of =
str +=" "+ sailorList.get(i).getName() + sailorList.get(i).getEmail();
This is just personal taste: I prefer "for-each" syntax.
for(Sailor s: sailorList){
out += s.getName()+" "+s.getEmail();
}
As others have noted, you are not assigning the values in your Sailor constructor so do that. You should also add any missing get/set methods.
public Sailor(String name, String email) {
this.name = name;
this.email = email;
}
The addCrewMember(Sailor) method in Crew looks correct but your toString() method is flawed.
You are trying to build a String but you are re-assigning it on each iteration rather then concatenating elements on each iteration.
Except in very simple cases, you should avoid String concatenation when compose Strings.
The code is pushing all of the elements together with no separation which means you will have an unreadable blob of characters (e.g. bobsmithbsmith#boat.comsamsmithssmith#boat.com)
I would suggest that you write a toString() in Sailor class. This example separates the elements of Sailor by commas and wraps the entire entity in curly braces (e.g. "{Sam Smith, ssmith#boat.com}"). Note the use of String.format(..) rather than concatenation.
public class Sailor {
... stuff ...
#Override public String toString() {
return String.format("{%s, %s}", name, email);
}
}
The Crew.toString() method can employ this to compose its String. The following makes use of the StringJoiner class to compose a comma-separated list of elements wrapped in square braces to represent the crew members (e.g. "[{Al Almond, aalmond#boat.com},{Bob Bobson, bbobson#boat.com}]");
public class Crew {
... stuff ...
#Override public String toString() {
StringJoiner sj = new StringJoiner(",", "[", "]");
for (Sailor s : sailorList) {
sj.add(s.toString()); // Sailor.toString() is explicitly invoked here but you could remove that call and it should still be invoked implicitly
}
return sj.toString();
}
}

Q: Player(class) java

Have to code a player class in java eclipse following these requirements
a) The Player class should have a default constructor and two custom constructors - one
that accepts a Name object, and another that accepts both a Name and PairOfDice object.
b) There should be get and set methods for its Name and a get method for PairOfDice. It
should have a method called rollDice and getDiceScore that both simply delegate to the
PairOfDice class, which already has this functionality. You should also have an
appropriate toString() method.
c) Add a further void method setFullPlayerName(String) that accepts a single String
argument (e.g. “Joe Bloggs”) and then uses this to set the first and family name
individually by extracting the relevant information and then calling the respective setter
methods of the Name class.
So far I have this
public class Player {
//Fields of the app
private String firstName;
private String lastName;
private Die red;
private Die blue;
//PlayerName and dice pair Default Constructor
public Player() {
firstName = "";
lastName = "";
red = new Die();
blue = new Die();
}
public Player(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Player(String firstName, String lastName,Die red,Die blue) {
this.firstName = firstName;
this.lastName = lastName;
this.red = red;
this.blue = blue;
}
// Methods
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setlastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getlastName() {
return lastName;
}
#Override
public String toString() {
return "PairOfDice:[red=" + red + ", blue=" + blue + "]";
}
public void rollDice() {
red.roll();
blue.roll();
}
public int getDiceScore() {
return red.getScore() + blue.getScore();
}
public Die getRed() {
return red;
}
public Die getBlue() {
return blue;
}
public String setFullName() {
if (firstName.equals("") && lastName.equals("")) {
return "";
} else {
return firstName + " " + lastName;
}
}
}
Is my code correct? if not what changes do i have to make to correct it
There are several problems:
Dice not Die: private Die red;
It's better to name your variable using a noun: private Dice redDice;
Should have a space after a comma: , Dice redDice, Dice blueDice
setFullName should accept 1 string and call setFirstName and setLastName to set the name
Example of implementation:
void setFullName(String fullName) {
/// split full name into lastName and firstName
setFirstName(firstName);
setLastName(lastName);
}
Everything seems fine, but I think you forgot to define the roll() function. Besides that, everything else seems fine :)
I see a couple of issues with this code:
a) The Player class should have a default constructor and two custom constructors - one that accepts a Name object, and another that accepts both a Name and PairOfDice object.
b) There should be get and set methods for its Name and a get method for PairOfDice. It should have a method called rollDice and getDiceScore that both simply delegate to the PairOfDice class, which already has this functionality. You should also have an appropriate toString() method.
If it's telling you that you need a Name object and a PairOfDice object, you can't just put in two Strings and two Dies and call that the same thing. You need to actually use the Name and PairOfDice classes.
c) Add a further void method setFullPlayerName(String) that accepts a single String argument (e.g. “Joe Bloggs”) and then uses this to set the first and family name individually by extracting the relevant information and then calling the respective setter methods of the Name class
Instead of setFullPlayerName, you made a method called setFullName that does not do what it's supposed to do. It's supposed to accept a String and return void, and instead it accepts no parameters and returns a String. It looks like a getter instead of a setter.
Let's get this straight: This portion of the code:
public String setFullName() {
if (firstName.equals("") && lastName.equals("")) {
return "";
} else {
return firstName + " " + lastName;
}
}
it's misleading due to the fact that the method name it's saying that it's setting the name, BUT you're returning the name instead
So change it to this:
public void setFullPlayerName(String name) {
String[] splitted = name.split(" ");
firstname = splitted[0];
lastname = splitted[1]
}
And now it's unnecessary to add the method setFullPlayerName
BUT I see that you may need a method to return a full name, so:
public String getFullPlayerName() {
return firstname + " " + lastname;
}
Hopefully that'll resolve your issue :D

How to access class field variable inside all methods from toString twin toStrung?

Once upon modifying the above with static modifiers, line 16 requires the following syntax:
getLegs();toStrung();
//I think this is essentially printing the last called method updating class field variable toString. For example, to do setLegs();toStrung(); prints setLegs()'s toString.
Question: How should one access a shared field within methods? What if I included it into the constructor? Ideally, I want the code to look like getLegs().toStrung() and for toString to be a clean slate for every method.
My answer: I think a seperate instance of String toString inside each method works to get a clean slate appeal, but the syntax doesn't make sense. I know it is about my design. I think a solution would be a new class, but this returns to the same conflict that relates to the class field variable.
public class Dog{
public String toString;
public Dog(String name){
this.name = name;
}
public int getLegs(){
toString = "Dog has " + legs + " legs.";
return legs;
}
public int setLegs(int legs){
toString = getName() + "'s legs have changed from "
+ getLegs() + " to " + legs + ".";
this.legs = legs;
return this.legs;
}
public void toStrung(){
System.out.println(Dog.toString);
}
public static void main(String[] args){
Dog Dundt = new Dog("Dundt");
Dundt.getLegs();
Dundt.toStrung();
}
1) toString() should not be a static member.
2) getLegs() should not have the side effect of changing the member String toString.
3) There should not be a member variable String toString.
4) toString() should return a String.
5) name needs to be a member.
6) legs needs to be a member.
7) toString() should generate the string from the members at run time.
8) You do not need to explicitly call toString() in main. Simply passing the instance of Dog to println will call it for you.
9) It is good practice to annotate methods you are overriding with the #Override annotation. toString() is a member of Object and you are overriding Object.
public class Dog{
private String name;
private int legs = 4;
public Dog(String name){
this.name = name;
}
public int getLegs(){
return legs;
}
public int setLegs(int legs){
this.legs = legs;
return this.legs;
}
#Override
public String toString(){
return "Dog is called " + name + " it has " + legs + " legs.";
}
public static void main(String[] args){
Dog dundt = new Dog("Dundt");
System.out.println(dundt);
}
}

how can I return a String?

package book1;
import java.util.ArrayList;
public abstract class Book {
public String Book (String name, String ref_num, int owned_copies, int loaned_copies ){
return;
}
}
class Fiction extends Book{
public Fiction(String name, String ref_num, int owned_copies, String author) {
}
}
at the moment when i input values into the variable arguments and call them with this :
public static class BookTest {
public static void main(String[] args) {
ArrayList<Book> library = new ArrayList<Book>();
library.add(new Fiction("The Saga of An Aga","F001",3,"A.Stove"));
library.add(new Fiction("Dangerous Cliffs","F002",4,"Eileen Dover"));
for (Book b: library) System.out.println(b);
System.out.println();
}
}
i get a return value of this:
book1.Fiction#15db9742
book1.Fiction#6d06d69c
book1.NonFiction#7852e922
book1.ReferenceBook#4e25154f
how can i convert the classes to return a string value instead of the object value? I need to do this without changing BookTest class. I know i need to use to string to convert the values. but i don't know how to catch the return value with it. could someone please point me in the right direction on how to convert this output into a string value?
You need to overwrite the toString() Method of your Book class. In this class you can generate a String however you like. Example:
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.author).append(": ").append(this.title);
return sb.toString();
}
You need to override the toString() method in your Book or Fiction class. The method is actually declared in the Object class, which all classes inherit from.
#Override
public String toString(){
return ""; // Replace this String with the variables or String literals that you want to return and print.
}
This method is called by System.out.println() and System.out.print() when they receive an object in the parameter (as opposed to a primitive, such as int and float).
To reference the variables in the method, you'll need to declare them in the class and store them via the class's constructor.
For example:
public abstract class Book {
private String name;
private String reference;
private int ownedCopies;
private int loanedCopies;
public Book (String name, String reference, int ownedCopies, int loanedCopies) {
this.name = name;
this.reference = reference;
this.ownedCopies = ownedCopies;
this.loanedCopies = loanedCopies;
}
#Override
public String toString(){
return name + ", Ref:" + reference + ", OwnedCopies: " + ownedCopies + ", LoanedCopies: " + loanedCopies; // Replace this String with the variables or String literals that you want to return and print.
}
}
The classes you have defined, don't store any values. It is in other words useful to construct a new book. You need to provide fields:
public abstract class Book {
private String name;
private String ref_num;
private int owned_copies;
private int loaned_copies;
public String Book (String name, String ref_num, int owned_copies, int loaned_copies) {
this.name = name;
this.ref_num = ref_num;
this.owned_copies = owned_copies;
this.loaned_copies = loaned_copies;
}
public String getName () {
return name;
}
//other getters
}
Now an object is basically a set of fields. If you want to print something, you can access and print one of these fields, for instance:
for (Book b: library) System.out.println(b.getName());
In Java, you can also provide a default way to print an object by overriding the toString method:
#Override
public String toString () {
return ref_num+" "+name;
}
in the Book class.
Need to give your object Book a ToString() override.
http://www.javapractices.com/topic/TopicAction.do?Id=55
Example:
#Override public String toString()
{
return name;
}
Where name, is a string in the Class.
I am hoping that you have assigned the passed arguments to certain attributes of the classes. Now, once you are done with that, you can override the toString() method in Book to return your customized string for printing.

Java code output is weird? [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed last month.
I do not understand why my output is not what I expected, instead of showing the persons information, the output displays: examples.Examples#15db9742
Am I doing something wrong in my code?
package examples;
public class Examples {
String name;
int age;
char gender;
public Examples(String name, int age, char gender){
this.name = name;
this.age = age;
this.gender = gender;
}
public static void main(String[] args) {
Examples[] person = new Examples[10];
person[0] = new Examples("Doe",25,'m');
System.out.println(person[0]);
}
}
Add a toString() method to your class:
public class Examples {
String name;
int age;
char gender;
public Examples(String name, int age, char gender){
this.name = name;
this.age = age;
this.gender = gender;
}
#Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(this.name + " ");
result.append(this.age + " ");
result.append(this.gender + " ");
return result.toString();
}
public static void main(String[] args) {
Examples[] person = new Examples[10];
person[0] = new Examples("Doe",25,'m');
System.out.println(person[0]);
}
}
When you say
System.out.println(person[0]);
java doesn't automatically know what you want printed out. To tell it, you write a method in your Examples class called toString() which will return a string containing the info you want. Something like:
#Override
public String toString() {
return "Name: " + name +
" Age: " + String.valueOf(this.age) +
" Gender: " + String.valueOf(this.gender);
}
Java has no way of knowing what you want it to print. By default, the toString() method is called when you use System.out.println() with an object.
Your Examples class should have its own toString() method so you can decide what to print. The default toString() returns a representation of the object in memory.
For example, to print out the object's name:
package examples;
public class Examples {
...
#Override
public String toString() {
return name;
}
}
Your output is right, when you print an object the method toString() of the object is called; by default it returns what you see (the class and a memory direction).
Override the method toString() of the class to make him return a descriptive String. E.g.:
public class Examples {
// The same ...
public String toString(){
return "My name is " + name + " and I have " + age + " years."
}
// The same ...
}
If you do that you will get a more descriptive String when calling toString() and so when printing an object of class Examples.
New output is
My name is Dow and I have 25 years.
person is an array of type Examples, so by acessing person[0] you are telling it to print an Examples instance. Since the Examples class does not implement an toString() method it will call the parent Object.toString() method that produces the output you are seeing.
Add the following method to your Examples class
public String toString() {
return "[name="+this.name+", age="+this.age+", gender="+this.gender+"]";
}
You have explicitly to create a method which outputs the persons data or override the toString() method to do the same thing:
public class Person
{
String name;
int age;
char gender;
public Person(String name, int age, char gender)
{
this.name = name;
this.age = age;
this.gender = gender;
}
//Override the toString() method
//is a usual programming technique
//to output the contents of an object
public String toString()
{
return "Name: " + this.name + "\nAge: " + this.age + "\nGender: "
+ this.gender;
}
//You can also write something like this
public void showInfo()
{
System.out.printf("Persons Info:\n\nName: %s\nAge: %s\nGender: %s", this.name, this.age, this.gender);
}
public static void main(String[] args)
{
Person p = new Person("bad_alloc", 97, 'm');
//System.out.println("Persons info:\n" + p.toString());
//If you want directly to "output the object" you have to override the toString() method anyway:
//System.out.println(p);//"Outputting the object", this is possible because I have overridden the toString() method
p.showInfo();
}
}

Categories

Resources