KEMBAR78
Java 2 | PDF | Java (Programming Language) | Data Type
0% found this document useful (0 votes)
6 views26 pages

Java 2

Java programming for beginners

Uploaded by

karelbissa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Java 2

Java programming for beginners

Uploaded by

karelbissa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to Java Programming

Lecturer: MUGHE GODLOVE


Introduction to Java
• Java is a high-level, object-oriented programming
language.
• Platform-independent via Java Virtual Machine (JVM).
• Commonly used in mobile apps, web development,
enterprise applications, and embedded systems.
• Why Learn Java?
• Secure, fast, and powerful.
• Rich API and large community support.
• Versatile and used across industries.
Setting Up Java
• Installation
• Download JDK from the official Oracle
website.
• Set JAVA_HOME environment variable.
• Verify installation using: java -version, javac –
version
• Using the jshell prompt
Writing First Java Program
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}

• Compile: javac Main.java


• Run: java Main
• Javac:Compiler
• Java: Java virtuel machine
Java Syntax
• Case-sensitive.
• Code inside classes.
• The main method:
« public static void main(String[] args){} »
• Semicolon ; ends a statement.
• Code blocks within {}.
Variables and Data Types
• Variables are locations in memory in which values
can be stored. They have a name, a type, and a
value.
Declaring Varaible:
• To use any variable in a Java program, you must
first declare it. Variable declarations consist of a
type and a variable name:
int myAge;
String myName;
boolean isTired;
Variables and Data Types
• Primitive Types: int, float, double, char,
boolean, long, short, byte
• Reference Types: String, Arrays, Objects
• Variable Declaration:
int age = 20;
String name = "John";
• Constants: final double PI = 3.14;
Variables and Data Types
Type Size Range
Byte 8 bits -128 127
Short 16 bits –-32,768 to 32,767
Int 32 bits –2,147,483,648 to
2,147,483,647
Long 64 bits –9223372036854775808
to 9223372036854775807

Float: Floating-point numbers are used for numbers with a decimal part.
Char: The char type is used for individual characters.
Boolean:the boolean type can have one of two values, true or false.
Comments and Literals
• Comments are important for code documention
• /* and */ : surround multiline comments
• Double-slashes (//) can be used for a single line
of comment. All the text up to the end of the
line is ignored:
• Literals are used to indicate simple values in
your Java programs. i.e what you type is what
you get
Operators
• Arithmetic Operators: +, -, *, /, %
• Comparison Operators: ==, !=, >, <, >=, <=
• Logical Operators: &&, ||, !
• Assignment Operators: =, +=, -=, *=, /=

Variable assignment
Expression Meaning
X=1 Assigning the value 1 to the variable X
x += y x=x+y
x –= y x =x – y
x++ x=x+1
Pre and Post Incrementation
y = x++;
y = ++x;

String Arithmetic
One special expression in Java is the use of the addition operator (+)
to create and concatenate strings.
System.out.println(name + “ is a “ + color “ beetle”);
Control Flow
• If-Else Statement
• Switch Statement
• Loops: for, while, do-while
if Conditionals
• The if conditional, which enables you to execute
different bits of code based on a simple test
if (x < y)
System.out.println(“x is smaller than y”);
else if(x< y)
System.out.println(“x is equal to y”);
else
System.out.println(“y is bigger.”);
The Conditional Operator or Ternary operator is :
An alternative to using the if and else keywords in a conditional statement

test ? trueresult : falseresult


int smaller = x < y ? x : y;
Switch Conditionals
• A common practice in programming in any language is to test a variable against some value, and if it
doesn’t match that value, to test it again against a different value, and if it doesn’t match that one to make
yet another test, and so on.
switch (test) {
case valueOne:
resultOne;
break;
case valueTwo:
resultTwo;
break;
case valueThree:
resultThree;
break;
...
default:
defaultresult;
}
Loops
• The for loop, as in C, repeats a statement or
block of statements some number of times
until a condition is matched.
while loops :
The while loop is used to repeat a statement or block of statements
as long as a particular
condition is true. while loops look like this:
while (condition)
{

bodyOfLoop;

}
do...while Loops

• The do loop is just like a while loop, except that do


executes a given statement or block until a condition
is false.
do
{
bodyOfLoop;
} while (condition);
for Loops
The for loop, as in C, repeats a statement or block of statements some number of
times until a condition is matched. for loops are frequently used for simple iteration in
which you repeat a block of statements a certain number of times and then stop.

for (initialization; test; increment)


{
Leastatements;
}

Breaking Out of Loops:


In all the loops (for, while, and do), the loop ends when the condition
you’re testing for is met.
What happens if something odd occurs within the body of the loop
and you want to exit the loop early? For that, you can use the break
and continue keywords.
Arrays
• One-Dimensional Array: int[] numbers = {1, 2,
3, 4};
• Loop Through Array:
for (int number : numbers)
{
System.out.println(number);
}
Methods
• Define a Method: static void greet() {
System.out.println("Hello!"); }
• Method with Parameters: static int add(int a,
int b) { return a + b; }
• Method Overloading: Same method name,
different parameters.
Object-Oriented Programming
• Classes and Objects
• Inheritance
• Encapsulation: Using private variables and
get/set methods.
• Polymorphism: Method overriding and
dynamic behavior.
Exception Handling
• try { int x = 5 / 0; } catch (ArithmeticException
e) { System.out.println("Can't divide by zero");
} finally { System.out.println("Finished"); }
Java Packages and Access Modifiers
• Packages: package mypackage; import
java.util.Scanner;
• Access Modifiers: public, private, protected,
default
File Handling
• File myFile = new File("example.txt"); if
(myFile.exists()) { System.out.println("File
found"); }
Useful Java Classes
• String
• Math
• Scanner
• Date
Recap and Final Project
• Project Ideas:
• Student Grade System
• To-do List Console App
• Simple Banking System
Resources
• W3Schools Java Tutorial:
https://www.w3schools.com/java/
• Oracle Java Documentation:
https://docs.oracle.com/en/java/

You might also like