Hot answers tagged

9

Suppressing warnings is something that needs to be done with extreme care. A warning means: The compiler found something that looks dodgy. It doesn't mean it is dodgy, it just looks like it to the compiler. Sometimes you have code that is perfectly fine and gives a warning. Sometimes you fix it by slightly modifying your code. Sometimes the compiler has ...


7

To me, the entire point of suppressing warnings is to maintain a "clean bill of health" for your project. If you know that your entire code base compiles cleanly, it's immediately obvious when someone does something wrong that causes the first warning to appear in the issues list. You can then fix the error or suppress it if you can prove that it's spurious. ...


5

Factory methods allow simplifying the constructors - the difference which instance in time is represented by the Object can be separated from the calculation of the instant. There are lots of factory methods, some doing String parsing, some converting from some other date or time representation, but all that can happen before the Object has to be created (...


5

It's not always necessary to create a new object when obtaining a value of an immutable type. A factory method may return an object that has already been created while a constructor call always creates a new object. There are some other advantages to factory methods, but they're not as closely related to immutability and the Date-Time API. For instance, ...


4

This: new Runnable(){ public void run(){ ...statements } } (in Java) is called an anonymous class. You're creating a new class definition + instance deriving from the Runnable interface, and providing the run() method. It's anonymous, since it's not named, and you can't re-use that definition elsewhere.


3

It seems like a batcher. So you could name it like this. public class HeavyWorkBatch { ArrayList<File> myList = new ArrayList<>(); public void AddToBatch(File f) { myList.add(f); } public void DoHeavyWork() { for (File f: myList) { /// ... } } } // Note there is a conspicuous lack of ...


3

C++ doesn't have a manual approach to memory management at all. If you're calling free in C++, you're using it wrong. That's the C way that we're proud to be disowning. Really, the predominant models are non-deterministic (GC), deterministic (C++, D), and manual (C).


3

Use database connection pooling. I don't recommend writing your own. Here's an explanation on StackOverflow. You can use c3p0 or read up more on your own by searching for "connection pooling."


3

What is the role of “throws exeption” It's used for checked exceptions. The idea is, if you write code to call a function that throws FoobarException, then the compiler will force you to either handle the exception, or to add the same throws FoobarException declaration to the function that you are writing. It's a way to make sure that nobody will forget ...


3

You should return a 404. You can do it by throwing a NotFoundException (https://jersey.java.net/apidocs/2.6/jersey/javax/ws/rs/NotFoundException.html). Also please look at this SO question if you need to control the returned content type http://stackoverflow.com/questions/23858488/how-i-return-http-404-json-xml-response-in-jax-rs-jersey-on-tomcat


3

HTTP 204 means that something was found, but it's empty. For instance, imagine that you're serving log files through HTTP, with the requests such as http://example.com/logs/[date-goes-here]. On May 18th, 2015: http://example.com/logs/2015-05-19 would return HTTP 404, which means that there are no logs, because, well, it's difficult to log the future. http:/...


2

Something that I don't think has been mentioned here is that there are efficiencies that come from garbage collection. In the most commonly used Java collectors, the main place that objects are allocated is an area reserved for a copying collector. When things start, this space is empty. As objects are created, they are allocated next to each other in the ...


2

I didn't design or write Java's Date-Time API, so I can't definitively say "why did they do it this way." I can however suggest two key reasons they should do it this way: Expressive power Parallel form First, consider the context: Java's dates and time code is a large, complex package dealing with a very tricky subject matter. As common as "time" is, ...


2

I'd suggest Threads. Threads are "lighter" than full-blown Processes and Java has far better tools for managing the interactions with/ between Threads than it has for Processes. BTW, if you were to use the script as you described it, your processes would run sequentially, one after the other. You'd need some way of telling your operating system to start ...


1

You can handle exception explicitly in controller method where possible by using @ResponseStatus, @ExceptionHandler, @ControllerAdvice annotation. For more detail please refer : Exception Handling in spring MVC.


1

I believe a multithreaded approach would be the best one. Also have you thought of using a mix of the two? Launch an application (manager) responsible for managing which files to process and their progress, to process each file you'd then launch the generic processing application in a separate process, a little bit like how browsers work today. This way ...


1

The proper HTTP error code on input is 400: Bad Request. In the response you could go with 500. If there is an error in marshalling or unmarshalling, an exception will be thrown which you can handle by registering an ExceptionMapper (scoll down to the Exception Mapping section). You can then determine what kind of error to throw. The JAX-RS package has a ...


1

It doesn't say much in the Javadocs about how this is implemented in the compiler, nor does it for the equivalent NotImplementedException in C#. However, it wouldn't surprise me if the compiler's static code analysis went something like this: an exception is thrown unconditionally before return is called, so there's no need to check the return type. When ...


1

Using CDI injection and JPA together isn't coupling, it's just what they used for dependency injection, AoP, and persistence (JPA). JSF (JavaServer Faces) is a Java specification for building component-based user interfaces for web applications. jax-rs just lets you support REST as a service. websocket is old school TCP in both directions. You've ...


1

Assumed you need a generic solution, where the program is not tied to a specific column structure at run time, you can use an ArrayList of rows, where each row is a string array String[]. You will need an additional HashTable<String,int> to store the mapping of the column name to the column index (a HashMap will probably work, either). So if you have ...


1

The big difference that garbage collection makes isn't that you don't have to explicitly delete objects. The much bigger difference is that you don't have to copy objects. This has effects that become pervasive in designing programs and interfaces in general. Let me give just one tiny example to show how far-reaching this is. In Java, when you pop ...


1

As fas as non-static initializer blocks are concerned, their bare function is to act as a default constructor in anonymous classes. That is basically their only right to exist.



Only top voted, non community-wiki answers of a minimum length are eligible