Java Programming
Presentation
JAVA KICKOFF
WORKSHOP
Introduction, Variables, Operations, Loops, and
OOP Concepts
Introduction to Java
High-level, Object-Oriented Language
Platform Independent (Write Once, Run Anywhere)
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!
Java compiltion
C:\Users\Your Name>javac Main.java
C:\Users\Your Name>java Main
Basic Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Variables and Data Types
Variables store data: int, double, char, boolean, String
Example:
int age = 20;
double price = 99.99;
char grade = 'A';
boolean isPassed = true;
public Main {
public static void main(String[] args) {
int age = 20; // Integer
double price = 99.99; // Floating-point
char grade = 'A'; // Character
boolean isPassed = true; // Boolean
String name = "Java"; // String
System.out.println("Age: " + age);
System.out.println("Price: " + price);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPassed);
System.out.println("Language: " + name);
}
}
Operations in Java
Arithmetic Operator:
Example :
public Main {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
Assignment Operator:
Example:
public class Main {
public static void main(String[] args) {
int x = 10;
x += 5;
System.out.println(x);
}
}
Comparison Operator:
Example:
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x > y); // returns true, because 5
is higher than 3
}
}
Math Functions in Java
The Java Math class has many methods that allows you to
perform mathematical tasks on numbers.
Math.max(x,y)
The Math.max(x,y) method can be used to find the highest value of x and y:
Example:t
Math.max(5, 10);
Math.min(x,y)
The Math.min(x,y) method can be used to find the lowest value of x and y:
Example
Math.min(5, 10);
Math.sqrt(x)
The Math.sqrt(x) method returns the square root of x:
Example
Math.sqrt(64);a
Math.abs(x)
The Math.abs(x) method returns the absolute (positive) value of x:
Example
Math.abs(-4.7);
Random Numbers
Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):
Example
Math.random();
To get more control over the random number, for example, if you only want a random number between 0
and 100, you can use the following formula:
Example
int randomNum = (int)(Math.random() * 101); // 0 to 100
For Loop
When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while loop:
Syntax:
for (statement 1; statement 2; statement 3) { // code block to be executed }
Example
for (int i = 0; i <= 10; i = i + 2) { System.out.println(i); }
Break:
The break statement can also be used to jump out of a loop.
This example stops the loop when i is equal to 4:
for (int i = 0; i < 10; i++)
{
if (i == 4)
break;
}
System.out.println(i);
}
public class Main {
public static void main(String[] args) {
System.out.println("Using continue:");
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
System.out.print(i);
}
System.out.println("\n\nUsing break:");
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.print(i );
}
}
}
Object-Oriented
Programming (OOP)
Encapsulation: Wrapping data and methods
Inheritance: Acquiring properties of parent class
Polymorphism: One method, multiple behaviors
Attributes: Variables inside a class
Encapsulation Example
class Person {
private String name;
public void setName(String n) { name = n; }
public String getName() { return name; }
}
Inheritance Example
class Animal { void sound()
{ System.out.println("Sound"); } }
class Dog extends Animal { void bark()
{ System.out.println("Bark"); } }
Polymorphism Example
class Animal { void makeSound()
{ System.out.println("Sound"); } }
class Dog extends Animal { void makeSound()
{ System.out.println("Bark"); } }
Conclusion
- Java is a powerful OOP language
- Understanding variables, loops, and OOP
concepts helps in building robust applications
- Practice is key to mastering Java!