Create class with mathematic operations [duplicate] - java

This question already has answers here:
"+" operator for Java-classes
(5 answers)
Closed 5 years ago.
I'm trying to make a java library with a bunch of extra classes, and I was adding one for Imaginary numbers. Is there any way in java to make a custom class that is affected by mathematics operations
for example
Imaginary(10) * Imaginary(50) = "500i"

No, it is not possible to define custom arithmetic operators that overload default operators, but you can create methods like Imaginary.mutiply(Imaginary i).

You cannot overload operators in Java. See this previous answer to a similar question: https://stackoverflow.com/a/5883909/1701316
Your class will need to implement its operations as methods. If you'd like, since any character is allowed in a method name, you can name them with the typical operators, but they'll still need to be called with dot-notation: Imaginary(10).*(Imaginary(50))

Related

Java 8 method references for multiple statements in streams [duplicate]

This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?

Confusion of Operator Overloading in java [duplicate]

This question already has answers here:
Operator overloading in Java
(10 answers)
How does the String class override the + operator?
(7 answers)
Closed 6 years ago.
Everywhere on the internet i found that java doesn't support Operator overloading but i am confused somehow.Because if that is so then how is the "+" operator able to add both constants and strings?
Any explanation would be appreciated
A language is said to support operator overloading when you can overload operators, i.e. make them do something that is not built into the language. Not when the langauge uses the same operator for two different things.
You can use these operators because it was implemented in the language how they behave. But there is no support for operator overloading in java.
You can create methods such as add(Object o) which is basically the same as what operators do except that it doesn't look as good.
Some languages, such as C#, allow you to overload the operators. In other words, you can define what is meant by == or ++, etc. This can be very useful in scenarios where you wish to use == to test for equality of objects instead of using a .equals() method, for instance.
Here's a tutorial for C#, which shows you how to do it with a + operator:
http://www.tutorialspoint.com/csharp/csharp_operator_overloading.htm
Or this:
https://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx
And you can't do this in Java at present.

Define += for custom classes in Java [duplicate]

This question already has answers here:
Operator overloading in Java
(10 answers)
Closed 7 years ago.
I am making a simple 2D physics engine as my first attempt at making any kind of physics engine. Unfortunately for anybody who's a fan of teaching physics, this is not a physics related question. I simply wanted to know if there was a way to define something simple like addition for a custom class. For example, I have created a class named Vector2D. If I have a velocity vector, and an acceleration vector, it would be easiest to simply have the following code:
Vector2D velocity = new Vector2D(xAxisVelocity, yAxisVelocity);
Vector2D acceleration = new Vector2D(xAxisAcceleration, yAxisAcceleration);
void update() {
velocity += acceleration;
}
However, since velocity and acceleration are not primitive types, so I cannot just add them together. From what I know right now, I would have to add their components together like so:
velocity.x += acceleration.x
..and so on..
What I would like to know is: Is there a way to define addition for classes, similar to how toString() can be overridden?
Just to clear it up, it isn't that big of a deal for me to make a method for adding the two vectors together, I just want to know if overriding is possible.
No, there's no operator overloading in Java. It's a design choice and that's what we have to live with.
See Why doesn't Java offer operator overloading for more discussion.
No — there's no user-defined operator overloading in Java. (This is intentional; the language designers felt that this feature of C++ caused too many problems.)

Why isn't .length() a method for arrays in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java - Array’s length property
String.length() vs Array.length
I'm currently in my AP Computer Science class in high school and I came across this in my reading.
From what I understand, .length() is a method used for strings, but why isn't .length() a method when applied on arrays? I understand that they're different objects, but why didn't Java just make another method for finding the length of arrays?
I appreciate any response I get. Thanks!
Since arrays are fixed length defined at the time they are instantiated length is a public final field on the class. There is no need to make it a method since there is no calculation to be done at run time.
See this section of the Java Spec for details:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7
Now, as for the design question of why they didn't provide an accessor method to obtain the value isn't specified. Perhaps this was done before any other convention was set and this is just a legacy thing. Only the language designers would know the "why" portion of their decision to do it this way.
Arrays are defined in the Java Language Specification #10.7. In particular:
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
[...]
I can't answer why this approach was chosen by the language designers.
Interestingly, it was already the case in the Oak specifications, which is the ancestor of Java.
I doubt that there's a good technical reason for this.
I suspect that this is one of those little inconsistencies that didn't get spotted early enough to get fixed without breaking a ton of code.

String.length() vs Array.length [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is String.length() a method?
Java - Array's length property
Was there a specific design/performance reason as to why String has a method for length but Array has a variable length?
There is no Array class in Java (other than in reflections). Arrays are "primitives" of a sort in Java and play by different rules from declared classes.
Certainly a length() method could have been defined on arrays, but the designers wanted to keep length as a property rather than a pseudo-method. (In part this may have made it easier for early Java implementations.) The reasons are somewhat buried in history.
(A better question is why Java couldn't decide whether to call the concept "length", "count", or "size" -- I always end up trying all three before I hit on the right one for an aggregating class.)

Categories

Resources