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

  • SEARCH
  • Home
  • 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 8959263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:23:43+00:00 2026-06-15T15:23:43+00:00

My code is: import java.util.*; public class A { public static void main(String[] args){

  • 0

My code is:

import java.util.*;

public class A {
public static void main(String[] args){
    List<String> list = new ArrayList();
    list.add("1"); //ok   line 1
    list.add(1); //error  line 2
}

When I run this code Java gives me an error and I know why, but even when I only use line 1 the compiler warns me. Why do I get this warning? I don’t understand what is the difference between my first example and this code:

import java.util.*;

public class A {
    public static void main(String[] args){
        List<String> list = new ArrayList<String>(); // <-- notice the second <String>
        list.add("1"); //ok   line 1
        list.add(1); //error  line 2
    }
}
  • 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-15T15:23:44+00:00Added an answer on June 15, 2026 at 3:23 pm

    When I run this code Java gives me an error and I know why, but even when I only use line 1 the compiler warns me. Why do I get this warning?

    When you use your code, compiler will give you a warning like

    List<String> list = new ArrayList();
    

    ArrayList is a raw type. References to generic type ArrayList should be parameterized

    It says that you have to provide a parameterized type for your ArrayList as well.

    List<String> list = new ArrayList<String>();
    

    EDIT:

    As sugguested by @newacct: if you are using Java 7, you could use empty angular brackets (<>) when instantiating your Collection like:

    List<String> list = new ArrayList<>();
    

    From the Java 7 documentation:

    You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context.

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

Sidebar

Related Questions

Here's my code: import java.util.Scanner; public class Arrays { public static void main(String[] args)
Java Code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[]
Here is the code: import java.util.*; public class dayName { public static void main(String[]
Here is my code: import java.util.Scanner; public class BankAccount1 { public static void main(String[]
Here is my code: import java.util.*; public class Multiply { public static void main(String[]
I have this code: import java.util.Scanner; public class Example { public static void main(String[]
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
My code: import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.Scanner; public class main { public static

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.