社区
Java SE
帖子详情
char[] ch=null;ch的输出
shiliufu
2007-01-28 12:34:43
public static void main(String[] args) {
char[] ch=null;
System.out.println(ch);
System.out .println("ch: "+ch);
}
为什么System.out .println("ch: "+ch);会输出ch: null
而System.out.println(ch);会报出空指针异常,
其它类型的数组都不会发生这种情况,只有char[]会这样。
...全文
536
6
打赏
收藏
char[] ch=null;ch的输出
public static void main(String[] args) { char[] ch=null; System.out.println(ch); System.out .println("ch: "+ch); } 为什么System.out .println("ch: "+ch);会输出ch: null 而System.out.println(ch);会报出空指针异常, 其它类型的数组都不会发生这种情况,只有char[]会这样。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
fengmingjie
2007-01-30
打赏
举报
回复
通俗的理解就是因为加了“ch”,system.out.println认为输出的是string啊
believefym
2007-01-30
打赏
举报
回复
最好直接分析源代码,println(char[])最终调用下面的代码,有个for循环,它把buf看作不为null来处理
而""+buf是自动调用Object.toString方法,如果是null的,就返回"null"
所以一个有异常,一个没异常
private void write(char buf[]) {
try {
synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush) {
for (int i = 0; i < buf.length; i++)
if (buf[i] == '\n')
out.flush();
}
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
iamsangster
2007-01-30
打赏
举报
回复
java .io.Writer
public void write(char cbuf[]) throws IOException {
write(cbuf, 0, cbuf.length);
}
cbuf.length 能不异常吗?
china8848
2007-01-28
打赏
举报
回复
我认为:char[]这个数组指向null,就是个空指针,所以System.out.println(ch);很正常,而"ch: "+ch 转化为了String 就向 ""+ch ,ch 转化为String后就是null,所以不异常
grapepaul
2007-01-28
打赏
举报
回复
学习了
nyj_vbnet
2007-01-28
打赏
举报
回复
好奇怪的问题,顶
c++
ch
ar
* 地址
输出
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "p
ch
.h" #include <iostream> #include<Windows.h> int main() {
ch
ar
ch
1 = 'a';
ch
ar
ch
2 = 'a'; const
ch
ar
ch
3 = 'a'; const
ch
ar
ch
4 = 'a'; int a;
ch
ar
* p =
NULL
; in.
c语言中字符串常用函数
编程中,常用到字符串的各个函数,总结如下: 1、字符串的初始化 1)
ch
ar
ch
[5]={0}; //字符串,不是字符数组 2)
ch
ar
ch
[5]={'\0'}; //字符串,不是字符数组,因为‘\0’与0等价。 3)
ch
ar
ch
[5]=""; //字符串,不是字符数组 4)
ch
ar
*
ch
= ""; //很明显的字符串 2、字符串的复制
ch
ar
ch
[5...
c语言文件操作,日常记事~~
void openFile(){ FILE *fp=fopen("d:/1.txt","w"); if(fp==
NULL
){ perror("Error"); } fclose(fp); fp=
NULL
; } void writeFile(){ //打开文件 FILE *fp=fopen("d:/1.txt","w");
ch
ar
ch
='a'; if(fp==
NULL
){ perror("Error".
C语言指针返回值
#include <stdio.h>
ch
ar
*mat
ch
(
ch
ar
*s,
ch
ar
ch
) /* 函数返回值的类型是字符指针 */ { while(*s != '\0') if(*s ==
ch
) return(s); /* 若在字符串s中找到字符
ch
,返回相应的地址 */ else s++; return(
NULL
); /* 在s中没有找到
ch
,返回空指针 */ } int main(void ) {
ch
ar
ch
, str[80], *p=
NULL
;
ch
ar
ch
2 = 0; .
while((
ch
= get
ch
ar
()) != EOF &&
ch
!=‘\n‘) | 作用 | 清空缓存区 | C和指针
C语言 利用while((
ch
= get
ch
ar
()) != EOF &&
ch
!='\n')进行清空缓存的通俗讲法。
Java SE
62,614
社区成员
307,327
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章