Why does this code using random strings print “hello world”?

2021-6-3 anglehua

The following print statement would print "hello world". Could anyone explain this? System.out.println(randomString(-229985452) + " " + randomString(-147909649)); And randomString() looks like this: public static String randomString(int i) { Random ran = new Random(i); StringBuilder sb = ne...

阅读全文>>

评论(0) 浏览(85)

How to split a string in Java

2021-6-3 anglehua

I have a string, "004-034556", that I want to split into two strings: string1="004"; string2="034556"; That means the first string will contain the characters before '-', and the second string will contain the characters after '-'. I also want to check if the string has '-' in it. If not, I will th...

阅读全文>>

评论(0) 浏览(117)

Sort a Map<Key, Value> by values

2021-6-3 anglehua

I am relatively new to Java, and often find that I need to sort a Map<Key, Value> on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the ke...

阅读全文>>

评论(0) 浏览(103)

How can I avoid Java code in JSP files, using JSP 2?

2021-6-3 anglehua

I'm new to Java EE and I know that something like the following three lines <%= x+1 %> <%= request.getParameter("name") %> <%! counter++; %> is an old school way of coding and in JSP version 2 there exists a method to avoid Java code in JSP files. What are the alternative JSP 2 li...

阅读全文>>

评论(0) 浏览(112)

Why use getters and setters/accessors?

2021-6-3 anglehua

What's the advantage of using getters and setters - that only get and set - instead of simply using public fields for those variables? If getters and setters are ever doing more than just the simple get/set, I can figure this one out very quickly, but I'm not 100% clear on how: public String foo; i...

阅读全文>>

评论(0) 浏览(94)

Difference between StringBuilder and StringBuffer

2021-6-3 anglehua

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these? StringBuffer is synchronized, StringBuilder is not. StringBuilder is faster than StringBuffer because it's not synchronized. Here's a simple benchmark test: public...

阅读全文>>

评论(0) 浏览(118)

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

2021-6-3 anglehua

I am trying to use Notepad++ as my all-in-one tool edit, run, compile, etc. I have JRE installed, and I have setup my path variable to the .../bin directory. When I run my "Hello world" in Notepad++, I get this message: java.lang.UnsupportedClassVersionError: test_hello_world : Unsupported major.mi...

阅读全文>>

评论(0) 浏览(154)

How do I create a Java string from the contents of a file?

2021-6-3 anglehua

I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited. Is there a better/different way to read a file into a string in Java? private String readFile(String file) throws IOException { BufferedReader reader = new BufferedRead...

阅读全文>>

评论(0) 浏览(147)

How can I convert a stack trace to a string?

2021-6-3 anglehua

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace? One can use the following method to convert an Exception stack trace to String. This class is available in Apache commons-lang which is most common dependent library with many popular...

阅读全文>>

评论(0) 浏览(153)

How does the Java 'for each' loop work?

2021-6-3 anglehua

Consider: List<String> someList = new ArrayList<String>(); // add "monkey", "donkey", "skeleton key" to someList for (String item : someList) { System.out.println(item); } What would the equivalent for loop look like without using the for each syntax? for (Iterator<String> i...

阅读全文>>

评论(0) 浏览(107)

Why does Java have transient fields?

2021-6-3 anglehua

Why does Java have transient fields? The transient keyword in Java is used to indicate that a field should not be part of the serialization (which means saved, like to a file) process. From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields: Variables may be mar...

阅读全文>>

评论(0) 浏览(134)

What does “Could not find or load main class” mean?

2021-6-3 anglehua

A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class ... What does this mean, what causes it, and how should you fix it? The java <class-name> command syntax First of all, you need to understand the ...

阅读全文>>

评论(0) 浏览(138)

Fastest way to determine if an integer's square root is an integer

2021-6-3 anglehua

I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer): I've done it the easy way, by using the built-in Math.sqrt() function, but I'm wondering if there is a way to do it faster by restricting yourself to integer-only domain. Mai...

阅读全文>>

评论(0) 浏览(117)

How can I concatenate two arrays in Java?

2021-6-3 anglehua

I need to concatenate two String arrays in Java. void f(String[] first, String[] second) { String[] both = ??? } What is the easiest way to do this? I found a one-line solution from the good old Apache Commons Lang library. ArrayUtils.addAll(T[], T...) Code: String[] both = ArrayUtils.addAll(f...

阅读全文>>

评论(0) 浏览(104)

What exactly is Apache Camel?

2021-6-3 anglehua

I don't understand what exactly Camel does. If you could give in 101 words an introduction to Camel: What exactly is it? How does it interact with an application written in Java? Is it something that goes together with the server? Is it an independent program? Please explain what Camel is. If...

阅读全文>>

评论(0) 浏览(105)

How do I create a file and write to it?

2021-6-3 anglehua

What's the simplest way to create and write to a (text) file in Java? Note that each of the code samples below may throw IOException. Try/catch/finally blocks have been omitted for brevity. See this tutorial for information about exception handling. Note that each of the code samples below will ove...

阅读全文>>

评论(0) 浏览(99)

Is null check needed before calling instanceof?

2021-6-3 anglehua

Will null instanceof SomeClass return false or throw a NullPointerException? No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null. From the Java Language Specification, section 15.20.2, "Type comparison operator instanceof": "At run ...

阅读全文>>

评论(0) 浏览(138)

Why is executing Java code in comments with certain Unicode characters allowed?

2021-6-3 anglehua

The following code produces the output "Hello World!" (no really, try it). public static void main(String... args) { // The comment below is not a typo. // \u000d System.out.println("Hello World!"); } The reason for this is that the Java compiler parses the Unicode character \u000d as a new ...

阅读全文>>

评论(0) 浏览(115)

How to round a number to n decimal places in Java

2021-6-3 anglehua

What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most situations. I also would like only significant digi...

阅读全文>>

评论(0) 浏览(98)

How to add local jar files to a Maven project?

2021-6-3 anglehua

How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources? Install the JAR into your local Maven repository as follows: mvn install:install-file \ -Dfile=<path-to-file> \ -DgroupId=<group-id> \ -DartifactId=<artifact-id> ...

阅读全文>>

评论(0) 浏览(135)

Powered by emlog 京ICP备15036472号-3 sitemap