#java Android AndroidDev javadeveloper  tutorial learning teaching
Explore tagged Tumblr posts
javatutorialsbytravismix · 7 years ago
Text
Variables
Variables are probably one of the most important concepts in programming.  Variables are probably the one thing in programming that you will use more than anything else.  So what are these variable things?  Variables are used to store values in memory so that you can access them later.  There are many different types of variables so lets get to them.  
The first variable type is int which lets store whole numbers.  Next we have float and double which let you store numbers with decimals.  A float is less precise than a double but also uses less memory however for most cases you can get by just fine with double as we have plenty of memory these days to work with.  The two other important variable types are String and boolean.  String lets you store strings of characters or words, but must be wrapped in double quotes ” “ The boolean type stores true or false. There is also the char type which stores a single character.
So lets take a look at how to use these variables.  Create a new Java project and find the main function.  As a reminder main is the code that looks like 
public static void main(String[] args) {
}
Inside of the main function place the following code.
int myInt = 5; // I am an integer and store whole numbers float myFloat = 3.2f; //I am a float and store numbers containing decimal points
double myDouble = 3.25; // I am a more precise version of float
 String myString = "Hello, World"; // I store strings of characters
boolean isBoolean = true; //I store true or false only
char myChar = 'c'; // I store single characters
You probably noticed that the float value has an f at the end before the semi colon.  You can use either lowercase or uppercase f but the f is required in Java to let it know that it is a float.  So now we have these variables and have initialized them (given them a starting value) what can we do with them?
Try adding the following code inside of main after your variables and running your code to see what happens.
System.out.println(myInt); System.out.println(myFloat); System.out.println(myDouble); System.out.println(myString); System.out.println(isBoolean); System.out.println(myChar);
Ok while that was interesting we can also change the value stored inside a variable at anytime by simply writing something like...
myFloat = 54.3f; 
To see what this does go ahead and have your program output myFloat after the previous code inside main using System.out.println  What you will notice is that the new output is 54.3  Another useful thing that you can do is with Strings.  Try the following code out.
System.out.println(myString + ", Travis");  What this code does is it concatenates ( adds together) the value stored in myString and “, Travis” so that you can output the whole thing.  Another useful thing you can do is
 myString = "Your number is: " + myInt;
now when you run System.out.println(myString); your output should be Your number is: 5 
You can even combine this all together inside the System.out.println function as follows
String myNewString = "Hello, "; String name = "Travis"; System.out.println(myNewString + name + " your number is: " + myInt);
Your output should be Hello, Travis your number is: 5
Your challenge for today is to keep messing with variables and coming up with your own things like what we have done above.  The best way to find out what works and what doesn’t work is to try things out.  The worst you will do is make a program that doesn’t work.  If something doesn’t work try reading the error output and seeing if you can figure it out and how to fix it.
Extra Reading
Java Variables https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
0 notes