Tag Info

New answers tagged

0

No, (1) classes don't automatically make an object-oriented language nor does (2) lack of them make a language non-OO. An example of (1) is Java: Java has classes, but that doesn't make Java OO; in fact, classes in Java are used for defining Abstract Data Types, in order to define objects, you need interfaces. It's the interfaces that make Java OO, not the ...


3

On the one hand I can argue this does not violate SRP, because the whole use is database communication. You could also say you have a single class in your application, because the whole class does processing of data. It doesn't work like this. If your class has: a mix of different abstraction levels; a mix of things that can be described as separated ...


3

Yes, I think you are violating the SRP. Not because you kept all of your database code in one class, but because you have database code for completely different entities (users, products, events, friends) all in one class. At the very least, separate your code into classes by the entity it's most closely related to. If you're looking for design patterns, you ...


0

I understand your doubts, because such service-mixins don't look natural for me either. But why? Strictly speaking, even in complete SOA architecture service composition is not forbidden. But your case is different. Your services are not independent units located in own processes with independent transaction management. Your services are just level in your ...


2

The fundamental problem is that you need to have an entry point - the earliest of your code that will run once the program is started. That must be something allowed by the language of choice. Non-member functions are allowed in C and C++ but not in Java. However static member functions are very close to non-member functions (you can simply call a public ...


0

The explicit constructor is a so-called copy constructor. It's intended for the rare case where you need to make sure that you receive a new String instance instead of a pooled one. The newly created string still is immutable, as you can't modify the passed in string reference.


1

The constructor public String(String original) is a copy constructor. Its sole purpose is to make a copy of a String object. Since String is immutable, making copies is rarely needed. That has been covered in the Javadoc of that constructor. However, since String is a core class and it is used everywhere, there could be some special cases requiring copying ...


3

Java strings have to be immutable because Java's security model depends on the value of them not changing between them being inspected by a SecurityManager instance and consumed by whatever part of the runtime is being protected by the SecurityManager. For example, if you have a SecurityManager on your thread and you execute: String str = new String ...


2

Immutability for strings has a lot of advantages even with an explicit constructor (I am sure you know them already: cheap copies, no side-effects, value semantics). The explicit constructor is exactly what everyone expects to be available, without it we would always have to write something ugly like String s = (new StringBuilder("abc").toString()); The ...


7

Of course it makes sense. When you get the value you should not care how it was constructed. It would make no sense if immutability would be dependent of the way an object was constructed: "abc" immutable, new String("def") mutable. It would create a mess.


1

Definitive source for Java As the comments already indicated, there is but one official specification for the Java language, namely the Java Language Specification made available from Oracle. The answer to your particular question of the calling behavior is found in section 8.4.1: When the method or constructor is invoked (§15.12), the values of the ...


1

They are immutable so they behave like values. You can pass reference to the string and don't have to fear that whatever code you passed it to might change it.


6

Lets consider the situation where you have the provided code of: public void delete() throws IOException, SQLException { // Non-Compliant /* ... */ } The danger here is that the code that you write to call delete() will look like: try { foo.delete() } catch (Exception e) { /* ... */ } This is bad too. And it will be caught with another rule ...


0

Your interface should document how it handles bogus input, and can declare Exceptions it will throw. In this case, a simple @throws IllegalArgumentException would do the trick.


0

there are various ways: rewrite the recursion into an iterative version and you don't have to worry about recursion depth only the stack depth if the iterative version requires one. Use an outside variable of some kind (static variable, field member,...) to track the recursion depth. This has the downside that it takes up some bytes just for this method ...


4

Yes, there is another way of limiting recursion depth, but I would not encourage it. In general, we can say that you are currently using a data-driven approach. public void myMethod(File arg, int accumulator) { where the accumulator counts the recursion call depth. The other way is to enumerate the calls. Said another way, hard-code the calls: private ...


2

I remember fiddling around with this a little bit when playing with Java a while back, but I wasn't real conscious of the distinction between checked and unchecked until I read your question. I found this article on Google pretty quickly, and it goes into some of the apparent controversy: ...


0

The comments on Doval's answer are right: create and use a PositiveDouble. Also, consider using a design by contract tool. A design by contract tool like cofoja might solve your problem. I have not used 1 but I know that there are several design by contract tools in Java.


2

We have a cluster-environment too. We use Hazelcast for such jobs. With Hazelcast you could embed the codeblock for updating within a "Hazelcast-Lock-Section". It is not my favourite solution, but this is how it is done in our application (and maybe suits hazelcast your needs). I opt for a smaller and easier solution: I would write a small (buzzword-alarm: ...


1

Using a messaging broker seems like the best tool to inform other applications that they should update rather than polling (which is fine really too). http://www.rabbitmq.com/ Locking the database table for writing is what you want to prevent race conditions.


0

If the Key object is already unique itself, not by equals() but by instance, and in case the key would actually never be null (and the code could be changed / the null key case could be removed), you could change the get() function to: static C get(Key key) { synchronized (key) { C c = cs.get(key); if (c==null) { c = new ...


1

If the only problem with initializing multiple copies of a single object value is the time/memory it takes, you may want to consider switching to a ConcurrentHashMap and using the computeIfAbsent method to initialize values. This has the side effect that two copies of a single value may be initialized, however only one will be retained (the second will be ...


1

This is a variation of the singleton pattern, called Multiton. You will have to manage the global state introduced with the HashMap. This can lead to diffucult unit testing. The implementation of the synchronized method looks the same, as shown in the Wikipedia article. But if you can restructure your use case, so that you can avoid the synchronizing of ...


1

In his book Effective Java, Josuha Bloch mentions this pattern as a good practice for this sort of scenario (Item 1: Consider static factory methods instead of constructors). Some common alternative names for the method that here is called C.getare the following C.getInstance, C.valueOf, C.of.


2

ASCII is a subset of UTF-8. You can read any ASCII-encoded document as UTF-8, and it will work. ASCII only uses 7 bits, and UTF-8 uses the unused eight bit to mark non-ASCII code units. The XML standard requires the XML declaration <?xml … to be present. If it is absent, you are not dealing with a well-formed XML document and should probably reject it. ...


4

US-ASCII is a 7-bit code, and it's a true subset of UTF-8. In other words, every ASCII file is by definition also an UTF-8 file. The file command classifies it as 'ASCII' because there are no 8-bit characters in it, and it's totally right in doing that. Nevertheless you should always read XML files assuming that they're UTF8-encoded. It doesn't hurt even if ...


2

First I am going to put a disclaimer: I really hope this is a school assignment project because storing account numbers and maintaining funds balance in a desktop application with a text file is unbelievably insecure and prone to error. I vehemently recommend more of a client/server/database design with security built between all layers. With that being ...


1

Here's a twist on Matt's answer, using the builder pattern combined with Java 8's new functional capabilities: Book.java import java.util.*; import java.util.function.Consumer; public class Book { private String title; private List<String> authors; private int year; public Book(String title, List<String> authors, int year) { ...


4

The item field in SuperType2 class is static so there is no inheritance; there is just an ambiguous reference . All fields from interfaces are static and public by default. Java would have been better designed if static fields and methods can only be accessed through class name and not through a reference; but this is not a major flaw. Since version 8, Java ...


-1

I'm not a Java programmer, but I see that you've asked "Why can't we store everything on the stack?" and I don't think anybody answered this, so I'll try to answer it myself. I don't quite remember Java programming, so here's a pseudo code: function func(string a, string b) { int k; string joined = a + b; ... } If we had only the stack, joined ...


0

Languages themselves have limited functionality by themselves. In order to build full software you'll need to learn many frameworks, libraries and tools besides the language itself. Java beat C++ 15-20 years ago in popularity, not because it was necessarily a better language than C++ (actually it's more limited than C++) but because it came with frameworks ...


0

This is almost impossible to answer surely with the information (or lack of) given? The choice of which language to use where is often a complex one that needs to consider what you want to achieve, who you will be working with (if you are working within a team), culture in the client or user's environment, long term code maintenance and management issues, ...


8

Where are primitive fields stored? Primitive fields are stored as part of the object that is instantiated somewhere. The easiest way to think of where this is - is the heap. However, this is not always the case. As described in Java theory and practice: Urban performance legends, revisited: JVMs can use a technique called escape analysis, by which ...


3

Depending on the number of fields, you might find it easier to use the Builder pattern to combine these changes than to create a separate constructor for each possible set of changes. It does requiring one object that is not going to be used (returned), but you can use one builder instance for as many books as you need to change. public final class Book { ...


2

One useful pattern is to define a public interface which can report all of the properties associated with the class, as well as a package-private interface which extends it with a few additional members. A package-private version of the class should be mutable, but the public-facing one should be immutable. The public-facing class should hold a reference ...


1

The numbers in the table indicate the number of four connected positions which include that space for example: the 3 in the upper left corner is for one each of horizontal, vertical, and diagonal lines of four which can be made with it. the 4 beside it is for two horizontal (one including starting in the corner, one starting on it, one vertical, and one ...


2

Returning an Input Steam enables your calculation to be performed on an ongoing basis as data is read from it, potentially even in a separate thread. It is equivalent to a unix command line pipe. If the processing takes a significant time to complete, this will effectively hide that processing time from the user by distributing it (or shunting it off to ...


0

Divide and conquer. You need the following: Something to parse command line args. Something to find all files in a directory. Recursive? Something to read text files one line at a time. Something to grep / regex the types of errors. Something to count / sort by errors. Printing. What format? Text, JSON, XML? Split it up into smaller tasks so you ...


0

The idea is to use one class to represent general faults There are no "general" faults. NPE, for example, can be thrown manually, so you can't even tell if it's intentional or not. There are also misused exception classes or something you just don't know about. How would the "wrapping" look in the code? I imagine: try { //... } catch ...


0

One multi-lingual/multi-platform programmer's thoughts ... Are the methods in question in the same class as main? If so, they can manipulate the values regardless of the declared access (public/private....). So I assume your question refers to methods which are defined in some other classes. The performance impact of the various options will be irrelevant ...


6

I don't think this is a good idea. This introduces coupling in a very non-obvious way. Consider a class or a service. It has clearly defined tasks, it's methods are well documented, it can be swapped out for something else that has the same interface/contract. Suppose it encounters an exception. The service then can decide whether it is recoverable or ...


0

The idea is to use one class to represent general faults (NPE, etc.) that can't be handled in place, and my strategy is to catch and log them in a single fault barrier so developers/devOps can troubleshoot them. These exceptions should be wrapped and passed back hiding some implementation details to the users of the API? It sounds to me like you ...


-1

I'd stick with one. Anytime you have an exception that isn't catastrophic, it isn't an exception. Log it to warn or error level then move on. Otherwise, you've just reinvented checked exceptions.


3

In this case, I would have to say create an object that holds them - like a "Data" class, which you can then instantiate. This provides a nice single point of access to the data. But, in this case, it does take up some extra space on the heap. (I'm not sure how concerned you should be with this though, java has automatic garbage collection) Another ...


2

It's because an abstract class isn't complete. One or more parts of its public interface are not implemented. You've said what they're going to be - what they're called, what their name is, what they return - but no implementation has been provided, so nobody would be able to call them. Compilers prevent you from instantiating such incomplete objects on the ...


2

Because an abstract class by design usually has a unimplemented method. What should the program do if someone tried to call it? Having unimplemented methods on an object is usually a bug so the compiler helps you by not allowing them to exist. If you want to throw an error when the method is called then just implement it with just throws new ...


0

Let me answer your question with another question: What would happen when you call any of the abstract methods?


0

I don't get it. It uses several 1.7 features and classes and would be very difficult to get it recompiled in JDK 1.5. Okay. To backport a library may be a big (expensive) task. But on the other hand, you say: The integration itself will be quick because the library has just 1 class and very little methods which need to be called from the ...


0

Why don't you write a bridge? Something that uses shared memory or something similar. Looking at your options, I am not sure, how fast this thing is supposed to work. But that's the only alternative I can think of. Googling tells me this won't be easy either... Unless you use imq or something... Methods to share memory or state across JVMs? still not easy ...


0

In my experience there are relatively few difficulties involved in taking a java 1.5 application and running it on a later vm. Java is generally quite good at maintaining backward compatibility, and the few parts that are not compatible between versions are mostly well separated into the sun.* package hierarchy. I would therefore aim to migrate the legacy ...



Top 50 recent answers are included