Tag: r

60 votes
30 answers
1.3K views
Why use getters and setters/accessors?

What's the advantage of using getters and setters - that only get and set - instead of simply using publi

avatar
Shantay
0 1 0 9
asked 07 September, 2021
64 votes
30 answers
836 views
How can I avoid Java code in JSP files, using JSP 2?

I'm new to Java EE and I know that something like the following three lines <%= x+1 %> <%= reque

avatar
Solomon
0 1 0 9
asked 07 September, 2021
51 votes
30 answers
705 views
Sort a Map<Key, Value> by values

I am relatively new to Java, and often find that I need to sort a Map<Key, Value> on the values. Since

avatar
Adolfo
0 1 0 9
asked 07 September, 2021
51 votes
30 answers
1.1K views
How to split a string in Java

I have a string, "004-034556", that I want to split into two strings: string1="004"; string2="034556"; That

avatar
Brianne
0 1 0 9
asked 07 September, 2021
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
567 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
66 votes
11 answers
911 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
411 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&apos;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
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&apos;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
757 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 &apos;android.os.NetworkOnMainThreadException&apos;?

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