About Question enthuware.ocajp.i.v7.2.871 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.871 :

Post by ETS User »

Code: Select all

import java.util.*;
public class TestClass {
    public static void main(String[] args) throws Exception {
        ArrayList<Integer> al = new ArrayList<>(); //1
        al.add(111); //2
        System.out.println(al.get(al.size()));  //3
     }
}
When I run this code it won't compile

This line
ArrayList<Integer> al = new ArrayList<>(); //1

Should be ArrayList<Integer> al = new ArrayList<Integer>(); //1

Then I get the index out of bounds exception

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by admin »

Are you sure you are using Java 7?
If you like our products and services, please help us by posting your review here.

christian_burger
Posts: 4
Joined: Thu Apr 21, 2016 2:46 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by christian_burger »

Hello,
I'm having the same problem as I expected initially when looking at the question, which I marked "won't compile".
This is the actual result:

burger question2871 # java -version
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

burger question2871 # javac ./TestClass.java
Note: ./TestClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

When using the -Xlint flag for the compiler it compiles with warnings.

burger question2871 # javac ./TestClass.java -Xlint
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd
./TestClass.java:4: warning: [rawtypes] found raw type: List
List al = new ArrayList(); //1
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
./TestClass.java:4: warning: [rawtypes] found raw type: ArrayList
List al = new ArrayList(); //1
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
./TestClass.java:5: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
al.add(111); //2
^
where E is a type-variable:
E extends Object declared in interface List
3 warnings

burger question2871 # java TestClass
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at TestClass.main(TestClass.java:6)

Pooja1993
Posts: 1
Joined: Wed Jul 27, 2016 2:56 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by Pooja1993 »

IndexOutOfBounds is coming because the string is taking the 1st index position which is not present it should be:

System.out.println(al.get(al.size()-1));

christian_burger
Posts: 4
Joined: Thu Apr 21, 2016 2:46 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by christian_burger »

Yes, I know, I mainly wanted to point out that the code won't compile as mentioned in the first post.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by admin »

The code given in the question complies fine and so does the code posted in the first post.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by JuergGogo »

I don't really understand how an ArrayList can be declared and initialized in a non-generic way:
List al = new ArrayList(); it seems to be an issue of backward compability.

Code: Select all

import java.util.*;
public class TestClass {
    
    public static void main(String[] args) throws Exception {
        List  al = new ArrayList(); //1
        al.add(111);//2
        al.add("gogo");
        System.out.println( al );
        System.out.println( al.get(0).getClass() );
        System.out.println( al.get(1).getClass() );
        //System.out.println(al.get(al.size()));  //3
    }     
}
output:
[111, gogo]
class java.lang.Integer
class java.lang.String

The object type hasn't been lost that way. How can I find out the reference type? Object? Is an non-generic ArrayList identical with ArrayList<Object>?

al.get(1).substring(1); --> compiler error
((String)al.get(1)).substring(1); --> ok, does compile

Thank you.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by admin »

Information given in generics is only for the compiler. So when you write List<String>, it is only the compiler that makes use of <String>. That is why al.get(1).substring(1); will compile only if you declare al to be of type List<String>. Because now the compiler knows that each element of the list will be a String and so al.get(1) will be a String and it is ok to call substring.

But if you define al to be of type List, the compiler has no idea what the list might contain. It can contain any object. So it is not sure that al.get(1).substring(1); will always be a valid invocation. That is why it fails compilation.

I am not sure what you mean by "issue of backward compatibility".
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by JuergGogo »

Thank you, Paul.

backward compability: ArrayList exists since Java 1.2 and Generics were introduced later with 1.5.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by admin »

JuergGogo wrote:Thank you, Paul.

backward compability: ArrayList exists since Java 1.2 and Generics were introduced later with 1.5.
OK, so what issue do you see with it?
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by JuergGogo »

I couldn't find any information about non-generic List/ArrayList in the Java 8 Api-Library. I'm happy with my conclusion, that ArrayList al; is identical to ArrayList<Object> al;

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by admin »

JuergGogo wrote:I couldn't find any information about non-generic List/ArrayList in the Java 8 Api-Library. I'm happy with my conclusion, that ArrayList al; is identical to ArrayList<Object> al;
Your conclusion is incorrect. Try this - https://stackoverflow.com/questions/788 ... listobject

But more importantly, it tells me that you haven't really understood the basic concept of generics. I will suggest you to read this topic from some good book. It is very important to clear misunderstandings about generics asap.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by JuergGogo »

Thank you, Paul.
There is an Oracle Tutorial on Generics, I will study that one day.

Code: Select all

import java.util.*;

public class TestClass {    
    public static void main(String[] args)  {
             
        List l = new ArrayList();
        List<Object> lo = new ArrayList<>();
        List<String> ls = new ArrayList<>();
        
        l = ls;     // ok, does compile
        lo = ls;   // Compiler error: incompatible types: String cannot be converted to Object
        
    }
}
For now I will remember that above code will generate a compiler error. Its message is quite surprising, that a String can't be converted to an Object. Normally an implicit widening cast.

Edit: I found this statement in JavaLanguageDescription.
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by Bhaskar »

The default capacity of ArrayList is 10. Why does it not print null?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by admin »

Bhaskar wrote:
Fri Aug 02, 2019 7:15 am
The default capacity of ArrayList is 10. Why does it not print null?
Not sure I understand your question. Could you be more clear?
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.871 :

Post by Bhaskar »

admin wrote:
Fri Aug 02, 2019 7:19 am
Bhaskar wrote:
Fri Aug 02, 2019 7:15 am
The default capacity of ArrayList is 10. Why does it not print null?
Not sure I understand your question. Could you be more clear?
I've managed to clarify the doubt, but my question went about like this:
The initial capacity of ArrayList is 10 at the time of its creation. This capacity determines the size of the internal Object array used to store the ArrayList values, initialized to null. I was under the impression that, ArrayList.size() returns the size of this internal Object array, but it turns out that it returns the number of elements actually inserted in the ArrayList.

Therefore, the answer is indexOutOfBoundsException and not null.

Please fell free to correct me if i am wrong.

Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests