Tag: java

54 votes
15 answers
2.8K views
Why does this code using random strings print "hello world"?

The following print statement would print "hello world". Could anyone explain this? System.out.println(random

avatar
Carmelina
0 1 0 9
asked 07 September, 2021
63 votes
30 answers
2.2K views
How to generate a random alpha-numeric string

I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my si

avatar
Arline
0 1 0 9
asked 07 September, 2021
66 votes
25 answers
568 views
Does Java support default parameter values?

I came across some Java code that had the following structure: public MyParameterizedFunction(String param1,

avatar
Keila
0 1 0 9
asked 07 September, 2021
57 votes
27 answers
1.4K views
Java inner class and static nested class

What is the main difference between an inner class and a static nested class in Java? Does design / implementa

avatar
Stacia
0 1 0 9
asked 07 September, 2021
49 votes
14 answers
1.7K views
Comparing Java enum members: == or equals()?

I know that Java enums are compiled to classes with private constructors and a bunch of public static members.

avatar
Margo
0 1 0 9
asked 07 September, 2021
49 votes
30 answers
2.8K views
How do I break out of nested loops in Java?

I've got a nested loop construct like this: for (Type type : types) { for (Type t : types2) {

avatar
Don
0 1 0 9
asked 07 September, 2021
66 votes
11 answers
912 views
How to use java.net.URLConnection to fire and handle HTTP requests

Use of java.net.URLConnection is asked about pretty often here, and the Oracle tutorial is too concise about i

avatar
Vi
0 1 0 9
asked 07 September, 2021
62 votes
23 answers
412 views
What is a JavaBean exactly?

I understood, I think, that a "Bean" is a Java class with properties and getters/setters. As much as I underst

avatar
Serina
0 1 0 9
asked 07 September, 2021
63 votes
30 answers
1.7K views
How do you assert that a certain exception is thrown in JUnit 4 tests?

How can I use JUnit4 idiomatically to test that some code throws an exception? While I can certainly do somet

avatar
Hallie
0 1 0 9
asked 07 September, 2021
59 votes
30 answers
1.1K views
What's the simplest way to print a Java array?

In Java, arrays don't override toString(), so if you try to print one directly, you get the className + &

avatar
Jeff
0 1 0 9
asked 07 September, 2021
48 votes
30 answers
2.7K views
How to get an enum value from a string value in Java

Say I have an enum which is just public enum Blah { A, B, C, D } and I would like to find the enum value

avatar
Vi
0 1 0 9
asked 07 September, 2021
57 votes
30 answers
1.8K views
"implements Runnable" vs "extends Thread" in Java

From what time I've spent with threads in Java, I've found these two ways to write threads: With imp

avatar
Thurman
0 1 0 9
asked 07 September, 2021
65 votes
28 answers
2.1K views
How do I declare and initialize an array in Java?

How do I declare and initialize an array in Java?

avatar
Wilfredo
0 1 0 9
asked 07 September, 2021
54 votes
29 answers
2.6K views
What's the difference between @Component, @Repository & @Service annotations in Spring?

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any

avatar
Tana
0 1 0 9
asked 07 September, 2021
53 votes
22 answers
1.2K views
What is reflection and why is it useful?

What is reflection, and why is it useful? I'm particularly interested in Java, but I assume the principle

avatar
Shanita
0 1 0 9
asked 07 September, 2021
54 votes
23 answers
2.1K views
How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? An

avatar
Yolando
0 1 0 9
asked 07 September, 2021
54 votes
29 answers
2.9K views
How do I determine whether an array contains a particular value in Java?

I have a String[] with values like so: public static final String[] VALUES = new String[] {"AB","BC","CD","AE

avatar
Suzan
0 1 0 9
asked 07 September, 2021
47 votes
30 answers
759 views
Does a finally block always get executed in Java?

Considering this code, can I be absolutely sure that the finally block always executes, no matter what somethi

avatar
Rudy
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
1.9K views
How can I fix 'android.os.NetworkOnMainThreadException'?

I got an error while running my Android project for RssReader. Code: URL url = new URL(urlToRssFeed); SAXPa

avatar
Rudy
0 1 0 9
asked 07 September, 2021
53 votes
30 answers
890 views
How can I create an executable JAR with dependencies using Maven?

I want to package my project in a single executable JAR for distribution. How can I make a Maven project pack

avatar
Xavier
0 1 0 9
asked 07 September, 2021