public class Briefcase{

	private int amount;
	private String face;
	private boolean isOpen = false;
	
	public Briefcase(int amount,int cNumber){
		this.amount = amount;
		this.face = Integer.toString(cNumber);
	}
	
	public void removed(){
		isOpen = true;
		face = "X";
	}
	
	public boolean isRemoved(){
		return isOpen;
	}
	
	public int getAmount(){
		return amount;
	}
	public String toString(){
		return face;
	}
	
	public String getFace(){
		return face;
	}
}



import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class BriefcaseTest{

	public static void main(String[] args){
		
		Briefcase[] briefcase  = new Briefcase[26];
		
		List<Integer> amounts = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26);
		Collections.shuffle(amounts);
		
		for(int i = 0 ; i<briefcase.length;i++){
				int value = amounts.get(i);
				briefcase[i] = new Briefcase(value,i+1); 
		}
		
		for(int counter = 0 ; counter<briefcase.length; counter++){
			System.out.print("\t "+briefcase[counter]+" ");
			if(counter%5==4){
			System.out.println();
			}
		}
	}
}

How come if I remove my toString method in the briefcase class it give me this result

http://a5.sphotos.ak.fbcdn.net/hphotos-ak-ash4/s720x720/301111_2437690142235_1251190183_32921126_1850325669_n.jpg

but if I have my toString method it gives me numbers, why is that? (I am experimenting btw)

Recommended Answers

All 12 Replies

The Object class's default toString returns the name of the class, @ and the hashcode which looks like a hex address.
You should override the toString method with your own code that provides the contents of the object in an easy to read format.

For further experimenting, change what your toString method returns. For example:
return "Briefcase face=" + face;

Thats the way the java prints an object if you dont override toString() method inherited from Object.

The method prints your type followed by the Hash code for that object.

The Object class's default toString returns the name of the class, @ and the hashcode which looks like a hex address.
You should override the toString method with your own code that provides the contents of the object in an easy to read format.

For further experimenting, change what your toString method returns. For example:
return "Briefcase face=" + face;

So you are saying that java objects has their own string representation?

Look at the Object class's toString method.
Since all java objects extend the Object class, they all inherit the Object class's methods.

Look at the Object class's toString method.
Since all java objects extend the Object class, they all inherit the Object class's methods.

ALright thanks, finally saw it on the documentation

but one last question , they do have their own String representation right?

What do you mean by "String representation"?
The Class class also has String representations for classes.

What do you mean by "String representation"?
The Class class also has String representations for classes.

Disregard my question , I reread the documentation

it stated that returns an objects String representation. finally got it, thanks

Keep experimenting.

Keep experimenting.

what more should I do?

toString

public String toString()
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())
 
Returns:
a string representation of the object.

Returns String representation of the object, there,

on another note, why should Keep on experimenting? XD

My comments were for learning java in general and were in response to your comment:

(I am experimenting btw)

My comments were for learning java in general and were in response to your comment:

Oh lol, sorry I am kinda disoriented, I've been coding for more than 6 hours now

Youre doing a lot of things Chiiqui! Keep going! and take breaks in between or youre gonna break youre head! hehehe! Greetings from fellow Java Lover.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.