java - char的默认值是什么?
char c = '\u0000';
当我打印c时,它在命令行窗口中显示“a”。
那么char类型字段的默认值是什么?
有人说'\ u0000'在unicode中表示无效; 是对的吗?
user1298336 asked 2019-06-07T07:58:58Z
14个解决方案
84 votes
char属性的默认值确实是System.out.println('\u0000');(空字符),如Java语言规范,第4.12.5节“变量的初始值”一节中所述。
在我的系统中,System.out.println('\u0000');行打印一个小方块,这意味着它不是可打印的字符 - 正如预期的那样。
Óscar López answered 2019-06-07T07:59:20Z
22 votes
'\ u0000'是字符的默认值。 其十进制等值为0。
当你声明一些char变量而不初始化它时,默认情况下会为它分配'\ u0000'。
看到这段代码
public class Test {
char c;
public static void main(String args[]) throws Exception {
Test t = new Test();
char c1 = '\u0000';
System.out.println(t.c);
System.out.println(c1);
System.out.println(t.c == c1);
}
}
此代码将在最后一次打印时打印为true。
Chandra Sekhar answered 2019-06-07T08:00:06Z
6 votes
'\u0000'代表null。 因此,如果您打印一个未初始化的char变量,您将什么也得不到。
roshan answered 2019-06-07T08:00:32Z
5 votes
Character的默认值是Character.MIN_VALUE,其内部表示为MIN_VALUE = '\u0000'
此外,您可以检查字符字段是否包含默认值
Character DEFAULT_CHAR = new Character(Character.MIN_VALUE);
if (DEFAULT_CHAR.compareTo((Character) value) == 0)
{
}
Yogesh Manware answered 2019-06-07T08:01:05Z
3 votes
这是'\ u0000'。 浏览此处获取更多信息。
Matt Fenwick answered 2019-06-07T08:01:31Z
0 votes
默认char是int值为0(零)的字符。
char NULLCHAR =(char)0;
char NULLCHAR ='\ 0';
The Coordinator answered 2019-06-07T08:02:11Z
0 votes
它的诱人说法是白色空间或整数0,如下所示
char c1 = '\u0000';
System.out.println("*"+c1+"*");
System.out.println((int)c1);
但我不会这样说,因为它可能会因不同的平台或未来而有所不同。 我真正关心的是我永远不会使用这个默认值,所以在使用任何char之前只需检查它是否是\ u0000,然后使用它来避免程序中的误解。 就这么简单。
Mateen answered 2019-06-07T08:02:46Z
0 votes
char数据类型的默认值为'\ u0000'(或0),最大值为'\ uffff'(或65,535(含))。
你可以在这里看到信息。
Luis Parada answered 2019-06-07T08:03:19Z
0 votes
请注意,null和零之间存在明显差异。在[http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](上面引用)中,声明如下: -
还有一个特殊的null文字,可以用作任何引用类型的值。 null可以分配给任何变量,但基本类型的变量除外。 除了测试其存在之外,你几乎无法使用null值。 因此,null通常在程序中用作标记,以指示某些对象不可用。
这就是为什么以下陈述会给你一个错误,而不是另一个: -
char a = null; //类型不匹配:无法从null转换为char。
char b = 0; //有效语法
Choo answered 2019-06-07T08:04:15Z
0 votes
\u0000是Java中char类型的默认值
正如其他人提到的,您可以使用比较来检查未初始化变量的值。
char ch;
if(ch==0)
System.out.println("Default value is the null character");
Aneesh Sharma answered 2019-06-07T08:04:49Z
-1 votes
我认为这是'\u00000'或者只是''而不是'\u0000'(第一个有5个零,而最后一个有四个零。)
Bcom answered 2019-06-07T08:05:15Z
-1 votes
\ n \ n char的默认值为\ u0000
public class DefaultValues {
char varChar;
public static void main(String...l)
{
DefaultValues ob =new DefaultValues();
System.out.println(ob.varChar=='\u0000');
}
}
这将返回true
Shubham Malhotra answered 2019-06-07T08:05:50Z
-1 votes
char的默认值为null,根据Unicode图表为'\ u0000'。 让我们看看它在打印时是如何工作的。
public class Test_Class {
char c;
void printAll() {
System.out.println("c = " + c);
}
public static void main(String[] args) {
Test_Class f = new Test_Class();
f.printAll();
} }
注意:输出为空白。
lft93ryt answered 2019-06-07T08:06:24Z
-1 votes
char原语类型的默认值是'\ u0000'(空字符),如Java语言规范中所述。
'u0000'的快捷方式是'\ 0',因此null可以用'u0000'或'\ 0'表示。
下面的Java程序使用实例字段'c'.验证空表示公共类DefaultValueForchar { char c; public static void main(String [] args){ char c0 ='\ 0'; char cu0000 ='\ u0000'; DefaultValueForchar obj = new DefaultValueForchar(); 的System.out.println(obj.c); 的System.out.println(C0); 的System.out.println(cu0000); 的System.out.println(C0== cu0000); 的System.out.println(obj.c== C0); 的System.out.println(obj.c== cu0000); }}
javapedia.net answered 2019-06-07T08:07:21Z