Java – what is operator overloading, which is different from polymorphism?

I program in Java and have been trying to understand exactly what operator overloading is I'm still a little confused

Operator can have different meanings according to which class it can be used? I've read that it's "name polymer"

Java obviously does not support it, and there seems to be a lot of controversy around this point Should I worry?

As the last question, the teacher declared that the operation uses operator overloading in the operation. He is mainly a C programmer, but we can write the operation in Java Since Java does not support overloading, is there anything I should be vigilant about?

Solution

Operator overloading basically means using the same operator for different data types Therefore, different but similar behaviors are obtained

Java does not support this situation, but any situation like this is very useful. You can easily solve it in Java

The only overloaded operator in Java is the arithmetic operator When used with numbers (int, long, double, etc.), it adds them as you expect When used with string objects, it connects them For example:

String a = "This is ";
String b = " a String";
String c = a + b;
System.out.print (c);

This will print the following on the screen: This is a string

This is the only case in Java where operator overloading can be discussed

About your task: if the requirement is to do something involving operator overloading, you can't do it in Java Ask your teacher what language you can use for this specific assignment You probably need to do this in C

PS: if it is integer, long, double and other objects, it will also work because of unpacking

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>