By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,064 Members | 449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,064 IT Pros & Developers. It's quick & easy.

Java declare variable

P: 3
What happens when the declare array like that?

Char arr[];

or what are there differences between them?

Char arr[] = new char[6];
Jul 1 '19 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 313
char arr[] : It's a character array, which is different from a String in many manners. Like:
- String methods cannot be used
-'+' overloaded operator can't be used
- It is stored in heap memory whereas String literals are stored in the String constant pool.
-A string can't be iterated over characters using loop, etc.

char arr[] = new char[6]
I am not sure about this declaration. 'new' is used to dynamically allocate memory. So the right hand side of the expression is allocating memory and returning an address.
When we declare an array, its name points to the first element. Here I guess the returned address is stored in arr.

On a side note, new is used with jagged arrays like:
Expand|Select|Wrap|Line Numbers
  1. char[][] arr = new char[2][];
  2. arr[0] = "Hello".ToCharArray();
  3. arr[1] = "cat".ToCharArray();
But it's a 2D system with variable number of columns in each row.

Share the link of the resource/article you are referring if you could.
Jul 2 '19 #2

P: 4
Char arr[]; is a syntax used to declare a character array without specifying length or size of it.

We need to write the following in the next line,
arr=new Char[6];
In this, we do not need to write Char[], class, again because the array is declared already.

Char arr[]=new Char[6] will create an array of characters with length 6 from starting index 0 to 5.

So, there are two methods which come in this scenario:

Either you write this:

Expand|Select|Wrap|Line Numbers
  1. Char arr[];
  2. arr[] = new Char[6];

Expand|Select|Wrap|Line Numbers
  1. Char arr[] = new Char[6];
Hope this difference makes sense!
Jul 3 '19 #3

P: 33
The noted fact is that Strings are immutable because it's interal state can't be changed after creation. however with char arrays, character buffer can be manipulated. Character arrays are faster, as data can be manipulated without any allocations.

Declaration of a char array can be done by using square brackets:

Expand|Select|Wrap|Line Numbers
  1. char arr[]; 
  2. OR 
  3. char[] arr;
Initialization of char array :

A char array can be initialized by conferring to it a default size.

Expand|Select|Wrap|Line Numbers
  1. char arr[] = new char[6];
We use the following code to assign values to our char array:

Expand|Select|Wrap|Line Numbers
  1. char arr[] = new char[6];
  3. arr[0]='b';
  4. arr[1]='y';
  5. arr[2]='t';
  6. arr[3]='e';
  7. arr[4]='s';
  8. arr[5]='c';
Sep 14 '20 #4

Post your reply

Sign in to post your reply or Sign up for a free account.