-3

my friend tried to print out a char variable without initialization. However, instead of showing up nothing, the screen showed up a letter 'a'.

and I asked him to initialized that variable like char c= '\u0000' and print it out again. But, it is still showing up as a letter 'a'.

however when he compared char c with real letter 'a', the result is false.

the code is

class  Char_a       
{
    public static void main(String[] args) 
    {
    char [] c=new char [5];

    for (int x=0 ; x<=4 ; x++ )
    {
    System.out.println("c["+x+"]="+c[x]);
    }

    char [] c2= {'\u0000'};
    System.out.println("c2[0]="+c2[0]);

    System.out.println("c2[0]==c[0]"+(c2[0]==c[0]));
    System.out.println("'a'==c[0]"+('a'==c[0]));

    }
}

his java version is 1.8.0_40 Jave (TM) SE Runtime Environment (build 1.8.0_40-b26) Java HotSpot (TM) 64 -Bit Server VM (build 25.40 -b25. mixed mode)

my friend just curious about why his char default value is showing up like letter 'a' even it is not same as the real letter 'a'.

and this is what he sees in his computer (Q.Q I cant post a picture..

but his result is kind of like

c[0]=a
c[1]=a
c[2]=a
c[3]=a
c[4]=a
c2[0]=a
c2[0]==c[0] true
'a' == c[0] false

and this is in my computer which is what you suppose to see

 c[0]= 
 c[1]= 
 c[2]= 
 c[3]= 
 c[4]= 
 c2[0]= 
 c2[0]==c[0] true
 'a' == c[0] false
xiaoming
  • 1
  • 3
  • 1
    `Char` doesn't exist in Java. `Character` and `char` do, but not `Char`. Case is important. – Makoto Jun 12 '15 at 16:17
  • sorry .. that was a typo.. but he is using char.. sorry – xiaoming Jun 12 '15 at 16:19
  • I cannot replicate that behavior. For me it just prints out nothing. There is most likely something extra in your friend's code that is causing this behavior. Seeing the entire text of the java class he is using would help in determining what exactly is going on. – SamYonnou Jun 12 '15 at 16:21
  • How does your friend run that code? Where does his program write it's output? If it's written to a file, what program is used to view it? Seems like some issue with character encoding... – fabian Jun 12 '15 at 16:31
  • Could you post the entire code, so we can see what's really happening? – Jose Luis Bernat Jun 12 '15 at 16:23
  • give me one second... let me ask my friend to get the picture... because in my computer it is show up normal which is nothing.. only in his computer is showing up like letter 'a' – xiaoming Jun 12 '15 at 16:26
  • Sure, but as the code compiles, the only option here is they are fields. – Jose Luis Bernat Jun 12 '15 at 16:34

2 Answers2

0

Several things here:

  • There are code points which are not printable, and \u0000 (NUL) is one of them. In my IDE it shows up as an empty box, but there's no guarantee that it'd show up at all for anyone else.

  • Your friend must be printing out b since that's the only value that would show up as a on screen.

  • The fact that your comparison returns false is accurate; since these are character points (and can be seen as numbers), the numbers are not the same. \u0000 is simply 0, and a is 97 (or 0x61 in hexadecimal).


After your revision, it seems that you and your friend are misunderstanding some things.

When you declare new char[5], you're creating an array that contains its initial value; for char, that's \u0000. So, there's no surprise that c2[0] == c[0], since c has no values in it.

a is a complete red herring. You're not assigning any values into the arrays, so it wouldn't make sense that a is equivalent to any value in those arrays.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • he also tries like a char array without initialization. and it comes out all letter 'a' even they are not same as letter 'a' . they just appear to be 'a' . it just kind of weird – xiaoming Jun 12 '15 at 16:22
  • You can't do that, though. You absolutely cannot use a variable before it's been initialized, even to print it. – Makoto Jun 12 '15 at 16:24
  • why his computer is showing up 'a' since the initial value for char is '\u0000' ? I know it is still '\u0000' . However ,it just shows up like an 'a' in my friend's computer. As you can see that my friend compare them and even it is show up like 'a' , it is not 'a'. – xiaoming Jun 12 '15 at 17:16
  • You haven't told us what OS he's running (or what OS you're running, for that matter). I'd also hedge my bets on it being an encoding issue with whatever terminal service you're using. Look on it with any other machine that is removed from those issues (like the Ideone example in another answer) and you won't observe that issue. – Makoto Jun 12 '15 at 17:19
0

Take a look here: https://ideone.com/kAFwea This is an online compilation showing that '\u0000' actually gets print as nothing not letter 'a'

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        char c = '\u0000';
        System.out.println("The char is: " + c + " not a");
    }
}

You can try as well with not initialized char variable.

The Javatar
  • 695
  • 5
  • 15