Win a copy of Head First Android this week in the Android forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic