Introduction
Java is a high-level programming language developed by James Gosling at Sun Microsystems and introduced in 1995. Java is called a high-level language as programs can be written in a readable format. The program is converted into bytecode by the interpreter because a computer only understands the language of bytecode, which is a sequence of 0s and 1s.
The program converted into bytecode by the interpreter is then run on a compiler to see the execution of the program and its output. Java is a platform-independent language as any program written in Java can be executed on any platform that supports the Java Virtual Machine. Furthermore, it is used to develop various applications, such as web applications, web servers, desktop applications, mobile applications, etc.
Concept of Object-Oriented Programming
Java is an Object-Oriented Programming language because its programming structure is based on the concept of classes and Objects. An object is a basic building block of a program representing the real-world entities. It knows how to operate and interact with the elements of a program, whereas a class is a blueprint for creating an object.
Objects are the instances of a class and define the attributes(unique identity) and behaviors (methods or functions) of a program. You can understand the concept of classes and Objects through real-life examples like a car is a class and its identity, like car name, model name, colour of a car, and features like acceleration, brake, and clutch functionality represent the objects of a class.
Basic Programming in Java
Given below are the basic programs in Java that makes your understanding clear about the basic syntax and way of writing a program in Java.
1. Java program to print a Statement
In this program, you only need to print a statement in Java.
// Java program to print a statement
class task {
public static void main(String[] args) {
System.out.print("Welcome to Tpoint Tech Website");
}
}
Output:
Welcome to Tpoint Tech Website
2. Java Program to Perform Arithmetic Operations
In this program, you need to perform the basic arithmetic operations like addition, subtraction, multiplication, and division.
Given below is a Java program to perform the basic arithmetic operations.
// Java program to perform the arithmetic operations
class task {
public static void main(String[] args) {
int var1 = 14, var2 = 12;
int sum = var1 + var2; // perform the addition
int sub = var1 - var2; // perform the subtraction
int mult = var1*var2; // perform the multiplication
int div = var1 / var2; // perform the division
int remd = var1 % var2; // gives the remainder
System.out.println("Addition Operation = " + sum);
System.out.println("Subtraction Operation = " + sub);
System.out.println("Mutiplication Operation = " + mult);
System.out.println("Division Operation = " + div);
System.out.println("Modulus Operation = " + remd);
}
}
Output:
Addition Operation = 26
Subtraction Operation = 2
Multiplication Operation = 168
Division Opeeration = 1
Modulus Operation = 2
Loops in Java
Loops in Java are used to perform repetitive tasks. It is a very important concept in programming, commonly used in writing a program. When loops are introduced in programming, they repeat a certain instruction of code. The block of code will repeat itself as long as the condition is true in the looping statement.
The loop will stop executing when the condition is false provided in the loop, and the flow of the program comes out of the loop and starts executing the next instruction, followed by a loop. The following are the 3 types of loops used in Java programming language.
1. for Loop: for loop are used to repeat a certain instruction of code as long as the condition provided in the loop is true. Given below is the syntax and example of for loop in Java programming.
Syntax of for loop:
for(initialization; condition; increment/decrement) {
// instructions of code to repeat itself
} // end of the for loop
Example:
// Java program to demonstrate the working of for loop
public class Main {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 5 ; i++) {
sum = sum + i; // This line will repeat itself up to 5 times
}
System.out.println(sum);
}
}
Output:
10
2. While loop: While loop is used in Java programming to repeat a certain instruction of code, and it is different from the for-loop not only in terms of syntax but also in use. It is used when we don’t know the exact number of times to perform a loop on certain instructions of code. Below is given the syntax and example of a while loop.
Syntax of while-loop:
while(condition) {
// instruction of code to repeat itself
}
Example:
// Java program to demonstrate the working of while-loop
class task {
public static void main(String[] args) {
int sum = 0;
int i = 0; // initialization
while(i < 5 ) {
sum = sum + i; // this statement will execute 5 times
i++; // increment operation
}
System.out.println(sum); // final result will be print
}
}
Output:
10
3. Do-while loop: The do-while loop is another looping statement in Java used to perform repetitive tasks for a set of instructions in a program. It is somewhat similar to while loop, the difference between the while loop and do-while loop is that while loop check the conditional statement before the iteration of each loop whereas in do-while loop, the conditional statement are checked after the loop has been executed once.
It makes the do-while loop to execute one time even if the initial condition is false. The do-while loop is used to ensure that the loop body executes once before checking the conditional statement. Given below is the syntax and example of a do-while loop.
Syntax of do-while loop:
do {
// code where looping will be performed
}
while(condition);
Example:
// Java program to demonstrate the working of do-while loop
public class Main {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i; // this line will execute 5 times
i++; // increment operation up to limit 5
} while(i < 5); // end of the do-while loop
System.out.print(sum); // final result will be printed
}
}
Output:
10
Break and Continue Statement in Java
The break statement is used to terminate the execution of a loop immediately and transfer control to the program outside the loop to follow the next instruction.
The break keyword is used to execute the break statement. Given below is an example of using the break statement in Java.
Example:
// Java program to demonstrate the working of break statement
public class Main {
public static void main(String[] args) {
int i = 0;
int sum = 0;
for(i = 0; i < 7; i++) {
sum = sum + i;
if(i == 4) // 1+2+3+4 = 10
break; // this line will stop the loop when i = 4
} // closing of the for loop
System.out.print(sum); // final result will be printed
}
}
Output:
10
The continue statement is used to jump the execution of a loop to the next iteration without executing the iteration for which the continue statement is used. The keyword continue is used to execute the continue statement in a Java program.
In simple words, it skips the iteration for which the continue statement is used and continues with the iteration as long as the condition is true for executing the loop. Given below is an example of using the continue statement in Java.
Example:
// Java program to demonstrate the working of continue statement
public class Main {
public static void main(String[] args) {
int i = 0;
int sum = 0;
for(i = 0; i < 7; i++) {
if(i == 4) // skip the iteration when i = 4
continue;
else
sum = sum + i; // sum will be added and store in the variable sum
} // closing of the for loop
System.out.print(sum); // final result will be printed
}
}
Output:
17
Dry Run of the Program
i i < 7 sum = sum + i
i = 0 0 < 7(True) sum = 0
i = 1 1 < 7(True) sum = 1
i = 2 2 < 7(True) sum = 3
i = 3 3 < 7(True) sum = 6
i = 4 4 < 7(True) sum = 6
i = 5 5 < 7(True) sum = 11
i = 6 6 < 7(True) sum = 17
i = 7 7 < 7(False) sum = 17
Conclusion
This article describes the basic operations used in Java programming along with their syntax and working code. It gives information about the different looping statements used in Java programming and different basic operations used while writing the code in Java. Java programming is in high demand in today’s time, and it will be in high demand in the future as well because of its scalability and various features it provides to its applications.
There are many popular apps that are built using Java, like Amazon, Netflix, Uber, Spotify, etc. There are many benefits of learning Java programming. I hope this article has provided you with valuable information. If you are looking for more of this kind of article, then I suggest you visit the Tpoint Tech website, where you can find various articles in different programming languages, along with an online compiler where you can run your code.