java中String类得变量在用print()输出String对象储存的字符串,他是如何实现这一输出的?例如 String S1 = “Hello”;System.out.print(S1),输出 :Hello,它是如何实现得?,自定义String类这一功能如何实现?如何实现 subString(),在输出时不是自己定义对象得引用?Java String
解决方案 »
- 电子书的各种格式转换
- 高分\在线等:sql sverver2005里的image字段,select出来后怎样insert到oracle里?
- 请问JOptionPane.showMessageDialog(message)中message支持html吗?
- 请问关闭窗体的命令是什么
- synchronized(this),这个this指的是什么???
- [求助]为什么使用new CachedRowSetImpl()时会出错
- nwpulipeng(夜听春雨)请领【高分向高手求学习JAVA代码。】帖子的分
- 还是关于String 转化成java.sql.Date 的问题,急求解
- 文本聚类
- 《JAVA2核心技术 高级特性》里面的光盘JDK软件(WINDOWS)怎么配置啊?
- 大神来看下,问什么照着书上的例子写到myecliose报错??
- nego_sun_java(稽姬)请进接分
String的内部是一个字符数组,所以可以用索引值实现substring
首先是调用String.valueOf(o)转化成bytes再打印。具体看APIpublic void print(Object obj)
Prints an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
obj - The Object to be printed
See Also:
Object.toString()
你把那个 toString 方法重新写一遍
public class TestString { /**
* <pre>
* @author kanpiaoxue
* Date 2013-11-14
* 当前类没有重写Object的 public String toString()
* 这样,在打印该实例的引用的时候,会调用 Object 的 toString() 方法。
*
* 下面的这个 toString() 是JDK中Object类的方法。可以看见它的组成是由“类名 + hashCode”组成的。
* public String toString() {
* return getClass().getName() + "@" + Integer.toHexString(hashCode());
* }
* </pre>
*/
class WithoutToString {
protected String name;
protected int age; public WithoutToString(String name, int age) {
super();
this.name = name;
this.age = age;
} } /**
* <pre>
* @author kanpiaoxue
* Date 2013-11-14
* 当前类重写了 Object的 public String toString()
* 在打印该实例的引用的时候,因为这里重写了 Object的 public String toString(),所以就调用当前类的 public String toString()方法。
* </pre>
*/
class WithToString extends WithoutToString {
public WithToString(String name, int age) {
super(name, age);
} @Override
public String toString() {
return "WithToString [name=" + name + ", age=" + age + "]";
}
} /**
* @param args
*/
public static void main(String[] args) {
TestString test = new TestString();
String name = "kanpiaoxue";
int age = 200;
WithoutToString a = test.new WithoutToString(name, age);
System.out.println(a); WithToString b = test.new WithToString(name, age);
System.out.println(b);
// -------- output ------------
//com.wanmei.test.TestString$WithoutToString@2a9931f5
//WithToString [name=kanpiaoxue, age=200]
}}
这样你打印出来的就是你重写的toString()方法的返回值了