Disclaimer!
If you find any mistakes in this post or you don't know what to do at a specific point? Feel free to write me a comment! 👍
Greets from Germany ✌️
Last Part | Next part |
---|---|
<- #01 The Classic "Hello World!" program | #03 Escape Sequences and better print instructions -> |
1. Variables and different Data Types
Still remembering this code? This is kind of our "base" code for a while. So remember how to write this.
public static void main(String[] args){ }
So now we gonna create our first variable. But before we need to answer the question, what a variable is. It is basically every character like x
and we give x
the number 5. In Java this should look like this:
And you are even able to print the content of a variable, with the System.out.println();
instruction. Important is just, that you don't use quotes ("") for this, because it would then just print x
as a text.
Remembered to save? CTRL + S or STRG + S. Now run the code via pressing F11. Does the console give out our number?
Of course it does! Ouhm... by the way, you are even able to save some space by shortening the code like this:
And now the question: Am I able to print floationg-point numbers? Of course! But you need to use another data type for this variable, because as you can see, we wrote an int
before the x
.
That means, that the data type of the variable is a Integer. And a Integer can't show floationg-point numbers. But the data type double
can!
So just replace the int
with a double
.
Boom! By saving and running, we gonna get the following output from the console:
Now if you are a bit confused, which data types are existing, here is a table:
Data Type | Size | Value Range |
---|---|---|
boolean | 1 bit | true / false |
byte | 8 bit | -128 | +127 |
short | 16 bit | -32,768 | +32,767 |
int | 32 bit | -2,147,483,648 | +2,147,483,647 |
long | 64 bit | -9,223,372,036,854,775,808 | +9,223,372,036,854,775,807 |
float | 32 bit | ±1,4E−45 | ±3,4E+38 |
double | 32 bit | ±4,9E−324 | ±1,7E+308 |
These aren't all existing data types. The common ones you should use are int
, double
and boolean
By the way, the boolean
data type can only hold 2 different states (like in the table). These are true
and false
.
Now let's do some math. Create 2 more variables called y
and z
and give y
the number you want and z
should be the sum of x
and y
. Then output the variable z
. Does your code now look like this?
I hope so! After running this code, you will see the correct output of the two numbers, that x
and y
holds (In my case 5 and 8).
But there is also a more efficient way to write this code. So at the end it will look like this (Note: It has still the same output):
You are also able to replace the + (PLUS) with - (MINUS) or * (TIMES) or / (DIVIDER). And you can also create mass System.out.println();
instuctions, which could look like this:
Did you noticed how I changed the x
value at the second half and the output then completely changed? This is the huge advantage of variables instead of normal numbers. I just have to change one number and then my code will completely work with only this number, instead of changing each number individual.
Support this project by upvote this post and commenting ^^
Last Part | Next part |
---|---|
<- #01 The Classic "Hello World!" program | #03 Escape Sequences and better print instructions -> |
Remember: I'm not a genius too! I'm 16 years old but I know quite a lot, because Java or programming in general is fascinating me so much (and yes I have friends and other hobbies :)). So don't give up! Follow you dreams and in some day, they will get true.