Java & J2EE Page 2 - Java Operators |
Variables with no data are just like frosty cold mugs with no soda in them --worthless. Sure, your girlfriend could throw them at you when you forget to take out the garbage or mention how pretty her sister looks. But when mugs and variables were invented, it was because their creator wanted an object that would hold something. In the case of mugs, it's beer, and in the case of variables, it's data. That's where certain kinds of operators come in. To fill a variable with data, you use one of the assignment operators, the simplest of which is the = symbol. my_iQ = 250; The above code assigns the value 250 to my_iq (I didn't want to be boastful, so I lowered the amount). If I later decided what the heck, why not be boastful, I could always do this: my_iq = 250; my_iq = my_iq + 50; This would first assign the value 250 to my_iq, then add 50 to the value of my_iq, leaving it at 300, which is what it truly is (wink wink). A simpler way to write the above code is like this: my_iq = 50; my_iq += 50; If I were drinking the night before, I could also use the subtraction assignment operator to show my true IQ: my_iq = 300; my_iq -= 250; The code above would show that alcohol has reduced my monstrous IQ to a feeble 50. If you are working with text or strings, you could do the following: class MyName { public static void main(String[] args){ String first_name = “James”; String second_name = “Payne”; String full_name = first_name + second_name; System.out.println(full_name); } } The above sample will print out the text: “James Payne” to your monitor.
blog comments powered by Disqus |
|
|
|
|
|
|
|