Java declare variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tecnetaz
    New Member
    • Jun 2019
    • 3

    Java declare variable

    What happens when the declare array like that?

    Char arr[];

    or what are there differences between them?

    Char arr[] = new char[6];
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    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:
    Code:
    char[][] arr = new char[2][];
    arr[0] = "Hello".ToCharArray();
    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.

    Comment

    • SagarEspark
      New Member
      • Jul 2019
      • 4

      #3
      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:

      Code:
      Char arr[];
      arr[] = new Char[6];
      or

      Code:
      Char arr[] = new Char[6];
      Hope this difference makes sense!

      Comment

      • Ishan Shah
        New Member
        • Jan 2020
        • 47

        #4
        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:

        Code:
        char arr[]; 
        OR 
        char[] arr;
        Initialization of char array :

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

        Code:
        char arr[] = new char[6];
        We use the following code to assign values to our char array:

        Code:
        char arr[] = new char[6];
        
        arr[0]='b';
        arr[1]='y';
        arr[2]='t';
        arr[3]='e';
        arr[4]='s';
        arr[5]='c';

        Comment

        Working...