Page 1 of 1

Java what is actually happening here?

Posted: Sat Sep 20, 2014 6:47 pm
by Chris
How does the computer compute this statement?

Code: Select all

char b = 'a';
b++;
System.out.println(b);
Can you change what different operators do against an object?

Re: Java what is actually happening here?

Posted: Sat Sep 20, 2014 8:26 pm
by vitinho444
They taught me this at college omg!!!

So what you say is: b = 'a';
but what java gets is b = 59; // i believe thats the ASCII code for a

so by doing b++ you are saying b = 59+1 = 60, turning the 'a' into a 'b' because its the following letter in the ascii table :D

If you can change this? I don't think so..

Re: Java what is actually happening here?

Posted: Sat Sep 20, 2014 9:02 pm
by KyleMassacre
I don't know java really at all but is there a way to parse ASCII chars as text?

Re: Java what is actually happening here?

Posted: Sun Sep 21, 2014 7:33 am
by a_bertrand
Indeed a char is nothing else as a number for the computer. Increasing the value of that number will actually display the next character on the screen when you print it.

In Java you can sadly not overload the operators therefore you can decide what the +, - or whatever else operator do with your classes. Is it stupid? Yes and one of the biggest thing I have against that language (as well as the fact not all is derived from object).

Re: Java what is actually happening here?

Posted: Sun Sep 21, 2014 8:50 am
by vitinho444
Alain are there languages where you can decide what Object++; does? Why is that good I ask?

Re: Java what is actually happening here?

Posted: Sun Sep 21, 2014 1:59 pm
by a_bertrand
C++ is one, and C# another, now I don't know the full list of languages supporting it, but I would say it's not all that odd.

Why is it useful? Well, let's take the classic example:

I create a class matrix ( http://en.wikipedia.org/wiki/Matrix_%28mathematics%29 ) which let me do matrix operations (basically what you need to do when you do 3D games) like matrixA * matrixB, then just coding it like I just said it would make the most sense. Why a language should prevent me to code in a NATURAL way?

Another example is the == (equal) compare operator. Do you compare the ADDRESS of the 2 classes or would it make sense if you could compare the content of it? Well I would say you compare more frequently the content than the memory address of your objects (and this is a frequent source of bugs for new comers in Java).

Another example is the events in C#, events are basically lists of call backs where I can say that I'm interested to be called for example when a user clicks on a button. In Java you use the addListenener function if I remember right, and then put there your call back there. C# offers a shortcut for it with the operator +=

Does it make sense ? Yes. Would I overload all the times the operators? Certainly not, but when it makes the code more natural to read then it's the way to go.

Re: Java what is actually happening here?

Posted: Sun Sep 21, 2014 2:27 pm
by vitinho444
Wow thanks for the lesson, the == is probably the most common, since I've started college I got used to use .Equals() that does what its supposed to do, compare contents. I don't use == for anything else nowadays, unless it's some numbers.

Re: Java what is actually happening here?

Posted: Sun Sep 21, 2014 2:44 pm
by a_bertrand
That's exactly my point. Why have an == and basically it's nearly never used? Does that make the language easy to read at first? No, sure if you know Java then you know you should use equal (which you can't use with numbers as they are no object).

For me a good language should help you write easy to read code, not make things harder.

Re: Java what is actually happening here?

Posted: Sun Sep 21, 2014 4:00 pm
by vitinho444
If you want readable code then use Basic :D

Code: Select all

If a > 10 then
Do something
end if 

Re: Java what is actually happening here?

Posted: Thu Sep 25, 2014 7:55 pm
by Jackolantern
Probably the best example of the power of operator overloading is the BigNumber class in Java and its equivalent in .NET, BigInteger. First, the purpose of this class (in both class libraries) is to represent numbers larger than a 64-bit integer can hold. Obviously, this doesn't come up that often but if you need it, you need it. In Java, which does not allow operator overloading, you have to do arithmetic with BigNumber objects like this:

Code: Select all

BigNumber result = bNumber1.add(bNumber2).multiply(bNumber3).subtract(4);
In .NET, which does allow operator overloading, you would do the same this way:

Code: Select all

BigInteger result = (bInt1 + bInt2) * bInt3 - 4;
It is pretty obvious which is better lol.

But as usual, with great power comes great responsibility. Operator overloading can be horribly abused, which is why it isn't allowed in Java. You don't see this much in C#, but in some legacy C++ code from the days before strong software engineering principles were the norm, you will find people using operator overloading in extremely confusing ways. It is intended to be used for operators that are logical to the situation, such as overloading +, -, *, etc. for BigInteger. For example, overloading -, +, * makes no sense for your Player class. What on earth does player1 + player2 mean? All you are doing at that point is making your code less legible.