Hot answers tagged

154

LibGDX is a framework mostly used for game development. In game development you usually have to do a whole lot of number crunching in real-time and any performance you can get matters. That's why game developers usually use float whenever float precision is good enough. The size of the FPU registers in the CPU is not the only thing you need to consider in ...


68

UPDATE: I've revised this answer. A number of good points were raised in the comments that deserved calling out. If my class implements an interface then can I say that I'm following inheritance? It is not entirely clear what you mean by "following inheritance". Let's ask a slightly different question? What is inheritance? When members of one ...


53

Floats use half as much memory as doubles. They may have less precision than doubles, but many applications don't require precision. They have a larger range than any similarly-sized fixed point format. Therefore, they fill a niche that needs wide ranges of numbers but does not need high precision, and where memory usage is important. I've used them for ...


43

Backwards Compatibility This is the number one reason for keeping behavior in an already existing language/library/ISA/etc. Consider what would happen if they took floats out of Java. Libgdx (and thousands of other libraries and programs) wouldn't work. It's going to take a lot of effort to get everything updated, quite possibly years for many projects ...


32

Atomic operations In addition to what others have already said, a Java-specific disadvantage of double (and long) is that assignments to 64-bit primitive types are not guaranteed to be atomic. From the Java Language Specification, Java SE 8 Edition, page 660 (emphasis added): 17.7 Non-atomic Treatment of double and long For the purposes of the ...


22

Inheritance means writing a new subclass for a superclass. Writing a new class against an interface is implementing that interface. (And writing a new interface based on an old one is extending that interface.) The only correct term that applies to all three possibilities is subtyping. Not every subtype is a subclass.


14

No, there isn't. Slashes work everywhere, backslashes work only on Windows and are a pain to type and read. They are used only by people who mistakenly think they have to use them. But of course, you should really be using the Paths API (documentation for JDK8, JDK9) and never write explicit path separators anyway.


6

I would say that Marker interfaces are a code smell in themselves. I'm not convinced they were ever a good idea but annotations definitely make them obsolete.


4

The point in initializing them this way is that it makes it clear to the reader where the values come from. I can look at the code and see that it is creating bitmasks for 64 bit values with parts X,Y,Z where the X and Z values must hold at least 30,000,000 and that is using the remaining space for the Y values. Imagine the code looked like this: private ...


4

It seems other answers missed one important point: The SIMD architectures can process less/more data depending if they operate on double or float structs (for example, eight float values at a time, or four double values at a time). Performance considerations summary float may be faster on certain CPUs (for example, certain mobile devices). float uses less ...


2

Whilst it's admirable to check your preconditions, I wonder: is your class usage such that you'll need to check all these ? e.g. I will check preconditions for a set of components that are exposed for more general usage, but for more limited usage in which I know how the component will be used, I won't perform so many checks. The counter argument to this ...


2

Using a thread local variable. I could store the IP address in the Controller into a thread local variable and access it in the EventService. I'm leaning towards this one, but I don't know. I've used this approach before, for storing contextual information about requests. I would perhaps expand from the above to store a thread-local request context ...


2

Reflection seems a very fragile and non-intuitive way to implement this. Instead I think you should be telling the objects what to do, and they will collaborate between themselves to determine this. e.g. restaurant.switchToSpringMenu(); and the Restaurant object could swap between menu instances that it has. Exact requirement is that I will be getting ...


2

You could have a single observable interface that allows subscribing by property name or even using regex. Using a map instead of a list to hold observers should work. You talk about observing 'variables' which may not mean much but you should be careful to avoid think in terms of concrete implementation details when designing interactions between classes. ...


2

It won't work with double or float because as you noticed you can't create a generic for primitive types. But it will work with the boxed types Double and Float. Due to autoboxing this won't make much of a difference in practice. That means you can create a generic which can be used with anything that extends Number with public class PhysicsVector<T ...


1

@Philipp's answer is right on for your direct question but let's step back to revisit assumptions. First assumption: You're using Java 8 or earlier. (A future version will support Generics over Primitive Types.) Second, why use generics here? Potentially to save space if you have a very large number of vectors in memory, but boxing will take more space per ...


1

Yes, there is a better solution. The root of your problem appear to be that you try to handle the exception at the wrong place, where you don't have the required information any more to roll back some actions that were taken. If creating your Container object consists of multiple steps that can fail and that have externally visible or persistent effects ...


1

In UML, aggregation and composition are variants of the association. Association is a "has a" relationship. Aggregation is a "part of" relationship. Composition is a stronger form of Aggregation, where the parts cannot exist without the whole. Since you know that a Cover has a Table, you have at least an association. However, can a Table exist without a ...


1

This is a very bad idea. You shouldn't be doing this. If you really want to do this, stop using java -- it's totally inappropriate for the task at hand. You seem to not understand OO -- if you did you wouldn't be asking this question. Java, despite its common misuses, is an OO language and should be treated as such. The first thing you should try is ...


1

Validation is making sure that something is in the state you expect it to be. For example, the User enters "a Date". Of course, users can't actually enter "a Date"; they can only enter the character representation of "a Date", in a format "defined" defined by their national/geographical location and/or personal preference (yes; Users can change their ...


1

Sure, you can have a parser to parse the message of an exception, pass this message to the ExceptionHandler class and do some logic on it. But even better would be to rethink your design. You are most likely forced to handle the message because your exception tree is incorrectly designed. You want the exception classes to represent the exceptional states, ...


1

With subclasses, you inherit state of the superclass (all instance variables, whether you see them or not) inherit actual implementations (all non-abstract methods) With interfaces, you fullfill a contract by implementing the declared methods. That's the classical way to look at it. Now with Java 8 the interfaces become a mixture: you still don't ...


1

I think you should be able to get/store these information in the details of the Spring Security Authentication (see org.springframework.security.core.Authentication#getDetails()). You should be able to access it through the SecurityContextHolder. If you want to add further information to the context you might want look at ...


1

I'd suggest moving all your logging out of the presentation layer and move it into the service layer. That way, if another application wants to create entities, you don't lose the logging information. You could have a createNewEntity() method that simply logs the creation event and passes back an empty entity, keep your save() method, and also handle the ...


1

So far, I think you are doing well. Look it from this point of view. Unchecked Exceptions - Controversy. If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception. May be is a inherited handicap. Spring Rest Client ...


1

In my humble opinion, the controller should be the "facade" itself, that means that the controller should decide which service to call, and the services should be in charge of generating the response object. What I would do is to define a method per action, and discriminate the service using REST naming, something like this: @Path("/") public class ...



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