Java what is actually happening here?

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Java what is actually happening here?

Post 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?
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: Java what is actually happening here?

Post 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..
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Java what is actually happening here?

Post by KyleMassacre »

I don't know java really at all but is there a way to parse ASCII chars as text?
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Java what is actually happening here?

Post 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).
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: Java what is actually happening here?

Post by vitinho444 »

Alain are there languages where you can decide what Object++; does? Why is that good I ask?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Java what is actually happening here?

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: Java what is actually happening here?

Post 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.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Java what is actually happening here?

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: Java what is actually happening here?

Post by vitinho444 »

If you want readable code then use Basic :D

Code: Select all

If a > 10 then
Do something
end if 
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Java what is actually happening here?

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”