• 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

javabeat free mock test doubt

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
can somebody help me with the folowing code snippet:


i am confused at line no1:

Gen<String> arr[] = new Gen[5];
what does this line suggests with respect to what can we add to this??
i felt there is a problem in line 3 also as because we need to typecast it as we are adding it to specific generic type that is String. Am i getting the concept wrong???please help
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Debasmita!

I also find the combination of generics and arrays puzzling (at least ...).

You declared your array as:
Gen<String> arr[] = new Gen[5];

This line compiles, but gives a warning. The warning comes up because the thing is not type safe. And you already proved that. You put something wrong into the array with:
arr[1] = new Gen(1); //line 3 (warning)

It is not type safe, because it produces only a warning, but it compiles. The fact that it compiles shows that it isn't type safe. The warning also comes when you put something decent in it, like in line 2.





You asked, what can be stored in and get out of such an array.

IN
We saw it already, you can store anything in it. Or better, you can store only Objects. Because of polymorphism (remember: Object o = new String("howdy!"); ) you can store anything.

OUT
Here comes the problem: you can get only Strings out of the elements, as the variable is declared as Gen<String> arr[].

PROBLEM
With these arrays you can store anything, but it guarantees that only Strings will get out of the Gen elements at the places of the array. Did I said it already? This is not type safe.


Half Cooked:
There is some kind of type safety in this "generic arrays".
For example the following would not compile, if you hang it at the end of the main method of class Gen:
Gen<Integer> intgen = new Gen<Integer> (new Integer(5)); // ok, no warning
arr[4] = intgen; // no compile!

Compared to
Gen<String> strgen = new Gen<String>("Hi"); //ok
arr[4] = strgen; // no warning! (and compiles happily)

Here place #4 of the Gen<String> arr[] is refered to a Gen<String> variable, which is perfectly ok, and doesn't produce a warning. For variable strgen only can contain Strings (as Gen's variable named g). Gen's variable g will always be a String, it is safe to return only Strings.
Have a cup of tea and compare this situation with your line 2 (that gave a warning, but we know it is safe in this case).


You may also add the following line to the end of your main method to prove the warnings make sense:
String s = arr[1].g; // no warning

in arr[1] you stored a Gen parameterized with Integer, the parametirization of variable arr secures the return of a String, compiler compiles (without !!! a warning) but at runtime you get a class cast exception. In this line there is no warning, because compiler thinks, there are only Strings in it. But you were warned earlier (lines 1 and 3).








Something different, your casts, first line 4:
arr[2] = (Gen<String>) new Gen(1);//line 4

Line 4 causes a warning, but an other warning than before.
It has nothing to do with arrays.
Compiler just tells you that he is actually casting against the raw type of Gen, i.e. it is the same as if you have written:
arr[2] = (Gen) new Gen(1);//line 4'

I outcommented line 5 as it does not compile:
// arr[3] = (Gen<String>) new Gen<Integer>(1); //line 5
In spite of the fact, that the casting is (at the end) only against the raw type, compiler refuses to compile this line and reports inconvertible types.
Nice compiler.





By the way, I repaired this lines (4&5), as I think it should have been. With some combination of symbols, e.g. ;) there will be smilies instead of code. When you see this, you may hit the edit-Button(notepad icon) and resend your posting with a tick in "Dissasemble smilies..." below the text box for posting. Smilies are also disassembled here, otherwise you see a smiley (or graemlin how they're called here) instead of ;)




How was the tea?

Bu.
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard,
thanks for such beautiful explaination.

there are some things which are still not clear to me:
like in line 3 i am putting an Integer object, so as per rule in the advanced for loop it should give me a runtime errot when i am trying to access it. but why i am not getting the exception?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by debasmita pattnayak:
it should give me a runtime errot when i am trying to access it. but why i am not getting the exception?

Since you don't use the generic variable. Change the code to something like the following and you will get your expected exception.
[ July 06, 2007: Message edited by: Manfred Klug ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic