EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Java for EmSAT Achieve  >  Java: Introduction

Java: Introduction | Java for EmSAT Achieve PDF Download

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.

What is Java?

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.

Setting up Java Development Environment

To start programming in Java, you'll need to set up your development environment. Follow these steps:

  • Download and install the Java Development Kit (JDK) from the Oracle website.
  • Set up the Java Development Kit (JDK) by adding the JDK's bin directory to your system's PATH variable.
  • Install an Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or NetBeans.

Once you have completed these steps, you're ready to start coding in Java!

Basics of Java Syntax

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!");

    }

}

Code Explanation

  • The above code demonstrates the famous "Hello, World!" program in Java.
  • public class HelloWorld declares a class named "HelloWorld".
  • public static void main(String[] args) is the entry point of a Java program. It's a special method that gets executed when the program starts.
  • System.out.println("Hello, World!"); prints the text "Hello, World!" to the console.

When you run the code, you should see the output:

Hello, World!

Variables and Data Types

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;

Code Explanation

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

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

Object-Oriented Programming (OOP) in Java

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:

  • In the above code, we define a class named "Person" with a name variable and an introduce() method.
  • We create an object of the class by using the new keyword and assign it to the variable person.
  • We set the name of the person object to "John Doe" and call the introduce() method, which prints the introduction.

The output will be:

My name is John Doe

Sample Problems and Solutions

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.");

}

The document Java: Introduction | Java for EmSAT Achieve is a part of the EmSAT Achieve Course Java for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
60 videos|37 docs|12 tests

Top Courses for EmSAT Achieve

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

video lectures

,

shortcuts and tricks

,

Java: Introduction | Java for EmSAT Achieve

,

Viva Questions

,

study material

,

MCQs

,

Previous Year Questions with Solutions

,

Java: Introduction | Java for EmSAT Achieve

,

Java: Introduction | Java for EmSAT Achieve

,

past year papers

,

ppt

,

Important questions

,

Sample Paper

,

Extra Questions

,

mock tests for examination

,

practice quizzes

,

Semester Notes

,

Objective type Questions

,

Free

,

Summary

,

pdf

;