• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

SCJP Generics(problem with under standing)

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi this is shameem.sk.....
My preparation for the SCJP1.5 is struck with this problem..
can any one explan...(java.shameem@gmail.com)
class ObjectTest <type>
{
private type num1;
private type num2;
ObjectTest(type val1,type val2)
{
System.out.println("num1 in consturctor--->"+num1);
System.out.println("val2 in consturctor--->"+val2);
this.num1=val1;
System.out.println("num1 After Asigning in consturctor>"+num1);
this.num2=val2;

}
public type getValue1()
{
return num1;
}
public type getValue2()
{
return num2;
}
};
class GenTest
{
public static void main(String args[])
{
ObjectTest<Integer> it1= new ObjectTest<Integer>(236, 175);
System.out.println("int value 1-------->"+it1.getValue1());
System.out.println("int value 2-------->"+it1.getValue2());
System.out.println("---Before passing the String Object--");
ObjectTest <String> it2= new ObjectTest<String>( "ishmayel","Raj");
System.out.println(" String 1------->"+it1.getValue1());
System.out.println(" String 2------->"+it1.getValue2());
}

};
When Run the above code I get the out put like this..............

****************output Starts*****************************
num1 in consturctor--------->null
val2 in consturctor--------->175
num1 After Asigning in consturctor----->236
int value 1---------->236
int value 2---------->175
----Before passing the String Object----
num1 in consturctor---->null
val2 in consturctor---->Raj
num1 After Asigning in consturctor------->ishmayel
String 1----->236
String 2------>175

****************output Ends*****************************
q) why we got the
String 1------>236
String 2------>175

why we are not getting the out put like this

String 1------>236
String 2------>175

Thanks & Regards..
Shameem.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(" String 1------->"+it1.getValue1());
System.out.println(" String 2------->"+it1.getValue2());

Because of, see above bold text. Put it2, you will get expected answer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic