📜 ⬆️ ⬇️

New superblits on java hardcore

So, yesterday we played in the java version of the “Intellectual casino against the experts”, and at the same time, with all due respect to the residents of the capital, the viewers won! The winner of this stage was Sergei SerCe Tselovalnikov, who solved three tasks. As promised, we give him a small prize: a VIP-ticket for the JPoint!

But joy aside. Yesterday, with TV viewers, we were accused of the fact that most of yesterday’s tasks were related to Java very indirectly. We accept this charge, and so today we have a pure Java version! no springs, examels and patterns. This will be a real test for true hardcore lovers!

Under the cut - the answers to yesterday's round and super blitz! Against the experts today plays a viewer from St. Petersburg Andrei apangin Pangin .
')




But first, the answers to yesterday's tasks .

Various aspects from Nikolai Garbuzov
Does the following aspect compile AJC compiler?
If so, what will it output to the console during compilation?

public aspect QuizAspect { public static int count(int i) { return i++; } before (int n) : execution(public int QuizAspect.count(int)) && args(n) && if(QuizAspect.count(1)>1) { System.out.println("QuizAspect " + n); } } 


Correct answer. Compiles successfully, but will not output anything to the console when compiling. In this case, calling the Quiz.count method in runtime will result in a StackOverflowError error!

You might think that the expression in if () will start when the code is compiled, but it is not.
(don't confuse aspects with meta programming)
The aspect will only embed the council code inside the count method, add if () to this code.


Expressions from Vladimir Sitnikov
What is the catch to delete Java comments with such an expression? List 3 reasons why this should not be done. (we believe that the source code is written in normal characters) -
 Pattern.compile("/\\*(?:[^*]|\\*[^/])*\\*/") 


Answer
  1. If this is a Java string, and supposedly a comment in it, it will not work
  2. If koment ends with two asterisks slash, it will not work
  3. If the comment is large, we’ll get a StackOverflowError, since Java regexps use backtracking.


Fix it in two ways:
  • Possessive quantifier:
     Pattern.compile("/\\*(?:[^*]|\\*(?!/))*+\\*/") 
  • Independent group:
     Pattern.compile("/\\*(?>,[^*]|\\*(?!/))*\\*/") 


Spring Contexts by Nikolai Alimenkova
There are 2 Spring contexts:

1. a.xml with bin

 <util:list id="myList"> <value>3</value> <value>4</value> </util:list> 


2. b.xml with bin

 <util:list id="myList"> <value>6</value> </util:list> 


What a code snippet prints:

 System.out.println(new ClassPathXmlApplicationContext("a.xml", "b.xml").getBean("myList")); 


And how can you get him to throw an error without changing the logic of the code?

Answer
Displays 6 , and you can force an error by setting the context to allowBeanDefinitionOverriding to false.


And now the most hardcore task of yesterday, according to the president, the prime minister and the defense minister of an elite club.
OOM from Nikita Salnikov-Tarnovsky
Below are 2 programs. Each of them tries to allocate a total of memory larger than the size of the heap. But one of them throws out java.lang.OutOfMemoryError, and the second does not. Why?

 public class OOM1 { private static final int SIZE = (int) (Runtime.getRuntime().maxMemory() * 0.55); public static void main(String[] args) { { byte[] bytes = new byte[SIZE]; System.out.println(bytes.length); } byte[] bytes1 = new byte[SIZE]; System.out.println(bytes1.length); System.out.println("I allocated memory successfully"); } } public class OOM2 { private static final int SIZE = (int) (Runtime.getRuntime().maxMemory() * 0.35); public static void main(String[] args) { { byte[] bytes = new byte[SIZE]; System.out.println(bytes.length); } byte[] bytes1 = new byte[SIZE]; System.out.println(bytes1.length); byte[] bytes2 = new byte[SIZE]; System.out.println(bytes2.length); System.out.println("I allocated memory successfully"); } } 



Answer
In both cases, javac understands that the variable bytes will not be used after the end of the inner block. Therefore, the second created array, which we put in the variable bytes1, will occupy the same slot as the variable bytes. As a result, after the assignment of bytes1 <- new bytes [SIZE], the value that was in the variable bytes becomes inaccessible and the GC can delete it. Thus, OOM2 requires only 70% of the heap, and not 105%.

Update 1
Here is an interesting analysis from cheremin on the subject of this task.

Update 2
For those who have both options OOM: to play the bug, run your JVM with the option -server

Gruvi from Baruch Sadogursky and Spring from Zhenya Borisov
And here we are waiting for a bummer. jbaruch and EvgenyBorisov asked me to keep the intrigue and not publish the answers to their tasks before the JPoint. They said that everyone who wanted to know the answers came to their reports .

Those who want to know the answers - try to write them in PM. Maybe they take pity on you!


Superblits from Andrei Pangin
Now is the time for true hardcore! Connoisseurs raise their eyes up, and a smiling apangin appears on their screens. He kagbe waves to all connoisseurs and throws thousands of air kisses to them.

Question one
What is wrong with this code? How to fix it?
 public static double[] getRandomVector(int size) { double[] vector = new double[size]; Arrays.parallelSetAll(vector, i -> Math.random()); return vector; } 



Question two
How can it be that a public method works noticeably faster than an identical private method?
 public class Modifiers { static final Inner inner = new Inner(); static class Inner { int x = 1; int getX1() { return x; } int getX2() { return getX1(); } int getX3() { return getX2(); } int getX4() { return getX3(); } int getX5() { return getX4(); } int getX6() { return getX5(); } int getX7() { return getX6(); } int getX8() { return getX7(); } int getX9() { return getX8(); } private int getPrivate() { return getX9(); } public int getPublic() { return getX9(); } } @Benchmark public int testGetPrivate() { return inner.getPrivate(); } @Benchmark public int testGetPublic() { return inner.getPublic(); } } 



Question three
Is it possible to create a class in Java (namely a class, not an interface) without a single constructor, even a private one?


So it goes!

Please write your answers below in the comments under the spoiler . You have 12 hours. The first, who will solve all three tasks, will receive a special prize from Andrey and Odnoklassniki. The remaining solves all three tasks will receive memorable prizes from JUG.ru.

Good luck!

UPDATE:
Defeated lany . His answers can be found below in kamentah! But how it was awarded:

Source: https://habr.com/ru/post/255951/


All Articles