Learn to Code via Tutorials on Repl.it!

← Back to all posts
Big Java Tutorial - Part 1
codingjlu

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:

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:

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 that String has an UPPERCASE S.
  • int - This data type stores non-decimal positive and negative numbers.
  • boolean - The value is either true or false.
  • 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 $ 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:

We can print variables by calling the variable name (without quotes):

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:

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 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),

The result is JaneDoe. We can make this two words by adding concatenating a space in between the names, like this:

The output is Jane Doe.

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.

Voters
PythoNinja
austinliu64
xGabrielLeite
firedragondnd
LegendaryWolf
codingjlu
Comments
hotnewtop
EpicGamer007

bro, i think it would be better if you chain all your tutorials and post it at once. dont spread them out like that ;)

codingjlu

@EpicGamer007 But it'll probably waste a lot of time if no one uses it.

EpicGamer007

@codingjlu if you make it longer, more people are likely to see it. trust me on this one

IntellectualGuy

@EpicGamer007 Yeah lmao