Why is printing “B” dramatically slower than printing “#”?
2021-6-3 anglehua
I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { if(r.nextInt(4) == 0) { ...How can I create an executable JAR with dependencies using Maven?
2021-6-3 anglehua
I want to package my project in a single executable JAR for distribution. How can I make a Maven project package all dependency JARs into my output JAR? <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> ...How to fix 'android.os.NetworkOnMainThreadException'?
2021-6-3 anglehua
I got an error while running my Android project for RssReader. Code: URL url = new URL(urlToRssFeed); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); RssHandler theRSSHandler = new RssHandler(); xmlre...Does a finally block always get executed in Java?
2021-6-3 anglehua
Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is? try { something(); return success; } catch (Exception e) { return failure; } finally { System.out.println("I don't know if this will get printed out...How do I determine whether an array contains a particular value in Java?
2021-6-3 anglehua
I have a String[] with values like so: public static final String[] VALUES = new String[] {"AB","BC","CD","AE"}; Given String s, is there a good way of testing whether VALUES contains s? Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of primitives (see the comm...How do I call one constructor from another in Java?
2021-6-3 anglehua
Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)? Yes, it is possible: public class Foo { private int x; public Foo() { this(1)...What is reflection and why is it useful?
2021-6-3 anglehua
What is reflection, and why is it useful? I'm particularly interested in Java, but I assume the principles are the same in any language. The name reflection is used to describe code which is able to inspect other code in the same system (or itself). For example, say you have an object of an unknown...What's the difference between @Component, @Repository & @Service annotations in Spring?
2021-6-3 anglehua
Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device? In other words, if I have a Service class and I change the annotation from @Service to @Component, will it still behave the same...How do I declare and initialize an array in Java?
2021-6-3 anglehua
How do I declare and initialize an array in Java? You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array). For primitive types: int[] myIntArray = new int[3]; int[] myIntArray = {1,...“implements Runnable” vs “extends Thread” in Java
2021-6-3 anglehua
From what time I've spent with threads in Java, I've found these two ways to write threads: With implements Runnable: public class MyRunnable implements Runnable { public void run() { //Code } } //Started with a "new Thread(new MyRunnable()).start()" call Or, with extends Thread: pu...How to get an enum value from a string value in Java?
2021-6-3 anglehua
Say I have an enum which is just public enum Blah { A, B, C, D } and I would like to find the enum value of a string, for example "A" which would be Blah.A. How would it be possible to do this? Is the Enum.valueOf() the method I need? If so, how would I use this? Yes, Blah.valueOf("A") will gi...How do you assert that a certain exception is thrown in JUnit 4 tests?
2021-6-3 anglehua
How can I use JUnit4 idiomatically to test that some code throws an exception? While I can certainly do something like this: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); } catch (IndexOutOfBoundsException e) { thrown = true;...What's the simplest way to print a Java array?
2021-6-3 anglehua
In Java, arrays don't override toString(), so if you try to print one directly, you get the className + '@' + the hex of the hashCode of the array, as defined by Object.toString(): int[] intArray = new int[] {1, 2, 3, 4, 5}; System.out.println(intArray); // prints something like '[I@3343c8b3' B...How to use java.net.URLConnection to fire and handle HTTP requests?
2021-6-3 anglehua
Use of java.net.URLConnection is asked about pretty often here, and the Oracle tutorial is too concise about it. That tutorial basically only shows how to fire a GET request and read the response. It doesn't explain anywhere how to use it to among others perform a POST request, set request headers,...What is a JavaBean exactly?
2021-6-3 anglehua
I understood, I think, that a "Bean" is a Java class with properties and getters/setters. As much as I understand, it is the equivalent of a C struct. Is that true? Also, is there a real syntactic difference between a bean and a regular class? Is there any special definition or an interface? Basical...How do I break out of nested loops in Java?
2021-6-3 anglehua
I've got a nested loop construct like this: for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // Breaks out of the inner loop } } } Now how can I break out of both loops? I've looked at sim...Comparing Java enum members: == or equals()?
2021-6-3 anglehua
I know that Java enums are compiled to classes with private constructors and a bunch of public static members. When comparing two members of a given enum, I've always used .equals(), e.g. public useEnums(SomeEnum a) { if(a.equals(SomeEnum.SOME_ENUM_VALUE)) { ... } ... } Howe...Java inner class and static nested class
2021-6-3 anglehua
What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these? From the Java Tutorial: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply...How to generate a random alpha-numeric string
2021-6-3 anglehua
I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over 500K+ generation (my needs don't really require anything much more sophisticated). Ideally, I woul...Does Java support default parameter values?
2021-6-3 anglehua
I came across some Java code that had the following structure: public MyParameterizedFunction(String param1, int param2) { this(param1, param2, false); } public MyParameterizedFunction(String param1, int param2, boolean param3) { //use all three parameters here } I know that in C++ I can a...