Java Basics In One Page

I realized I cannot really cover every part of Java by writing blog posts. I mean I can but there’s no point reinventing the wheel when so many tutorials are already available. What I can do though is write a short guide which I may use in the future to revisit Java basics. So here we go!

#1. Get the tools

To start with Java, you’ll need JDK installed. Get it from here. Once you install JDK in your computer. Check whether the following commands are valid in your terminal.

[code]
javac
java
[/code]

They should be valid and shouldn’t return an error! Once you got that, just install a good text editor like Sublime Text (I recommend Sublime).

#2. Print Hello World

Next, just copy paste the following code in a new file inside Sublime text and save it as hello.java on your desktop!

[code lang=”java”]
class hello {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
[/code]

Now open your terminal, navigate to desktop (usually cd desktop) and then run the following commands:

[code]
javac hello.java
java hello
[/code]

First command should simply compile the code and create an object file like hello.class in the same directory. The next command will actually execute the class file which was created. This will print Hello World in the terminal.

#3. Variables And Basic Maths

Now create integer variables and so some basic maths using Arithmetic operators:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;
int y = 10;
System.out.println(x+y);
}
}
[/code]

This should create two variables (imagine boxes) containing 5 and 10. And then we simply output them on screen using the arithmetic operator plus (+). Try other operators for subtraction, multiplication, division and modulus (%). Modulus returns the remainder from the division.

#4. Strings

Like integers, you can also create string variables:

[code lang=”java”]
class hello {
public static void main (String args[]) {
String xyz = "Omkar";
System.out.println(xyz);
}
}
[/code]

This will simply print Omkar on screen!

#5. Concatenation

You can print them both together using the concatenation operator (+ operator).

[code lang=”java”]
class hello {
public static void main (String args[]) {
String xyz = "My number is: ";
int num = 5;
System.out.println(xyz + num);
}
}
[/code]

This will simply print “My number is: 5” on screen by concatenating the two variables before printing them.

#6. If Statement

Decision making is an important part in coding. You can do this using If statement. It’ll check whether a condition is true and then execute a certain piece of code:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;

if (x > 10) {
System.out.println("Batman");
}
}
}
[/code]

This will check if 5 is greater than 10 and return true or false. Since this condition returns false, Batman will not be printed.

#7. If Else

What if we want to print something when condition turns out to be false? We use Else block in that case:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;

if (x > 10) {
System.out.println("Batman");
}
else {
System.out.println("Superman");
}
}
}
[/code]

#8. Else If

We can have multiple condition checks using else if:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;

if (x > 10) {
System.out.println("Batman");
}
else if (x == 5) {
System.out.println("I’m a Number");
}
else {
System.out.println("Superman");
}
}
}
[/code]

This should print “I’m a Number” as x equals to 5.

#9. Switch

Switch is another decision making statement. Here’s how you can use it to choose or switch the flow using different conditions:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 3;

switch(x) {
case 1: System.out.println("1");
break;
case 2: System.out.println("2");
break;
case 3: System.out.println("3");
break;
default: System.out.println("nothing");
}
}
}
[/code]

This should print 3 on screen as x is 3. If nothing returns true, default case is executed.

#10. Array

My previous post was about arrays but I’ll go ahead and add the code here again to keep everything on a single page –

[code lang=”java”]
class hello {
public static void main (String args[]) {
int[] orray = {1,2,3,4,5,6};

for(int i=0; i<orray.length; i++) {
System.out.println(orray[i] + " ");
}
}
}
[/code]

Note: there are multiple ways to create an array. I’ve just shared one of them in the example above!

#11. Loops

I’ve written a separate post about loops already! You can check it out by clicking here. I had missed the for each loop in that post, so let’s cover that here:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int[] orray = {1,2,3,4,5,6};

for(int i:orray){
System.out.println(i);
}
}
}
[/code]

For each loop basically does the same thing as a for loop but is very useful for traversing an array!

#12. End

That’s all in this post. There’s so much more which I can cover but I’ll stop here for now. Maybe I’ll add more to this post in the future but I want to keep this simple so I’ll just leave it for now 🙂

Arrays in Java

So there’s something called Array in Java. In an array, you can store fixed-size sequential collection of elements of the same type.

The simplest way to create an array is using the following code:

[code lang=”java”]
int[] orray = {1,2,3,4,5,6};
System.out.println(orray[0]);
[/code]

This code declares and creates an array called orray of size 6 starting from index 0 to index 5. And on the next line we use print statement to print the element at position 0 in the array (which is 1).

The coolest part about arrays is that we can run a loop and print (or do something with) all the elements in the array as follows:
[code lang=”java”]
class hello {
public static void main (String args[]) {
int[] orray = {1,2,3,4,5,6};

for(int i=0; i<orray.length; i++) {
System.out.println(orray[i] + " ");
}
}
}
[/code]

This should print numbers from 1 to 6 (elements in the array), each on it’s own line. We can do so much more with arrays but that’s all you need to know for now!

Loops in Java

If you want to repeat certain set of statements over and over again, you can make use of loops in Java. Let’s start with the while loop:

[code lang=”java”]
int x = 0;
while (x < 10) {
System.out.println(x);
x++;
}
[/code]

This will print 0 to 9 on screen, each on a new line because we have println. If you use print instead, it’ll be printed on the same line.

In the above code, we basically initiated variable x to zero. And said keep printing x till it’s value is less than 10. Also each time we print x, we increment x by 1.

Let’s do the same thing using a do-while loop now:

[code lang=”java”]
int x = 0;
do {
System.out.println(x);
x++;
}
while(x<10);
[/code]

This again does the same thing but the difference is (as you might have noticed), it will execute the loop at least once even if the condition returns as false.

Let’s do this with a for loop now:

[code lang=”java”]
for (int x=0; x<10; x++) {
System.out.println(x);
}
[/code]

This prints the same thing. In this code, x is initialized to zero and the condition is checked (x is less than 10). So everything inside the for loop gets executed. After execution x is incremented and again the condition is checked. This goes on till the condition returns false.

There’s another kind of loop called the for-each loop. We’ll look into that after we understand Arrays. So the next post is of course about Arrays!

Decision making in Java

The best part about coding is making things happen. But it’s not fun printing predefined stuff, we need things to happen on specific conditions. For this we make use of something called if statement. This is a simple if statement:

[code lang=”java”]
int x = 5;
int y = 2;

if (x > y) {
System.out.println("X is greater than Y");
}
[/code]

Currently the condition (x > y) is true, so the if block will be executed. And then everything else after the if block will be executed in normal flow.

What if you need to execute something if (x < y)? In that case, you can use an else statement as follows:

[code lang=”java”]
int x = 5;
int y = 2;

if (x > y) {
System.out.println("X is greater than Y");
}
else {
System.out.println("X is less than Y");
}
[/code]

In any case you wish to have multiple conditions, you can do that as well using an else if statement in the middle as follows:

[code lang=”java”]
int x = 5;

if (x == 5) {
System.out.println("X is 5");
}
else if (x == 6) {
System.out.println("X is 6");
}
else {
System.out.println("X is not 5 or 6");
}
[/code]

You can also have nested if statements as follows:

[code lang=”java”]
String x = "human";
String y = "male";

if (x == "human") {
if (y == "male") {
System.out.println("It’s a human male model");
}
}

[/code]

Then there’s the switch statement:

[code lang=”java”]
int x = 0;

switch(x) {
case 1: System.out.println("number is 1");
System.out.println("this is first case");
break;
case 5: System.out.println("number is 5");
break;
default:
System.out.println("number is not 1 or 5");
}
[/code]

You can try to run the above code with different values of x. Try putting in 0, 1 and 5 to see the output. If x equals 1, statements inside the first case are executed. If x equals 0, the default case is executed as no case is matched.

In the next post, I’ll share info on loops in Java!

Basic Arithmetic in Java

You can do basic arithmetic in Java but before we get to that point, we need to learn about variables in Java. Check the following code –

[code lang=”java”]
class basic {
public static void main (String args[]) {
int a = 3;
int b = 2;
System.out.println(a+b);
}
}
[/code]

Ignore public static void main part, that’s something which you need to include in every program and we’ll get to that sometime later in the future. For now, just know that you need to have it in your program to run it. Everything that matters is contained within the main block. And the program execution begins from the main block!

So we initiate variable a as integer with value 3 and we do the same for b with value 2. Then using a print command we print the sum of a and b using an arithmetic operator (a+b).

Similarly we can subtract, multiply, divide numbers in the program –

[code lang=”java”]
class basic {
public static void main (String args[]) {
int a = 3;
int b = 2;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
}
}
[/code]

In the next post, I’ll share a bit about decision making statements and conditional operators in Java!