This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
in the first case you are creating a object type of string array of size 5. Since you haven't initialized value for elements of array so by default element will get null(which is default for object) in case of int array it will be 0. Now in second case you are creating object of type string array with size three and you have initialized values for the elements in the array.
For the detailed Explanation. To confirm, In both the above cases, Im trying to create an object of the class String Right? But in the first case Im not initiatialising any values, just declaring the array size alone as 6. Where as in the second case, Im trying to intialise objects . Hope Im right.
I was trying to initialise the objeccts to the array and it was throwing an error. please see the below code.
Also Whenever I declare any variable, for eg
All these does it indicate that Im creating an object of the class int & char??
In Line No 7, you get error because, its syntax error. Compiler doesnt know what that line mean.
Line No 5: says create a reference variable "names" which is of type String array, which can hold 6 references to String objects.
Line No 6: Its another way of initializing String array. Here you are also initializing the the elements of array, along with variable "name" of type String array.
And about second question, int, char are comes under primitive type. So you are not creating object of any class. Primitive types contain bit pattern which helps to find out its value.
Joined: Aug 26, 2008
Varshini Priya wrote:
In java Array is a type of object so when you declare
String  a = new String;
you are declaring an object of type string array and not the type of string. In your code you are getting error because you are declaring same variable 'names' twice. If you want to assign the values for elements of an array then assess elemens one by one and assign values.
if you write String a  = new String["a", "b"] then you are creating an array object of type string as well assigning the value of its element.