• 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

SCJP Generics geting the compilation Error

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ObjectTest <type>
{
private type num1;
private type num2;
ObjectTest(type val1,type val2)
{
this.num1=val1;
this.num2=val2;
}
public type getValue1()
{
return num1;
}
public type getValue2()
{
return num2+num1; //Getting the compilation Error Here
}
};
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());
}
};
when I compile the Above code I am getting the compilation Error at the comment line. Why that error raising...
I am adding the same data types...what is the reasion behind this.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The + binary operator only works as addition or String concatenation. You can't use it to add two object references that don't refer to Strings or wrapper classes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic