Java is one of the most popular programming languages in the world, known for its versatility and wide range of applications. Whether you want to develop desktop applications, mobile apps, or even server-side systems, Java can be a great choice. In this article, we will provide a beginner-friendly introduction to Java, covering the basics and providing examples and code explanations to help you get started.
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle Corporation) in the mid-1990s. It was designed to be platform-independent, meaning that Java programs can run on any operating system or device with a Java Virtual Machine (JVM) installed. This "write once, run anywhere" feature makes Java a popular choice for cross-platform development.
To start programming in Java, you'll need to set up your development environment. Follow these steps:
Once you have completed these steps, you're ready to start coding in Java!
Let's take a look at some basic Java syntax:
// Hello World program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
When you run the code, you should see the output:
Hello, World!
Variables are used to store and manipulate data in a program. In Java, you need to declare variables before using them. Here's an example:
// Variable declaration and assignment
int age = 25;
String name = "John Doe";
double pi = 3.14;
In the above code, we declare three variables: age, name, and pi.
The int keyword specifies the variable type as an integer, and double specifies a floating-point number.
We assign values to the variables using the assignment operator =.
Control flow statements allow you to control the flow of program execution based on conditions. Here are a few examples:
if-else statement:
// Checking if a number is positive or negative
int number = -10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is negative.");
}
Code Explanation:
In the above code, we check if the variable number is greater than 0.
If the condition is true, we print "The number is positive." Otherwise, we print "The number is negative."
for loop:
// Printing numbers from 1 to 5 using a for loop
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Code Explanation:
The above code uses a for loop to print numbers from 1 to 5.
The int i = 1 initializes the loop variable i with 1.
i <= 5 is the condition that controls the loop execution.
i++ increments the value of i by 1 in each iteration.
The output will be:
1
2
3
4
5
Java is an object-oriented programming language, which means it uses objects to represent and manipulate data. Here's a simple example of a class and object in Java:
// Creating a class named "Person"
class Person {
String name;
void introduce() {
System.out.println("My name is " + name);
}
}
// Creating an object of the class "Person"
Person person = new Person();
person.name = "John Doe";
person.introduce();
Code Explanation:
The output will be:
My name is John Doe
Sample Problem 1: Write a Java program to find the sum of all numbers from 1 to 10.
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
Sample Problem 2: Write a Java program to check if a year is a leap year.
int year = 2020;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
60 videos|37 docs|12 tests
|
|
Explore Courses for EmSAT Achieve exam
|