Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:58:55+00:00 2026-06-14T10:58:55+00:00

i’m a newbie to java..i’m having difficulty in understanding generics. with what i understood

  • 0

i’m a newbie to java..i’m having difficulty in understanding generics. with what i understood i wrote the following demo program to understand Generics but there are errors..help required.

class GenDemoClass <I,S> 
{
    private S info;
    public GenDemoClass(S str)
    {
        info = str;
    }
    public void displaySolidRect(I length,I width)
    {
        I tempLength = length;
        System.out.println();
        while(length > 0)
        {
            System.out.print("          ");
            for(int i = 0 ; i < width; i++)
            {
                System.out.print("*");
            }
            System.out.println();
            length--;
        }
        info = "A Rectangle of Length = " + tempLength.toString() + " and Width = " + width.toString() + " was drawn;";     
    }

    public void displayInfo()
    {
        System.out.println(info);
    }
}

public class GenDemo
{
    public static void main(String Ar[])
    {
        GenDemoClass<Integer,String> GDC = new GenDemoClass<Integer,String>("Initailize");
        GDC.displaySolidRect(20,30);
        GDC.displayInfo();
    }
}

if i replace type variables I and S with Integer and String in the GenDemoClass then code seems to work..
the errors are

error: bad operand types for binary operator '>'
                while(length > 0)
                             ^
  first type:  I
  second type: int
  where I is a type-variable:
    I extends Object declared in class GenDemoClass
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:58:57+00:00Added an answer on June 14, 2026 at 10:58 am

    The problem is that most objects do not work with the > operator.

    If you declare that your type I must be a subtype of Number, then you can convert the instance of type I to an int primitive in the comparison. For instance

    class GenDemoClass <I extends Number,S> 
    {
    
    
    public void displaySolidRect(I length,I width)
        {
            I tempLength = length;
            System.out.println();
            while(length.intValue() > 0)
            {
    
            }
    

    At this point you’re sunk because you cannot modify the length value like you want to – it’s immutable. You can use a plain int for this purpose.

    public void displaySolidRect(I length,I width)
        {
            int intLength = length.intValue();
            int intHeight = width.intValue();
            System.out.println();
            while(intLength > 0)
            {
               // iterate as you normally would, decrementing the int primitives
            }
    

    In my opinion this is not an appropriate use of generics, as you don’t gain anything over using the primitive integer type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.