THe code does not compiles for me since the statement is missing greate than and less than size for a list <>
List s1 = new ArrayList( );
About Question enthuware.ocajp.i.v8.2.1465 :
Moderator: admin
-
- Posts: 2
- Joined: Sat Jan 19, 2019 4:38 pm
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1465 :
Please compile it from command line. There is nothing wrong with it. You will get compiler warning but no error. <> is required only if you are using generics.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Aug 25, 2021 7:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1465 :
Quick question. Shouldn't the List be delcared/defined like this:
List<String> s1 = new ArrayList<String>( );
Is String the default?
List<String> s1 = new ArrayList<String>( );
Is String the default?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1465 :
No, String is not the default. List s1 = new ArrayList( ); means that it is an untyped List, which means the compiler will not check what you put in that list. For example, s1.add(new Integer(1)); and s1.add("hello"); both will be accepted by the compiler.
But when you do List<String> s1 = new ArrayList<String>( ); the compiler will raise an error if you try to add anything other than a String to the list.
This is a feature of Java called "generics". Please go through this tutorial to understand the basics: https://docs.oracle.com/javase/tutorial ... index.html
But when you do List<String> s1 = new ArrayList<String>( ); the compiler will raise an error if you try to add anything other than a String to the list.
This is a feature of Java called "generics". Please go through this tutorial to understand the basics: https://docs.oracle.com/javase/tutorial ... index.html
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 123 guests