Big Java Tutorial - Part 1
Java Tutorial Part 1
Since everyone is making Python tutorials, and Python isn't all that great cuz statements don't end with semicolons (usually), I decided to make a Java tutorial. In 3-part awesome crash-course tutorial you'll learn about the Java syntax, printing, data types/variables, math and concatenation, input, if statements, switches, and loops in Java, as well as classes, objects, methods, and parameters. *Takes deep breath.*
So let's get started...
But first, go ahead and create a Java repl. Try all the examples in the tutorial and you'll be a great Java programmer.
Unlike Python, in Java, we need stuff to be in classes and methods. Kepp in mind that the base thing that runs in a Java program is anything and everything in the
main() method. All methods must be in a class, which must have the same name as the filename. The basic syntax is like this:
Note: Coments in Java are either denoted by
// Single line comment or
Anyways, here it is:
And of course, EVERY STATEMENT MUST END WITH A SEMICOLON:
You'll learn more about classes and methods later in this tutorial.
Important in every programming language. The basic way to print Strings (words) is by using the this code:
Stuff this into the
main() method (in place of
// Code here.) and give it a go. You should see
Hello world! in the console. You can replace
Hello world! with anything else you want. So, that's how you print Strings in Java.
We have several different types of data in Java (as well as in many other languages). The most commonly used ones are:
String- This stores a group of characters, aka words. Beware that
Stringhas an UPPERCASE
int- This data type stores non-decimal positive and negative numbers.
boolean- The value is either
char- This data type stores either an uppercase or lowercase letter.
double- This stores decimal values.
So, to actually use these variables, we must declare them. The syntax is as follows:
You can call the variable name ALMOST anything...here are the "almosts":
- A variable name cannot start with a number.
- A variable name cannot contain symbols besides
- A variable name cannot be a Java keyword. Find the full list of keywords here.
Rember that everything in Java is case-sensitive.
Here are some rules for declaring each data type value:
Stringmust be surrounded by double-quotes:
"Your words here".
- Numbers and booleans aren't surrounded by anything.
charsare surrounded by single-quotes:
We can print variables by calling the variable name (without quotes):
The output is
Math and concatenation
To do math in Java, simply use the regular math operators:
Let's try dividing two numbers:
This will give us the number
2. For some cases, you don't need to create a new variable to see your result. You can just print it out, like this:
And we get the number
We can do the same stuff for other number types too, such as
double for decimal values.
However, if we try to add Strings, they will merely concatenate (be joined together),
The result is
JaneDoe. We can make this two words by adding concatenating a space in between the names, like this:
The output is
If we try to add
chars, the ASCII number value will be printed.
I'll post the second and third parts of the tutorial if enough people upvote it, which means my work isn't wasted.
bro, i think it would be better if you chain all your tutorials and post it at once. dont spread them out like that ;)