• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using String

 
Ranch Hand
Posts: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Varshini Priya
Ranch Hand
Posts: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well spotted campbell
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic