Operate with me !!!

Operating anything gives you some fruitful result...

Its true that, whatever you do is an algorithm. Whatever it may be, listening to music, reading books, cooking food, walking, running, playing games, being in love or making love...
(I can guess the smile)

Whatever it is, it is an algorithm.

Well, good enough ! So, you tell there is no place for emotion, everything is so mechanical ?
No, its not like that. I never told that Emotion has no place. Actually emotion is also a special kind of algorithm which is more spontaneous, complex and random.
Human brains are designed so well that it gives 100% abstraction of the algorithm it follows over the data received from our sensory organs. Organs are input/output devices like Keyboard, Mouse, Monitor while brain is the Central Processing Unit. Brain also have the Register Cache, Random Access Memory and Hard Disk attached with it.

Aww...(Hmm...) Finally you came to the point where I can think myself as computer.
Hey, never do that. Think the exact opposite. Your Computer is something that follows you. Don't follow a less functional machine. Always try to put the way you think in the machine.
Might be human race will define a machine in the same complex and spontaneous way it works some day. Might be the year would be 5015...

So, what's the point here?
Well, the point here is that algorithm is everything that needs to be followed to achieve something.

So, what is an Algorithm ?
Algorithm is nothing but some predefined set of rules that needs to be followed by a machine.

OK, so what is the way to achieve a rule ?
Rules are defined in set of operations.
For example, to add two numbers, you need to follow something like the following algorithm,
  • Take input for operand1
  • Take input for operand2
  • Add the operands
  • Store it in another variable
  • Display result
Well, your algorithm can be something else. But the point is that you are doing some operation.

Now the point comes to take a look on some operators.

What is an operator now ?
An operator is a way to instruct the underlying machine to perform something.

Well, so, what is the operator for Being in Love ?
As I mentioned, the human brain gives 100% pure abstraction. So, we are not even allowed to view the operators.

You are so bad!!! You only told about being in love at the first place, now you are not able to answer. So what do you know ?
Well, I understand your point but sorry I am helpless here.


Hey, let's learn about operators in Java rather than Human Brain !!!

OK, no option you left for me now !!!
Thanks for the curse you just made for me.

Well, in Java, we have different operators. They are grouped in different sets. We already have checked some operators in previous two articles. Again, here also I will be discussing some more and eventually I will cover all operators as and when required instead of putting them altogether.
If you are searching for a complete reference, you can check this link.

Lets check the assignment operator (=),
In Java, if you are trying to store the result in a memory, you should be using this.
Let's have a look at a code segment,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 package blog;  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public class MainClass {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // literal assignment  
           boolean bool = true;  
           int twenty = 20;  
           String string = "string";  
           // Expression assignment  
           int fifty = twenty + 30;  
      }  
 }  

So, you can see that, we can assign expressions to variables and use them. This is simple as you can see and you have used it in your algebra notebook many a lot of times.
Similarly, we use +, -, *, /, % for addition, subtraction, multiplication, division and modulo division respectively.
Let's put them in a whole in code and check each one,



 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
   
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public class MainClass {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // literal assignment  
           boolean bool = true;  
           int twenty = 20, thirty = 30;  
           String string = "string", space = " ", concatenation = "concatenation";  
           // Addition  
           int fifty = twenty + thirty;  
           String concatenated = string + space + concatenation;  
           // Subtraction  
           int ten = thirty - twenty;  
           // Multiplication  
           int sixHundred = twenty * thirty;  
           // Division  
           int one = thirty / twenty;  
           // Modulo  
           int remainder = thirty % twenty;  
           System.out.println(fifty);  
           System.out.println(concatenated);  
           System.out.println(ten);  
           System.out.println(sixHundred);  
           System.out.println(one);  
           System.out.println(remainder);  
      }  
 }  

And the output is,

 50  
 string concatenation  
 10  
 600  
 1  
 10  


Hey wait, wait. I just found two problem !
One is we are adding two strings and 30/20 is supposed to be 1.5 and not 1.

Exactly, what does they mean at all ?
Yes, we can add two Strings. Keep in mind that only addition is allowed for String, no multiplication, division. We'll check this part later. For now just keep in mind you can add two String.
For your second question, Java does the arithmetic perfectly. It is me who did this mess.

What ?
Yes, when we divide two integers and the result becomes a double value, the decimal part is lost and only the integral part remains.
As in our case, we have 30/20 which yields 1.5, so the 0.5 decimal part is lost and only the integral part 1 is stored in the variable.

JOB WATCH: If you are getting something unexpected in a big calculation which includes many arithmetic calculations. Then check for the assignment of the intermediate results.

Now, we have another assignment operator, which works on three operands. This is known as ternary operator.

This is basically a replacement of simple if-else block for variable assignment. It has the following form,
variable x = (expression) ? value if true : value if false

Let's put this in code, that will make a clear picture.


 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  

 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public class MainClass {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // Literal assignment  
           int ten = 10;  
           // Using ternary  
           int twenty = (ten == 10) ? 20 : 30;  
           int thirty = (ten != 10) ? 20 : 30;  
           String fourty = (ten == 10) ? "40" : "30";  
           System.out.println(twenty);  
           System.out.println(thirty);  
           System.out.println(fourty);  
      }  
 }  
And the output is,

 20  
 30  
 40  

Basically the previous program is analogous to the following,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  

 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jan 25, 2015  
  */  
 public class MainClass {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // Literal assignment  
           int ten = 10;  
           int twenty, thirty;  
           String fourty;  
           if (ten == 10) {  
                twenty = 20;  
           } else {  
                twenty = 30;  
           }  
           if (ten != 10) {  
                thirty = 20;  
           } else {  
                thirty = 30;  
           }  
           if (ten == 10) {  
                fourty = "40";  
           } else {  
                fourty = "30";  
           }  
           System.out.println(twenty);  
           System.out.println(thirty);  
           System.out.println(fourty);  
      }  
 }  

Of course, the output is no different from the previous one.

Already if-else is there, why do we need ternary ?
Well, for less code to write, better readability. It does not really impact on performance. It basically depends on the programmer which one to use.

At this point, you are more likely to check all the operators. I am not going to make you wait...
Just wish you a happy operator testing !!!!


Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment