Difference between ArrayList iList = new ArrayList(); and ArrayList iList = new ArrayList(); [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2
















This question already has an answer here:




  • ArrayList Generic without Type

    5 answers




What is the difference between this two line:



ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();


Step 1: raise a compile time error



public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Step 2: works fine



public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Output:
Value of 2nd element: Test_Element



I am expecting an error like



"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.



Could anyone please explain me, why I am able to insert a String in this list.










share|improve this question















marked as duplicate by Denys Séguret java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    check out stackoverflow.com/questions/25689673/…

    – johnheroy
    Jan 3 at 7:14






  • 2





    That is because of raw types. Don't use them.

    – MC Emperor
    Jan 3 at 7:17











  • You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList

    – Vishwajit R. Shinde
    Jan 3 at 7:29


















2
















This question already has an answer here:




  • ArrayList Generic without Type

    5 answers




What is the difference between this two line:



ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();


Step 1: raise a compile time error



public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Step 2: works fine



public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Output:
Value of 2nd element: Test_Element



I am expecting an error like



"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.



Could anyone please explain me, why I am able to insert a String in this list.










share|improve this question















marked as duplicate by Denys Séguret java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    check out stackoverflow.com/questions/25689673/…

    – johnheroy
    Jan 3 at 7:14






  • 2





    That is because of raw types. Don't use them.

    – MC Emperor
    Jan 3 at 7:17











  • You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList

    – Vishwajit R. Shinde
    Jan 3 at 7:29














2












2








2









This question already has an answer here:




  • ArrayList Generic without Type

    5 answers




What is the difference between this two line:



ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();


Step 1: raise a compile time error



public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Step 2: works fine



public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Output:
Value of 2nd element: Test_Element



I am expecting an error like



"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.



Could anyone please explain me, why I am able to insert a String in this list.










share|improve this question

















This question already has an answer here:




  • ArrayList Generic without Type

    5 answers




What is the difference between this two line:



ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();


Step 1: raise a compile time error



public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Step 2: works fine



public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}


Output:
Value of 2nd element: Test_Element



I am expecting an error like



"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.



Could anyone please explain me, why I am able to insert a String in this list.





This question already has an answer here:




  • ArrayList Generic without Type

    5 answers








java generics arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 7:25







SKG

















asked Jan 3 at 7:10









SKGSKG

192




192




marked as duplicate by Denys Séguret java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Denys Séguret java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    check out stackoverflow.com/questions/25689673/…

    – johnheroy
    Jan 3 at 7:14






  • 2





    That is because of raw types. Don't use them.

    – MC Emperor
    Jan 3 at 7:17











  • You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList

    – Vishwajit R. Shinde
    Jan 3 at 7:29














  • 1





    check out stackoverflow.com/questions/25689673/…

    – johnheroy
    Jan 3 at 7:14






  • 2





    That is because of raw types. Don't use them.

    – MC Emperor
    Jan 3 at 7:17











  • You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList

    – Vishwajit R. Shinde
    Jan 3 at 7:29








1




1





check out stackoverflow.com/questions/25689673/…

– johnheroy
Jan 3 at 7:14





check out stackoverflow.com/questions/25689673/…

– johnheroy
Jan 3 at 7:14




2




2





That is because of raw types. Don't use them.

– MC Emperor
Jan 3 at 7:17





That is because of raw types. Don't use them.

– MC Emperor
Jan 3 at 7:17













You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList

– Vishwajit R. Shinde
Jan 3 at 7:29





You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList

– Vishwajit R. Shinde
Jan 3 at 7:29












1 Answer
1






active

oldest

votes


















0














There's no compile-time check on generic type of ArrayList when you write ArrayList list = new ArrayList<Integer>(). The generic type on the right side of the assignment expression is basically ignored. ArrayList doesn't really have an inner type, just that the T in ArrayList<T> helps you make sure you only perform operations with instances of T on your list.



It would be useful to understand why you want to have integers and strings mixed in the same ArrayList--there might be a design smell here :)






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There's no compile-time check on generic type of ArrayList when you write ArrayList list = new ArrayList<Integer>(). The generic type on the right side of the assignment expression is basically ignored. ArrayList doesn't really have an inner type, just that the T in ArrayList<T> helps you make sure you only perform operations with instances of T on your list.



    It would be useful to understand why you want to have integers and strings mixed in the same ArrayList--there might be a design smell here :)






    share|improve this answer




























      0














      There's no compile-time check on generic type of ArrayList when you write ArrayList list = new ArrayList<Integer>(). The generic type on the right side of the assignment expression is basically ignored. ArrayList doesn't really have an inner type, just that the T in ArrayList<T> helps you make sure you only perform operations with instances of T on your list.



      It would be useful to understand why you want to have integers and strings mixed in the same ArrayList--there might be a design smell here :)






      share|improve this answer


























        0












        0








        0







        There's no compile-time check on generic type of ArrayList when you write ArrayList list = new ArrayList<Integer>(). The generic type on the right side of the assignment expression is basically ignored. ArrayList doesn't really have an inner type, just that the T in ArrayList<T> helps you make sure you only perform operations with instances of T on your list.



        It would be useful to understand why you want to have integers and strings mixed in the same ArrayList--there might be a design smell here :)






        share|improve this answer













        There's no compile-time check on generic type of ArrayList when you write ArrayList list = new ArrayList<Integer>(). The generic type on the right side of the assignment expression is basically ignored. ArrayList doesn't really have an inner type, just that the T in ArrayList<T> helps you make sure you only perform operations with instances of T on your list.



        It would be useful to understand why you want to have integers and strings mixed in the same ArrayList--there might be a design smell here :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 7:16









        johnheroyjohnheroy

        32616




        32616

















            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules