File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Using String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using String " Watch "Using String " New topic
Author

Using String

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93


Hi


Im trying to declare a String array and I would like to know the difference in the below two types of declration




I understand that in the first case, Im creating an object "names" which is of the type String . But Im not clear with the difference between the above two declaration. Please Assist. Many Thanks

Regards,
Varshini
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Hi Varsini,

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.

Hope this will be helpful.


SCJP 1.5(97%) My Blog
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93


Thanks Shivendra,

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??

please Advise, Many Thanks

Regards,
Varshini

Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

Varshini Priya wrote:




You have declared 'names' twice
Varshini Priya wrote:

Also Whenever I declare any variable, for eg


All these does it indicate that Im creating an object of the class int & char??

int and char are primitives types in Java, they are not Objects as such. The equivalent Java Objects are java.lang.Integer and java.lang.Character

If you write

it would mean you have created a reference 'i' to a new java.lang.Integer Object whose value is 2.
sujith Acharya
Ranch Hand

Joined: Dec 25, 2006
Posts: 60
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.
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Varshini Priya wrote:






In java Array is a type of object so when you declare
String [] a = new String[5];
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
Good explanation, but . . .

shivendra tripathi wrote:String a [] = new String["a", "b"]
???

Surely it's this?

And always keep the [] as part of the type. The type is String[], so writing String[] a is better than String a[].
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Well spotted campbell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using String