开发者

Java new Date() in print

开发者 https://www.devze.com 2023-03-31 17:27 出处:网络
just learning Java and I know this may sound stupid but I have to ask. System.out.prin开发者_高级运维t(new Date());

just learning Java and I know this may sound stupid but I have to ask.

System.out.prin开发者_高级运维t(new Date());

I know that whatever is in the argument is converted to a string, the end value that is, new Date() returns a reference to a Date object. So how is it that it prints this?

Mon Aug 29 13:22:03 BST 2011

The only thing I can think of is somehow the function parses through all the data members gets their values converts them to a String and prints them.

If not how does it work?

Thanks


The method System.out.print(obj); calls the .toString() method of the object. It's up to the class to define that method as it likes. If the object is null then "null" is printed.

If the class doesn't define its own .toString() method, the default implementation for the Object class applies, which essentially prints @ followed by the hashcode.


When you print something, the toString() method of that object is called. So it is up to the individual class to provide an implementation that makes sense.

For reference, see

  • Java Tutorial: The Object class

And this is Date's toString() method:

public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
StringBuilder sb = new StringBuilder(28);
int index = date.getDayOfWeek();
if (index == gcal.SUNDAY) {
    index = 8;
}
convertToAbbr(sb, wtb[index]).append(' ');            // EEE
convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM
CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd

CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH
CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
TimeZone zi = date.getZone();
if (zi != null) {
    sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
} else {
    sb.append("GMT");
}
sb.append(' ').append(date.getYear());  // yyyy
return sb.toString();
}


An Object has a toString(), which is a textualized representation of the Object. All java objects extends Object implicitly or explicitly. Some classes have overridden the toString() method (like java.util.Date).

So, when you do:

System.out.println(new Date()); 

It's "loosely" translated as

System.out.println(new Date().toString());

That's the default behaviour of Java.


Actually, the out.println() from System calls the PrintStream.println(Object x), which (internally, in the code), does (return (x == null)? "null" : x.toString()).

If the subclassed Object doesn't override the toString() method, the Object.toString method is called instead (since all classes implicitly or explicitly extends Object).


By default the object's toString() method is called.


Each object in Java extends from Object. Object defines a method toString, that is responsible for converting the object to a string representation.

The default implementation in Object, on most JVMs, returns the full class name followed by "@" and a hex number, representing uniquely that object instance in the JVM.

The Date object overrides the toString method, returning the string you see printed.


The print method calls the toString method on the new date object implicitly. toString is defined in Object, but is overwritten in Date as documented here.

In general: Everytime you use an object in a place where a String is expected, the toString method will be automagically called.


System.out.println accepts an object as an argument. As told before all Java classes inherit from Object.. This Object class contains a 'toString' method, and the println method simply calls the toString method from the object. Since all classes inherit from Object, every class has a toString method which can be called.

0

精彩评论

暂无评论...
验证码 换一张
取 消