Java for Absolute Beginners: Simplified Learning with Notes and Code Examples
🕒 2025-04-16 14:49:14.093102This is an introduction to Java programming for newcomers. The fundamental syntax is quite simple to understand.
What will you learn?
- Introduction to Java Programming Language
- How to print in Java?
- Declare Variables and increment/decrement operator
- Take input from the user
- String and Math Operations
- Conditional Statement (if-else)
- Loop (for, while, do-while)
- Switch Statement
- Arrays and how to iterate them?
- Exception Handling (try-catch)
- Function: Defining and Calling
- OOP program to find the area and perimeter of a rectangle
Introduction to Java Programming Language
Java is the most popular programming language. The Java Programming Language makes it very simple to create mobile applications, online applications, desktop programs, and other games.
We can use Java on different platforms like Windows, Mac, Linux, Raspberry Pi, etc.). It is an object-oriented programming language in which a program is divided into objects. If you are familiar with C++ or C#, it will be very easy for you to get all the syntax of Java. If not, don't worry, we will start with a basic course.
To start with Java, you need to first install JDK (Java Development Kit) and then you can choose any editor like IntelliJ Idea or Eclipse. Both editors have their own features. But for beginners, you don't need to worry about this as both of the editors contain features that we want and the easiness is almost the same in both the editor.
How to print in Java?
It is very simple to print in Java. You can use System.out.print. You can write like sysout and then click enter to generate a statement System.out.println. It is based on what editor you used.
This is a very general structure of your code. The package indicates your folder name where your class is located. In this example, Basics is a class name (Basics.java) and it is located in a folder JavaFile.
package JavaFile;
public class Basics {
public static void main(String[] args) {
//To print in Java
System.out.println("Ishwar Gautam");
//To print in the same line
System.out.print("My name is ");
System.out.println("IG ");
}
}Output:
Ishwar Gautam
My name is IG
Declare Variables and increment/decrement operator
Just like other programming languages, you can easily declare variables. While declaring variables, you have to give proper types like int, float, String, and boolean.
The increment and decrement operator is a very useful way to increase/decrease the value of a variable. For increment, ++ operator can be used and for decrement, -- operator can be used. i++ is same like i=i+1 or i+=1. All of these work as same.
package JavaFile;
public class declareVariables {
public static void main(String[] args) {
//To declare variable
int num = 10;
System.out.println(num); //print 10
System.out.println(num++); //print 10 and then increment by 1
System.out.println(++num); //First increment by 1 and then print
System.out.println(num--); //print 12 and then decrement by 1
System.out.println(--num); //First decrement by 1 and then print
//Declare float value and then print them
float num2 = 20.2 f;
System.out.println(num2);
//Declare double value and then print them
double num3 = 20.2;
System.out.println(num3);
}
}Output:
10 10 12 12 10 20.2 20.2
Take input from the user
To take input from the user, Scanner util can be used. We need to create a Scanner object and using that object, input can be taken from the user.
package JavaFile;
import java.util.Scanner;
public class userInput {
public static void main(String[] args) {
//Taking input from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scan.nextLine();
System.out.println("Your name is " + name);
System.out.print("Enter your age: ");
int age = scan.nextInt();
System.out.println("Your age is " + age);
}
}Output:
Enter your name: Ishwar
Your name is Ishwar
Enter your age: 26
Your age is 26
String and Math Operations
Several string and math operations can be performed like calculating the length of a string and getting a random number.
package JavaFile;
public class StringAndMathOperation {
public static void main(String[] args) {
String name1 = "Ishwar";
System.out.println(name1); // Ishwar
System.out.println(name1.length()); // 6
System.out.println(name1.toUpperCase()); // ISHWAR
System.out.println(name1.toLowerCase()); // ishwar
System.out.println(name1 + " from\" " + "Nepal --> Escape sequence"); // Ishwar from" Nepal --> Escape sequence
String name = "Ishwar";
System.out.println(name.contains("ar")); // true
System.out.println(name.startsWith("bi")); // false
System.out.println(name.endsWith("ar")); // true
System.out.println(name.indexOf("sh")); // 1
System.out.println(name.charAt(5)); // r
System.out.println(Math.max(10, 3)); // 10
System.out.println(Math.min(10, 3)); // 3
System.out.println(Math.sqrt(81)); // 9.0
System.out.println(Math.abs(-6)); // 6
System.out.println(Math.random()); //generate random number between 0 and 1
System.out.println(4 + (8 - 4) * Math.random()); //generate random number between any two numbers like 4 and 8
}
}Conditional Statement (if-else)
A conditional statement is used to check the condition and perform specific operations. If you are familiar with C/C++, you must be familiar with the if-else block. The syntax is very easy.
package JavaFile;
public class ConditionalStatement {
public static void main(String[] args) {
//If-else condition:
int age = 25;
if (age > 25) {
System.out.println("greater than 25");
} else if (age < 25) {
System.out.println("less than 25");
} else {
System.out.println("equal to 25");
}
}
}Output:
equal to 25
Loop (for, while, do-while)
A loop is used to iterate an element. We have for loop, while loop, and do-while loop in Java.
package JavaFile;
public class Loop {
public static void main(String[] args) {
// for loop
System.out.println("FOR LOOP");
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// while loop
System.out.println("WHILE LOOP");
int j = 0;
while (j < 5) {
System.out.println(j);
j++;
}
// do while loop
System.out.println("DO WHILE LOOP");
int k = 0;
do {
System.out.println(k);
k++;
} while (k < 5);
}
}Switch Statement
A switch is also another conditional statement. If a certain condition is met, a specific block will be executed.
package JavaFile;
import java.util.Scanner;
public class SwitchStatement {
public static void main(String[] args) {
//switch statement
int age = 26;
switch (age) {
case 12:
System.out.println("you are 12 years old");
break;
case 60:
System.out.println("you are 60 years old");
break;
default:
System.out.println("your age do not match any of the cases");
}
Scanner scan = new Scanner(System.in);
System.out.println("Press any key from 1 to 7 to generate day from Sunday to Saturday:");
int key = scan.nextInt();
switch (key) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("You pressed number out of range");
}
}
}Output:
your age does not match any of the cases
Press any key from 1 to 7 to generate the day from Sunday to Saturday:
7
Saturday
Arrays and how to iterate them?
An array is a way to store data/elements. It stores data in a list. We can iterate an array with for loop.
package JavaFile;
public class Array {
public static void main(String[] args) {
//Arrays
int[] marks = {
1,
5,
6,
7
};
int[][] marks2 = {
{
1,
2,
3
},
{
4,
5,
6
}
}; //two dimensional array
System.out.println(marks[0]); // 1
System.out.println(marks.length); //length of array 4
String name2 = "Ishwar";
System.out.println(name2.length()); //length of string 6
//classical way to iterate array
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}
//modern way to iterate array
//For each loop
for (int val: marks) {
System.out.println(val);
}
}
}Exception Handling (try-catch)
Exception handling helps prevent program crashes. It involves using a try-catch block. We place our code in the try block. If everything runs smoothly, the catch block won't execute. However, if any errors occur, the catch block steps in to manage those exceptions.
package JavaFile;
public class ExceptionHandling {
public static void main(String[] args) {
int[] marks = {
22,
57,
81
};
//try catch
try {
System.out.println(marks[4]);
} catch (Exception ex) {
System.out.println("Exception occur: " + ex);
}
}
}Output:
Exception occur: java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 3
Function: Defining and Calling
The function is a block of code that can be reusable. We need to call a function to execute it.
package JavaFile;
public class Function {
// Define function outside main function
static int sum(int firstNum, int secondNum) {
return firstNum + secondNum;
}
public static void main(String[] args) {
System.out.println(sum(5, 10)); // 15
}
}Object-oriented programming | Write an OOP program to find the area and perimeter of a rectangle.
OOP to find the area of a circle/perimeter of a rectangle
Java is an Object-Oriented Programming Language, so keep that in mind.
- In object-oriented programming, objects divide the program into individual parts.
- OOP follows a bottom-up approach.
- Adding new data and functions is easy.
- Object-oriented programming offers data hiding, enhancing its security.
- OOP is based on the real world.
To find the area and perimeter of a rectangle, we first have to know about the formula to calculate the area and perimeter of a rectangle:
Area of rectangle = length * breadth
Perimeter of rectangle = 2 * (length + breadth)
This shows object-oriented programming because we made a rectangle object from the Area_perimeter class and then used it to run our function and get the result.
package rectangle;
public class Area_Perimeter {
private double l, b;
public Area_Perimeter(double l, double b) {
this.l = l;
this.b = b;
}
public double area() {
return l * b;
}
public double perimeter() {
return 2 * (l + b);
}
public static void main(String[] args) {
Area_Perimeter rectangle = new Area_Perimeter(5, 3);
double Area_of_rectangle = rectangle.area();
double Perimeter_of_rectangle = rectangle.perimeter();
System.out.println("Area: " + Area_of_rectangle);
System.out.println("Perimeter: " + Perimeter_of_rectangle);
}
}
Thank you for taking the time to read my content. I hope you found this information useful. If you have any questions, please leave them in the comments section. I'll get back to you as soon as possible. For more similar content, you can visit my YouTube channel IG Tech Team. Thanks.
Comments
Loading comments...
Leave a Comment