I try to explain things as easy as possible. I had actually done a previous tutorial and people loved it. After all, there isn't much to do on here ever since the redesign.
And you can also get the sum, difference, product, etc. of other numerical variables into one variable!!
int thisMayBePrettyBig = myByte + exampleVar + windingRoad;
Besides these reasons, there are so many other ways variables can be used, and are really helpful in programming.
That's all for this lesson. In the next lesson, I will talk about mathematics in Java!
We can also use variables with System.out.println().
If you use the variable's name as the argument, like so:
System.out.println(firstInitial);
Then in the console, it will print out the value: A.
You can also use math with variables!
System.out.println(myByte + exampleVar);
Which will give us 88.
The declaration part: you start by typing in the data type, then the name of the variable. The initialization part: then with the equal sign (=), you set it equal to the value.
int exampleVar = 38;
You can also declare and initialize on different lines:
int anotherVar;
anotherVar = 71;
Let's define other data types into variables.
Okay, let's learn how to make our own variables.
Java is a "strongly-typed" language, meaning that you have to assign the data type to its correct value yourself. Let's look at the "declaration and initialization" process.
<data type> <name> = <value>;
Okay, now that we learned about the data types, let's put them to good use with variables.
A variable is a placeholder for a data type that takes a name. It's just like in algebra. You have a variable called x, and the number it holds is 2. That's just how variables work in programming.
The final two data types are char and String.
A char is a single Unicode character. Chars must always be between single quotation marks, like this: 'a'
A String is pretty much anything within two double quotation marks. It is the only data type in Java that must be defined with a capital letter. Our message "Hello world!!!!!" is an example of a string.
Next one we have is the boolean. The word boolean is named after George Boole, a logician. A boolean value is simply true or false.
If 5 is greater then 3, then the boolean is true. If the opposite, however, the boolean is false.
The next data types we will look at are float and double. With these data types, you can use decimals.
Floats can have up to seven decimal places, while doubles can have up to sixteen. Doubles are much more accurate, especially in currency, because of its advantage of decimal places.
However, you can't assign a really huge number as an int; Java indeed has its limits.
Byte range: -128 to 127 inclusive.
Short range: -32,768 to 32,767 inclusive.
Int range: -2³¹ to 2³¹-1.
Long range: -2⁶³ to 2⁶³-1.
The first data type is called int. Int is an integer value that is 32-bit in size. Ints CANNOT be decimal numbers. They must be whole numbers.
Three other data types that are similar are byte (8-bit), short (16-bit), and long (64-bit). Int is the most common out of all these types.