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

anshulkatta
  • 1,952
  • 18
  • 29
  • 6
    if(f.listofinteger.get(1) instanceof Integer); – dev2d Jun 03 '13 at 07:49
  • @VJD: That is the problem with the code, you should post it as an answer. – Keppil Jun 03 '13 at 08:01
  • oh a semi colon is there right :/ – anshulkatta Jun 03 '13 at 08: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 – anshulkatta Jun 03 '13 at 08: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 and check – dev2d Jun 03 '13 at 08:46
  • @Keppil : Thanks for comment, will keep it in mind going ahead – dev2d Jun 03 '13 at 08:46
  • @VJD you can still put an answer i ll accept you , thanks for explanation – anshulkatta Jun 03 '13 at 08:52
  • @anshulkatta thanks, but thats okay. ultimately it's all about helping :) – dev2d Jun 03 '13 at 08:58

3 Answers3

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..

Community
  • 1
  • 1
BobTheBuilder
  • 17,650
  • 5
  • 35
  • 58
  • That is true, but the validation done here has nothing to do with generics. The problem with OPs code is a syntax error. – Keppil Jun 03 '13 at 07:58
  • @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 – anshulkatta Jun 03 '13 at 08:35
  • 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. – BobTheBuilder Jun 03 '13 at 08:41
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.)

Andrzej Doyle
  • 97,637
  • 30
  • 185
  • 225
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...

Manuel Pires
  • 617
  • 3
  • 19