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.
Syntax
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 /*Multiline comment*/
.
Anyways, here it is:
public class Main { // Assuming the file name is called "Main.java". This is the class. public static void main(String[] args) { // And this is the method. // Code here. } }
And of course, EVERY STATEMENT MUST END WITH A SEMICOLON: ;
.
You'll learn more about classes and methods later in this tutorial.
Printing
Important in every programming language. The basic way to print Strings (words) is by using the this code:
System.out.println("Hello world!");
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.
Data types
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 thatString
has an UPPERCASES
.int
- This data type stores non-decimal positive and negative numbers.boolean
- The value is eithertrue
orfalse
.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:
type name = value;
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
$
and_
. - 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:
- A
String
must be surrounded by double-quotes:"Your words here"
. - Numbers and booleans aren't surrounded by anything.
chars
are surrounded by single-quotes:'a'
.
Declaring variables:
int intExample = 5; String stringExample = "Hello world!"; double doubleExample = 23.032 char charExample = 'Q'; boolean booleanExample = false;
We can print variables by calling the variable name (without quotes):
System.out.println(stringExample); // Refer to the last example
The output is Hello world!
.
Math and concatenation
To do math in Java, simply use the regular math operators:
+
for adding-
for subtraction*
for multiplication/
for division
Let's try dividing two numbers:
int numberOne = 10; int numberTwo = 5; int divisionResult = numberOne / numberTwo; System.out.println(divisionResult); // Print the result.
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:
int numberOne = 10; int numberTwo = 5; System.out.println(numberOne / numberTwo);
And we get the number 2
.
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),
String first = "Jane"; String last = "Doe"; System.out.println(first + last);
The result is JaneDoe
. We can make this two words by adding concatenating a space in between the names, like this:
System.out.println(first + " " + last);
The output is Jane Doe
.
If we try to add char
s, 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 ;)
@EpicGamer007 But it'll probably waste a lot of time if no one uses it.
@codingjlu if you make it longer, more people are likely to see it. trust me on this one
@EpicGamer007 Yeah lmao
Do you have a 2nd tutorial?