Elements of Java Programming
Infinity Tutors
1. What is Java?
s
Java is a popular, object-oriented programming language used worldwide
or
for building applications across platforms. Its motto is “Write Once, Run
Anywhere” because compiled Java programs can run on any device that has
the Java Virtual Machine (JVM).
t
Tu
2. Setting Up Java Environment
Before writing Java code, we install the Java Development Kit (JDK) and
ty
set up an Integrated Development Environment (IDE) like Eclipse, IntelliJ,
or Visual Studio Code. This environment helps us write, compile, and run
i
Java programs efficiently.
fin
3. First Program: Hello World
In
The first program every programmer writes is the famous “Hello World”. It
prints the text to the screen. Below is the simplest Java program:
1 // Define a public class named HelloWorld
2 public class HelloWorld {
3
4 // Main method : program entry point
5 public static void main ( String [] args ) {
6 // Print text to the console
7 System . out . println ( " Hello , world ! " ) ;
8 }
9 }
1
Listing 1: HelloWorld.java
Explanation:
• public class HelloWorld: Defines a class named HelloWorld.
• public static void main(String[] args): The main method where
program execution begins.
• System.out.println(): Prints the string inside parentheses to the
console.
s
Hello, world!
4. Variables and Data Types
t or
Tu
Variables store data that your program uses. Java is statically typed, mean-
ing each variable must have a type, like int for integers, double for decimals,
char for single characters, boolean for true/false, and String for text.
ty
1 // Class demonstrating variables and printing them
2 public class VariableExample {
i
public static void main ( String [] args ) {
fin
4 int age = 20; // integer
variable
5 double price = 99.99; // floating
In
point number
6 char grade = ’A ’; // single
character
7 boolean passed = true ; // true or
false
8 String name = " Alice " ; // text string
9
10 System . out . println ( " Name : " + name ) ;
11 System . out . println ( " Age : " + age ) ;
12 System . out . println ( " Price : $ " + price ) ;
13 System . out . println ( " Grade : " + grade ) ;
14 System . out . println ( " Passed : " + passed ) ;
15 }
16 }
2
Listing 2: VariableExample.java
Explanation: Each variable is declared with a type and initialized with a
value. The + operator concatenates (joins) strings and variables for printing.
Name: AliceAge: 20Price: $99.99Grade: APassed: true
5. Simple Arithmetic Operations
s
or
You can use variables to perform arithmetic operations like addition, sub-
traction, multiplication, and division.
1 // Program showing basic arithmetic t
Tu
2 public class Ar ithmet icExam ple {
3 public static void main ( String [] args ) {
4 int a = 15;
5 int b = 4;
6
ty
7 int sum = a + b ; // addition
8 int diff = a - b ; // subtraction
int prod = a * b ; // multiplication
i
9
fin
10 int quot = a / b ; // integer division
11 int mod = a % b ; // remainder ( modulus )
12
13 System . out . println ( " Sum : " + sum ) ;
In
14 System . out . println ( " Difference : " + diff ) ;
15 System . out . println ( " Product : " + prod ) ;
16 System . out . println ( " Quotient : " + quot ) ;
17 System . out . println ( " Remainder : " + mod ) ;
18 }
19 }
Listing 3: ArithmeticExample.java
Explanation: The program performs arithmetic and stores the results in
variables, then prints them.
Sum: 19Difference: 11Product: 60Quotient: 3Remainder: 3
3
Notes
• Java programs are made up of classes.
• The main method is the starting point.
• Use variables to store data of specific types.
• Use System.out.println() to print output.
• Java supports arithmetic and string operations.
Next lesson: Learn about control flow statements like if, for, and while
loops!
s
t or
Tu
i ty
fin
In