Hey Everyone, in my last tutorial I showed you how to setup a Java file and print “Hello World” to the console. Now lets learn about variables!
In Java, every variable must have a type. The different types are: byte (number, 1 bit), short(number, 2 bytes), int (interger, 4 bytes), long (number, 8 bytes), float (float number, 4 bytes), double (float number, 8 bytes), char (a character, 2 bytes), boolean (true or false, 1 byte).
Examples:
Integer
//you can separate the declaration and initialization
int myNumber;
myNumber = 5;
//or you can combine them
int myNumber = 5;
Double, or a decimal number
double d = 6.5;
d = 3.0;
Float
//to use floats you have to cast them.
//but don't worry, we wont need to use floats for the things we will be doing here
float f = (float) 7.2;
Character
char c = 'k';
String
String name = "Kyle";
//note the capitalized S and the double quotes
//you can concat strings and primitives
int num = 4;
String c = "I gave you " + num + " quarters"; //Be sure not to use "" with primitives.
Boolean
boolean isActive = false;
boolean isSelected = true;
In the next tutorial we will put some of these variables to use!