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); } }
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.
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
- If this is a Java string, and supposedly a comment in it, it will not work
- If koment ends with two asterisks slash, it will not work
- 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("/\\*(?>,[^*]|\\*(?!/))*\\*/")
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.
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"); } }
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; }
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(); } }
Is it possible to create a class in Java (namely a class, not an interface) without a single constructor, even a private one?
Source: https://habr.com/ru/post/255951/
All Articles