Java Variables

Hello, how are you doing? well i am very fine indeed in this fresh morning, first i want to tell you that i’m not the master of Coding because i’m still learning, excuse me if I made mistakes so any helpful knowledge would help. Now we’re going to talk about the base of Programming or Coding, specifically in Java. Program or Codes are based out of Variables, Syntax, Algorithm, etc.. (well that is more or less).

Now i’m going to explain the #1 which is Variables.

Variable

Variable are a symbolic name that contains or store information that are going to be referenced and used by the programs, The variable name usually represents what the variable contains. In Java there are 3 types of variable, that is: String, Integer and Double variable, These 3 variable uses different name because it contains different types of Data. The simple explanation is, if you wanna store your name in a variable, the type of variable you use is String. Or, maybe you wanna store you age in the variable, in that case you use Integer. Or even you wanna store how much money you make in a month, that type of data is stored as a Double. Don’t get confused here, you might ask, Why the heck does Java uses three variables instead of just one.Well, good question there, Java is a Strongly Typed Languange. Which means that Typing in Java will be absolute certainity that the information you stored in the variables are in a “certain way”.. so it will be very specific, it will give you error if the stored information does not match the type it suppose to be.

String variable

Just like I say before, if you wanna store your name in a variable which is a String, the informations that is stored would be like this, “Rafif Santika”, a String would make any data treated like an old plain English languange, a String would represents data as a Words (or specifically a Letter). It may come in your life as a programmer, that you would add two sentences or two Strings together. So, what does it means to add two sentences or two Strings together? You see, if you have 2 Strings variable, one of them contains the data “42” and the other one “3”, if you add them together what would you get? if you say “45” , then Great!!.

You’re Wrong!. Remember when I say “a String would make any data treated like an old plain English languange”, it happens to any data that is stored whether it’s a word or a letter or even a number, so the right anwer is “423”. Just imagine if the two Strings contains “Hello” and “World!”

what would happen to them if we add two of them? hopefully your natural instinct would say “Hello World!”(Haha!). The same thing happen to “42” and “3”.. Java behaves or treats the data differently because the different Type of variable.

Integer variable

Moving on to the Integer. Last time we say that if you want to store you age in a variable we use Integer, well that is because, Integer must contains data that is a number that doesn’t contain decimal in it, it would be a whole number like, 4 and 2 and 20 and 3 (that is a secret code about me, can you guess what those number means?) or maybe -4 and -1 and -20 and -3 (that is also a secret code), alll of the number I said up there, it all considered by Java as an Integer. So you need Integer variable to store them, so if you add an Integer to another like this: 42 + 3 it will make a result of 45 (not like how the String works) so if you do this:

(This will be written in Java languange, you could write this in Java Eclipse or Netbeans)

public static void main(String[] args) {

int first_number, second_number, answer; /*first we started with empty Variable*/

first_number = 10; /*then we fill the Variable*/
second_number = 15; /*then we fill the Variable*/
answer = first_number + second_number ; /*last we calculate them*/

System.out.println(“Addition Total = ” + answer ); /*then we output them to the world*/

}

The output result should be

Addition Total = 25;

if your output result is like that, Then you have Suceeded.

Double variable

Last thing, the Double variable. like we also say upthere, if you want to store how much money you make in a month, you uses the Double variable. Let’s just say you make $35.5 a month (it’s just an example) and you want to put it in your Code or Program, well you must know that you need to put it into somekind of variable right? what would you do? and then you wonder “let’s try and put it into the Integer variable” and you put it into the Integer variable, Great!! it’s Wrong!! you’ll get an error then you say “what is wrong? i put it into a variable, i mean it contains numbers right? and Integer are suppose to deal with number” well you’re right about one thing, Integer contains number data, But!! it has to be a specific whole number like 1,2,3… and it cannot contain another kind of data, well it’s right that there is a number you put that, but it contains another data type which is a $(dollar sign) and it contains decimal with two digits of accuracy. “then what am i suppose to do??” well you put it in a Double variable. for example(you could also write this in Java Eclipse and Netbeans) :

public static void main(String[] args) {

double first_number, second_number, answer; /*first we started with empty Variable*/

first_number = 20.5; /*then we fill the Variable*/
second_number = 20.8; /*then we fill the Variable*/
answer = first_number + second_number ; /*last we calculate them*/

System.out.println(“Addition Total = ” + answer ); /*then we output them to the world*/

}

The output result should be

Addition Total = 41.3;

if your output result is like that, Then you have Suceeded at double variable.

Conclusion

  • Java is a Strongly Typed Languange
  • There are 3 types of Java variables
  • Store your name in a String variable (store any word or letter)
  • Store your age in an Integer variable (store any kind of whole number)
  • Store how much money you have in Double variable (store any kind of special number, like decimal, or any symbols like $)

Give comment, help you and me grow our knowledge together. Tips, Counsel and Knowledge would really be appreciated. Thank you, see you on another Article.

😀

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

One thought on “Java Variables”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.