12 Replies - 232 Views - Last Post: 19 January 2013 - 11:36 AM Rate Topic: -----

#1 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Default value of primitive type char

Posted 18 January 2013 - 07:43 PM

public class Test
{
	char ch1 = '\u0000';
	char ch2;
	char ch3 = ' ';
	
	public static void main(String [] args)
	{
		// Do something...
	}
}

In the code above, (if I understand correctly), the values for ch1 and ch2 are null which is represented by whitespace. However, I am curious, is the value for ch3 also null (which is also represented by whitespace). Are all three variables interchangeable or just ch1 and ch2? Any feedback is much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Default value of primitive type char

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Default value of primitive type char

Posted 18 January 2013 - 07:51 PM

What do you mean by 'interchangeable'?
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Default value of primitive type char

Posted 18 January 2013 - 07:52 PM

There is no null for primitives like char, so

char ch1;



should not compile.

ch and ch3 are an empty space character.
Was This Post Helpful? 1
  • +
  • -

#4 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Default value of primitive type char

Posted 18 January 2013 - 08:00 PM

View PostGregBrannon, on 18 January 2013 - 07:51 PM, said:

What do you mean by 'interchangeable'?

I meant did all three variables equal to null. I just checked The Java Tutorials again and it turns out that '\u0000' is acutally a value (0) so that is definitely not null.
Was This Post Helpful? 0
  • +
  • -

#5 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Default value of primitive type char

Posted 18 January 2013 - 08:08 PM

View Postfarrell2k, on 18 January 2013 - 07:52 PM, said:

There is no null for primitives like char, so

char ch1;



should not compile.

ch and ch3 are an empty space character.

public class Test
{
	char ch1 = '\u0000';
	char ch2;
	char ch3 = ' ';
	
	public static void main(String [] args)
	{
		Test t = new Test();
		System.out.println(t.ch1);
		System.out.println(t.ch2);
		System.out.println(t.ch3);
	}
}

When I ran the code above in main(), it just shows white space but I don't get any compile-time errors so I don't think I understand what you mean. I also overlooked that ch1 is actually a value (0) according to the Java Tutorials but I'm still not sure if ch3 has a Unicode representation or if it is null. I would appreciate it if you can explain ch3 to me. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Default value of primitive type char

Posted 18 January 2013 - 08:10 PM

There is no null for char, but you are right - it does compile.

This post has been edited by farrell2k: 18 January 2013 - 08:12 PM

Was This Post Helpful? 1
  • +
  • -

#7 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Default value of primitive type char

Posted 18 January 2013 - 08:20 PM

View Postfarrell2k, on 18 January 2013 - 08:10 PM, said:

There is no null for char,

Oh I see now. Apparently, there is an error in a Java book I am reading. The book says the default value for char is 'u\0000'(null). It probably should have said something like 'u\0000'(whitespace) LOL. That's why I was confused. Thanks, I appreciate your help. :D
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Default value of primitive type char

Posted 18 January 2013 - 08:23 PM

No. It's confusing, but it represents null which is actually NUL, ascii 0.

So I am technically wrong when I say there is no null, but it is not null like for objects. It is NUL. Confused yet? :)

This post has been edited by farrell2k: 18 January 2013 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#9 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Default value of primitive type char

Posted 18 January 2013 - 08:29 PM

View Postfarrell2k, on 18 January 2013 - 08:23 PM, said:

No. It's confusing, but it represents null which is actually NUL, 0.

Very confusing. I just tried to google this NUL. Apparently, this is a control character with the value zero. Is that correct, I'm not familiar with ASCII or Unicode.
Was This Post Helpful? 1
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Default value of primitive type char

Posted 18 January 2013 - 08:31 PM

View Postjjh08, on 19 January 2013 - 03:29 AM, said:

View Postfarrell2k, on 18 January 2013 - 08:23 PM, said:

No. It's confusing, but it represents null which is actually NUL, 0.

Very confusing. I just tried to google this NUL. Apparently, this is a control character with the value zero. Is that correct, I'm not familiar with ASCII or Unicode.



Yeah, think of it as ascii character nothing. I edited the post you quoted. It makes more sense if you look at the ascii table.
Was This Post Helpful? 1
  • +
  • -

#11 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Default value of primitive type char

Posted 18 January 2013 - 08:41 PM

View Postfarrell2k, on 18 January 2013 - 08:31 PM, said:

View Postjjh08, on 19 January 2013 - 03:29 AM, said:

View Postfarrell2k, on 18 January 2013 - 08:23 PM, said:

No. It's confusing, but it represents null which is actually NUL, 0.

Very confusing. I just tried to google this NUL. Apparently, this is a control character with the value zero. Is that correct, I'm not familiar with ASCII or Unicode.



Yeah, think of it as ascii character nothing. I edited the post you quoted. It makes more sense if you look at the ascii table.

Ok, now I get it. I just looked at the ASCII table here and I see that the symbol is NUL and the description is null char. When I thought of null, I always thought of objects also but I see now...the ASCII/Unicode definitions make it confusing but it's really not once you look at the table. Thanks :)
Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Default value of primitive type char

Posted 19 January 2013 - 11:32 AM

char are int so initialized to 0 when used as instance variables.
Whithin code, local char variables will generate a compile error if rhey might not initialized before they get used.
Was This Post Helpful? 1
  • +
  • -

#13 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Default value of primitive type char

Posted 19 January 2013 - 11:36 AM

View Postpbl, on 19 January 2013 - 06:32 PM, said:

char are int so initialized to 0 when used as instance variables.
Whithin code, local char variables will generate a compile error if rhey might not initialized before they get used.


You the man! I knew I remembered something about that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1