Using Generics in Java and Numeric Types

Hi, I just launched Java for a course that I will be taking soon (yes, I'm a nerd, I study before I start this class). I wanted to combine several concepts to find out how everything works in Java.

Now what I wanted to try to do was a simple Shape class (Forme in french), which has a minimum of 1-2 classes inheriting from it and makes several instances inside the collection object. I also wanted to make these classes generic in order to try this. This is the part that I'm having problems with.

I am very used to C ++, so this may be a confusing problem, but it seems that I cannot use generics so that, for example, I can indicate that the measurements will be, say, integer or double or Float or whatever. Is it possible?

The GetArea () method below cannot compile due to base and hauteur .

 package testconcepts; import java.util.*; import java.lang.*; abstract class Forme <T extends Number> { protected String nom_forme; String NomForme() { return nom_forme; } abstract Double GetArea(); } class Triangle <T extends Number> extends Forme { protected T base, hauteur; Triangle() { this.nom_forme = "TRIANGLE"; } @Override Double GetArea() { return base * hauteur / 2; } T GetBase() { return base; } T GetHauteur() { return hauteur; } void SetBase(T base) { this.base = base; } void SetHauteur(T hauteur) { this.hauteur = hauteur; } } public class TestConceptsJava { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(new Triangle<Double>()); } } 

Please note that I also cannot assign 0 in the constructor due to type problems.

In addition, I know that there are very similar topics, but I have not yet found a solution to my problem in these other issues.

TL; DR How can I create generics for working with primitives of a primitive type (Double, Integer ..)?

+4
source share
5 answers

In GetArea (), you call the * operator on two classes. What is the point of saying something like:

 Object o1 = //whatever; Object o2 = //whatever; return o1 * o2; 

Is not it? You cannot multiply two classes. What you really want to do is multiply the base value of these wrapper classes (which is implicitly done for you in Java). Overwriting GetArea () like this should solve your problem:

 Double GetArea() { return base.doubleValue() * hauteur.doubleValue() / 2; } 

Note that you will also have to use the base value everywhere you need in this class.

+3
source

So your problem is on this line, I think:

 Double GetArea() { return base * hauteur / 2; } 

The reason you get the error message is because the * undefined operator for objects of type Number . Since you know that you will be dealing with int and double (I suppose), you can try the following:

 Double GetArea() { return base.doubleValue() * hauteur.doubleValue() / 2; } 

As a note, we usually start the method names with a lowercase letter, i.e. getArea() .

+6
source

Going to Double GetArea() { return base.doubleValue() * hauteur.doubleValue() / 2; } Double GetArea() { return base.doubleValue() * hauteur.doubleValue() / 2; } will compile the code, but this is a bad idea in Java, since generics in java are not for that. They simply basically provide static type security when using object collections.

Java does not have operator overloading, so you cannot expect * to work on anything but primitive types.

I would advise reading the basic tutorial on Java generation to understand what they are for in Java: http://docs.oracle.com/javase/1.5.0/docs/guide/language/generics.html

+1
source

Take a look at this part of your calculations:

 base * hauteur 

Since both base and hauteur are declared using a wrapper class, you cannot use the * operator, as you can, with primitive types. Instead, you need to explicitly get the value:

 base.doubleValue() * hauteur.doubleValue() 

Note that this will not be a problem if base or hauteur declared using a primitive type. Then the Java compiler will be able to use automatic decompression to convert the other to the appropriate type and perform any implicit conversions.

+1
source

What you are looking for is called "auto (un) boxing." Unfortunately, operations with T must be valid for all Number subtypes, and β€œauto- (un) boxing” is not such an operation (for example, it will not work with BigInteger ).

+1
source

Source: https://habr.com/ru/post/1438088/


All Articles