Core Java

Java Programming Basics

1. Introduction

Java is a high-level, general-purpose, object-oriented, and secure programming language developed by James Gosling at Sun Microsystems, Inc. in 1991. It is formally known as OAK. In 1995, Sun Microsystem changed the name to Java. In 2009, Sun Microsystem takeover by Oracle Corporation.

In this tutorial, we are going to learn the basics of Java Programming. We will learn about the JRE, the basic syntax, how to add comments to the code, Variables, Data Types, Keywords, Operators, and Loops.

2. Java Runtime Environment

The Java Runtime Environment, or JRE, is a software layer that runs on top of a computer’s operating system and provides libraries and other resources that a Java program needs in order to run. The JRE is one of the three components needed for developing and running Java programs. The other two components are the Java Development Kit, or JDK, which is a set of tools for developing Java applications, and the Java Virtual Machine, or JVM, for executing Java applications. The JRE combines the Java code we created using the JDK, with the necessary libraries required to run it on a JVM and then creates an instance of the JVM that executes the program.

3. Java Basic Syntax

Every line of code that runs in Java must be inside a class. A Java program is a collection of objects, and these objects communicate through method calls to each other to work together. A class name should always start with an uppercase letter. Note that Java is case-sensitive. Let’s see an example of Java syntax.

Hello.java

public class Hello {

//	This is a comment
	public static void main(String[] args) {
		System.out.println("Hello World");
	}

}
  • line 1: This is how a java class is declared. The code that is running is inside the Hello class.
  • line 3: We can also put comments inside our code. Comments are ignored by the JDK.
  • line 4: This is the main() method and is necessary for every java program in order to run. Inside we put the main code that we want to run.
  • line 5: This is the code we run for this example and prints to the console ‘Hello World’. System is a class provided by the JRE. It provides us with different methods. out is an instance of PrintStream type. println is a method of out instance.
java programming - Hello.java Example Output
Fig. 1: Hello.java Example Output

4. Comments

As we saw in the previous example, we can also add comments. We can indicate a single-line comment with two forward slashes (//). Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by Java.

Hello.java

public class Hello {

	/*
	This is a multi-line comment
	*/
	public static void main(String[] args) {
		System.out.println("Hello World");
	}

}

5. Variables and Data Types

We use variables to store data values. To create a variable, you must specify the Data Type, a name, and assign it a value: DataType name = value;. In Java, there are different Data Types of variables, for example:

  • String – Stores text. String values are surrounded by double-quotes. String text = “text”;.
  • int – stores integers without decimals. int x = 5;.
  • double – stores number with the decimals. double x = 5.0;.
  • boolean – can only store two values: true or false. boolean b = true;.

Hello.java

public class Hello {

	/*
	This is a multi-line comment
	*/
	public static void main(String[] args) {
		String text = "Hello World";
		System.out.println(text);
	}

}
java programming - Hello World Example with Variable
Fig. 2: Hello World Example with Variable.

6. Java Keywords

Java keywords are also known as reserved words. These are predefined words by Java and cannot be used as a variable or object name. A list of these keywords can be found here.

Here is a tutorial for Java Keywords.

7. Operators

Operator in Java is a symbol that is used to perform operations. For example: =, +, *, & etc. There are many types of operators.

  • Simple Assignment Operator
    • = Simple Assignment Operator
  • Arithmetic Operators
    • + Additive operator (also used for String concatenation)
    • – Subtraction operator
    • * Multiplication operator
    • / Division operator
    • % Remainder operator
  • Unary Operators
    • + Unary plus operator indicates a positive value
    • – Unary minus operator negates an expression
    • ++ Increment operator increments a value by 1
    • — Decrement operator decrements a value by 1
    • ! Logical complement operator inverts the value of a boolean
  • Equality and Relational Operators
    • == Equal to
    • != Not equal to
    • > Greater than
    • >= Greater than or equal to
    • < Less than
    • <= Less than or equal to
  • Conditional Operators
    • && Conditional -AND
    • || Conditional -OR
    • ?: Ternary (shorthand for if-then-else statement)
  • Type Comparison Operator
    • instanceof Compares an object to a specified type
  • Bitwise and Bit Shift Operators
    • ~ Unary bitwise complement
    • << Signed left shift
    • >> Signed right shift
    • >>> Unsigned right shift
    • & Bitwise AND
    • ^ Bitwise exclusive OR
    • | Bitwise inclusive OR

Learn more in our Java Operators Tutorial.

8. Loops

Looping in programming languages is a feature that gives us the ability to execute a set of instructions while some condition evaluates to true. Java provides three ways for looping.

8.1 While Loop

While loop starts with the checking of condition. If it is true, then the loop body statements are executed, otherwise, the loop ends. Here is an example.

While.java

public class While {
	public static void main(String args[]) {
		int x = 1;

		while (x <= 4) {
			System.out.println("x = " + x);

			// Increment the value of x for next iteration
			x++;
		}
		System.out.println("Loop ended");

	}
}
  • line 3: we initialize a variable for the condition.
  • line 5: checks if the statement is true, 1 less than or equal to 4 is true so we enter into the loop.
  • line 9: we increment the value of x by 1 and the loop repeats. When x =5 the statement is false so the loop ends and the next line after the loop is executed.
java programming - While Loop output
Fig. 3: While Loop output.

8.2 For Loop

For loop. Unlike a while loop, a for statement consumes the initialization, condition, and increment/decrement in one line providing a shorter structure of looping. This is the same example as before, using the for loop.

For.java

public class For {

	public static void main(String[] args) {

		for (int x = 1; x <= 4; x++) {
			System.out.println("x = " + x);
		}
		System.out.println("Loop ended");
	}

}
  • Line 5: Initialization condition: int x = 1;. Here, we initialize the variable in use. Testing Condition: x <= 4;. If the statement is false the loop ends. If it is true, the loop body is executed. Increment/ Decrement: x++ It is used for updating the variable for the next iteration.
Fig. 4: For Loop Output.
Fig. 4: For Loop Output.

8.3 Do…While Loop

Do…While loop starts with the execution of the body. Do…While loop will execute its body at least once. After the execution of the body, the condition is checked. If it is true, the next iteration of the loop starts. When the condition becomes false, the loop ends.

DoWhile.java

public class DoWhile {

	public static void main(String[] args) {
		int x = 1;
		do {

			System.out.println("x = " + x);
			x++;
		} while (x < 0);
		System.out.println("Loop ended");
	}

}
Fig. 5: Do…While loop Output.

9. Summary

In these examples, we saw the basics of Java. Java is a very powerful programming language and there are a lot of things that someone can learn and eventually create his own program.

11. Download the source code

This was an example of the basics in Java.

Download
You can download the full source code of this example here: Java Programming Basics

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button