• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Address of object?

 
Ranch Hand
Posts: 103
Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the following code. Is it printing address of object or ?
Remove the comment line at toString method and check.

//: CarVector.java
import java.util.*;
class Car2 {
private int carNumber;
Car2(int i) {
carNumber = i;
}
public int carNo() { return carNumber; }
public String toString() {
return "This is Car no # " + carNumber;
}
}
class CarVector {
private Vector v = new Vector();
public void addElement(Car2 obj) {
v.addElement(obj);
}
public String elementAt(int index) {
return "Hello" + (Car2)v.elementAt(index);
}
//public String toString() {
// return "U R No " + ((Car2)v.elementAt(4)).carNo();
//f}
public int size(){ return v.size(); }
public static void main(String[] args) {
CarVector cars = new CarVector();
for(int i = 0 ; i < 5 ; i++)
cars.addElement(new Car2(i));
for(int i = 0 ; i < cars.size() ; i++)
System.out.println("Car Number # " + cars);
}
}
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not the address of any object. The method println tries to call toString() of any object, that has been passed to it.
Remember the class Object has toString().
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic