About Question enthuware.ocajp.i.v8.2.925 :
Moderator: admin
-
- Posts: 2
- Joined: Sun Apr 30, 2017 8:07 am
- Contact:
About Question enthuware.ocajp.i.v8.2.925 :
The question asks which of the following code "compiles without error"?
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();
//INSERT CODE HERE
}
}
Then why option d) Double d = al.get(al.length); is correct since it compiles fine.
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();
//INSERT CODE HERE
}
}
Then why option d) Double d = al.get(al.length); is correct since it compiles fine.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
Are you sure it compiles fine? Did you read the explanation given with this option?
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
al.add(111); is not a valid option.
The add() is expecting Double, so autoboxing has to take place. Since al.add(111d); is working fine.
I don't understand:
< autoboxing will call new Double(), its constructor accepts double and String. Why al.add("111") doesn't work then?
< why the int 111 is't implicitely casted to double, then autoboxed to Double?
Thank you.
The add() is expecting Double, so autoboxing has to take place. Since al.add(111d); is working fine.
I don't understand:
< autoboxing will call new Double(), its constructor accepts double and String. Why al.add("111") doesn't work then?
< why the int 111 is't implicitely casted to double, then autoboxed to Double?
Thank you.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
Because the rules of boxing are clearly defined in JLS ( https://docs.oracle.com/javase/specs/jl ... #jls-5.1.7 ) and it states that if you have an int value then it can only be autoboxed into an Integer. Converting an int to double and then boxing that double into a Double is not permitted in a single step.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
Can you give me an example of how the indexOf Method works?
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
And one more thing a1.contains("string") will print Boolean value false right??
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
The code given in the question itself illustrates the use of indexOf. Try adding a few elements in al and then see what it prints for al.indexOf(1.0);
>And one more thing a1.contains("string") will print Boolean value false right?
What happened when you tried it out?
>And one more thing a1.contains("string") will print Boolean value false right?
What happened when you tried it out?
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
Code: Select all
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();
al.add(123.0); al.add(114.0); al.add(138.0); al.add(153.0); al.add(168.0);
System.out.println(al.indexOf(1.0)); //Getting -1 here no matter how many elements I add
System.out.println(al.contains("string")); //This returns false as expected
}
}
Last edited by admin on Fri Dec 15, 2017 9:15 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]
Reason: Please put code inside [code] [/code]
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
Well, the purpose of indexOf is to find out if the passed object exists in the list. If so, it returns the index at which it exists otherwise it returns -1.
So why do you expect it to return anything other than -1 if your list doesn't contain 1.0?
So why do you expect it to return anything other than -1 if your list doesn't contain 1.0?
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.925 :
Okay got it.Thanks!
Who is online
Users browsing this forum: No registered users and 7 guests