ocpjp

java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException

限于喜欢 提交于 2019-11-30 17:43:43
I am preparing for an OCPJP 8 exam for the next 2 months and currently I this one got my attention as I dont understand why public class BiPredicateTest { public static void main(String[] args) { BiPredicate<List<Integer>, Integer> containsInt = List::contains; List<Integer> ints = java.util.Arrays.asList(1,20,20); ints.add(1); ints.add(20); ints.add(20); System.out.println(containsInt.test(ints, 20)); BiConsumer<List<Integer>, Integer> listInt = BiPredicateTest::consumeMe; listInt.accept(ints, 15); } public static void consumeMe(List<Integer> ints, int num) { ints.removeIf(i -> i>num); ints

java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException

流过昼夜 提交于 2019-11-30 01:16:24
问题 I am preparing for an OCPJP 8 exam for the next 2 months and currently I this one got my attention as I dont understand why public class BiPredicateTest { public static void main(String[] args) { BiPredicate<List<Integer>, Integer> containsInt = List::contains; List<Integer> ints = java.util.Arrays.asList(1,20,20); ints.add(1); ints.add(20); ints.add(20); System.out.println(containsInt.test(ints, 20)); BiConsumer<List<Integer>, Integer> listInt = BiPredicateTest::consumeMe; listInt.accept

Formatting using printf and format

青春壹個敷衍的年華 提交于 2019-11-29 02:50:49
In the following program class ZiggyTest2 { public static void main(String[] args){ double x = 123.456; char c = 65; int i = 65; System.out.printf("%s",x); System.out.printf("%b",x); System.out.printf("%c",c); System.out.printf("%5.0f",x); System.out.printf("%d",i); } } The output is 123.456trueA 12365 Can someone please explain how a double value (i.e. 123.456 ) is converted to a boolean (ie. true ) The reason I ask is because I know java does not allow numbers to be used for boolean values. For example, the following is not allowed in Java if (5) { //do something } Thanks for "%b" : If the

Garbage collection mock for the OCPJP exam

不打扰是莪最后的温柔 提交于 2019-11-28 06:50:54
问题 Four objects are eligible for garbage collection when i3 = null; is executed in the class shown below. I've added comments to explain how I got this answer. Is my reasoning correct? public class Icelandic extends Horse{ public void makeNoise(){ System.out.println("vinny"); } public static void main(String args[]){ /** * 2 objects created */ Icelandic i1 = new Icelandic(); /** * 2 objects created */ Icelandic i2 = new Icelandic(); /** * 2 objects created */ Icelandic i3 = new Icelandic(); /**

Formatting using printf and format

元气小坏坏 提交于 2019-11-27 17:07:58
问题 In the following program class ZiggyTest2 { public static void main(String[] args){ double x = 123.456; char c = 65; int i = 65; System.out.printf("%s",x); System.out.printf("%b",x); System.out.printf("%c",c); System.out.printf("%5.0f",x); System.out.printf("%d",i); } } The output is 123.456trueA 12365 Can someone please explain how a double value (i.e. 123.456 ) is converted to a boolean (ie. true ) The reason I ask is because I know java does not allow numbers to be used for boolean values.

How exactly does String.split() method in Java work when regex is provided?

与世无争的帅哥 提交于 2019-11-27 02:01:40
I'm preparing for OCPJP exam and I ran into the following example: class Test { public static void main(String args[]) { String test = "I am preparing for OCPJP"; String[] tokens = test.split("\\S"); System.out.println(tokens.length); } } This code prints 16. I was expecting something like no_of_characters + 1. Can someone explain me, what does the split() method actually do in this case? I just don't get it... Pshemo It splits on every "\\S" which in regex engine represents \S non-whitespace character. So lets try to split "x x" on non-whitespace ( \S ). Since this regex can be matched by one

Java SneakyThrow of exceptions, type erasure

和自甴很熟 提交于 2019-11-27 01:44:06
Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex) { SneakyThrow.<RuntimeException>sneakyThrowInner(ex); } private static <T extends Throwable> T sneakyThrowInner(Throwable ex) throws T { throw (T) ex; } public static void main(String[] args) { SneakyThrow.sneakyThrow(new Exception()); } } It may seems strange, but this doesn't produce a cast exception, and permits to throw a checked exception without having to declare it in the signature, or to wrap it in an unchecked exception. Notice that neither sneakyThrow(...) or the main are declaring

Slight confusion regarding overriding where variables are concerned

六月ゝ 毕业季﹏ 提交于 2019-11-26 20:31:31
I'm preparing for the SCJP (recently rebranded as OCPJP by Oracle) and one particular question that I got wrong on a mock exam has confused me, the answer description doesn't explain things clear enough. This is the question : class A { int x = 5; } class B extends A { int x = 6; } public class CovariantTest { public A getObject() { return new A(); } public static void main(String[]args) { CovariantTest c1 = new SubCovariantTest(); System.out.println(c1.getObject().x); } } class SubCovariantTest extends CovariantTest { public B getObject() { return new B(); } } The answer is 5 , but I chose 6

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

白昼怎懂夜的黑 提交于 2019-11-26 11:50:24
public class Java{ public static void main(String[] args){ final byte x = 1; final byte y = 2; byte z = x + y;//ok System.out.println(z); byte a = 1; byte b = 2; byte c = a + b; //Compiler error System.out.println(c); } } If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte. Why does it happen when we add two final bytes that fit in a byte? There is no compiler error. From the JLS 5.2 Assignment Conversion In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int : - A

Why does Double.NaN==Double.NaN return false?

﹥>﹥吖頭↗ 提交于 2019-11-26 11:43:58
I was just studying OCPJP questions and I found this strange code: public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); } When I ran the code, I got: false true How is the output false when we're comparing two things that look the same as each other? What does NaN mean? Adrian Mitev NaN means "Not a Number". Java Language Specification (JLS) Third Edition says : An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no