0

The List listofinteger is of type Integer , but accepting String Object and when i do check for instance of Integer , it is giving true , its too strange , can anybody explain what is happening.

All i know is when i send the List listofinteger to method ,it is given reference to List reference variable of no type , when i add it takes the input as String , now when i return the List listofanything to List of Integer type , it should take because its a reference .

Now but i check for instanceof , it is printing true for Integer , but its String.

import java.util.ArrayList;
import java.util.List;

public class TestEx {

     List<Integer> listofinteger=new ArrayList<Integer>();      //list of integer type
     public static void main(String... args)
     {

         TestEx f=new TestEx();
         f.listofinteger.add(123);              //adds integer by wrapping it(auto)

         f.listofinteger=f.addelement(f.listofinteger);

         if(f.listofinteger.get(1) instanceof Integer);
         {
             System.out.println("true");                   //prints true here
             System.out.println(f.listofinteger.get(1));
         }



     }
      List<Integer> addelement(List listofanything)
     {
          listofanything.add("asdasdasd");            //adding String object

        return listofanything;
     }
}    

I know that the method addelement(List listofanything ) here should be given a type Integer but i am here testing it , to understand the concept of Generics

8
  • 6
    if(f.listofinteger.get(1) instanceof Integer); <- remove semi-colon Commented Jun 3, 2013 at 7:49
  • @VJD: That is the problem with the code, you should post it as an answer. Commented Jun 3, 2013 at 8:01
  • oh a semi colon is there right :/ Commented Jun 3, 2013 at 8:23
  • @VJD its a semi colon problem , well , i have another query that why it is actually allowing a string , if java developers thought that it will stop this problem of adding any object into list , then in first place they allowed to be list of some type to be given reference to simple type of list Commented Jun 3, 2013 at 8:34
  • @anshulkatta well, you are not adding it to an Integer list but List, it will allow you to add it, try changing listofanything from List to List<Integer> and check Commented Jun 3, 2013 at 8:46

3 Answers 3

5

First of all, as @VJD commented, you have a syntax error - unneeded ; at:

if(f.listofinteger.get(1) instanceof Integer);

About your question, generics are compile time tool to check for type safety. In runtime there's no validation, as of type erasure. That's why you get no error adding String to list of Integers..

Sign up to request clarification or add additional context in comments.

3 Comments

That is true, but the validation done here has nothing to do with generics. The problem with OPs code is a syntax error.
@whoAMI its a semi colon problem , well , i have another query that why it is actually allowing a string , if java developers thought that it will stop this problem of adding any object into list , then in first place they allowed to be list of some type to be given reference to simple type of list
I didn't really understand your question, but generics are much later concept (introduced in java 1.5) than java itself. You can try and read more a the question I've linked and all around the web.
1

Your program prints true because of a syntax error in your code, which happens to be legal with a different meaning to what you intended.

Your if statement is

  if(f.listofinteger.get(1) instanceof Integer);

The semicolon at the end ends the whole statement, and makes it equivalent to passing an empty block:

  if(f.listofinteger.get(1) instanceof Integer) {
    // Do nothing
  }

After that you have the block that was supposed to be part of the if-condition, but is now just an anonymous block. (You can add braces in your code more or less wherever you want, to separate statements for scoping purposes. They still execute in the standard order).

Putting both of them together, your code is equivalent to:

  if(f.listofinteger.get(1) instanceof Integer) {
    // Do nothing
  }

  System.out.println("true");                   //prints true here
  System.out.println(f.listofinteger.get(1));

and so it should be clear that the last two lines will always be executed, regardless of what the if condition was.

As others have noted, you can fix this by removing the semicolon after the if statement. (And since this is a confusing and hard-to-spot problem, many static analysis tool such as FindBugs will highlight these semicolons as a likely problem.)

Comments

0

You are passing your List as an Generic List...

You need to identify your List as List <Integer> listofanything for your code give an error if you add a string... I changed your addElement code to:

List<Integer> addelement(List<Integer> listofanything)
 {
      listofanything.add("asdasdasd");            //Error when adding

    return listofanything;
 }

and an error appeared compiling...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.