SlideShare a Scribd company logo
9801 200 111 | info@neosphere.com.np
9801 200 111 | info@neosphere.com.np
Application Fundamentals
9801 200 111 | info@neosphere.com.np
Types of Software
https://whatis.techtarget.com/definition/system-software
System Software
https://searchsoftwarequality.techtarget.com/definition/application
Application Software
Application
Software
Games, MS Office,
Browser etc.
System Software
Operating System,
Tools & Utilities,
Compilers
Hardware
Disk, CPU, RAM
9801 200 111 | info@neosphere.com.np
Types of
Application
Web Desktop Mobile
▪ Java
▪ .NET
▪ PHP
▪ Python
▪ Others
▪ Java
▪ .NET
▪ Python
▪ Others
▪ Android (java, Kotlin)
▪ iOS (Swift)
L
a
n
g
u
a
g
e
s
9801 200 111 | info@neosphere.com.np
We require
database to
store data
9801 200 111 | info@neosphere.com.np
Driver
We require JDBC (Java
Database Connectivity) Driver
to perform operation with
Databases
9801 200 111 | info@neosphere.com.np
It is intended to let application developers "write once,
run anywhere" (WORA), meaning that compiled Java
code can run on all platforms that support Java without
the need for recompilation.
9801 200 111 | info@neosphere.com.np
Java is Object Oriented
9801 200 111 | info@neosphere.com.np
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compile: javac filename.java
Run: java ClassName
9801 200 111 | info@neosphere.com.np
javac HelloWorld.java
Compile:
java HelloWorld
Run:
9801 200 111 | info@neosphere.com.np
This is the entry point of our Java program. the main method has to have this
exact signature in order to be able to run our program.
public again means that anyone can access it.
static means that you can run this method without creating an instance of
HelloWorld.
void means that this method doesn't return any value.
main is the name of the method.
9801 200 111 | info@neosphere.com.np
• The HelloWorld.java file is known as source code file.
• It is compiled by invoking tool named javac.exe, which compiles the source code into a .class file.
• The .class file contains the bytecode which is interpreted by java.exe tool.
• java.exe interprets the bytecode and runs the program.
9801 200 111 | info@neosphere.com.np
Package in Java
A package in Java is used to group related classes. Think of it as a folder
in a file directory. We use packages to avoid name conflicts, and to
write a better maintainable code. Packages are divided into two
categories:
9801 200 111 | info@neosphere.com.np
Java Packages
• Built-in Packages (packages from the Java API)
• User-defined Packages (create your own packages)
9801 200 111 | info@neosphere.com.np
Importing packages
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
9801 200 111 | info@neosphere.com.np
Scanner Class in Java
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
9801 200 111 | info@neosphere.com.np
To import a whole package,
end the statement with an asterisk sign (*).
import java.util.*;
9801 200 111 | info@neosphere.com.np
Methods in Java
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
9801 200 111 | info@neosphere.com.np
modifier returnType nameOfMethod (Parameter List) {
// method body
}
9801 200 111 | info@neosphere.com.np
Variables and Types
Although Java is object oriented, not all types are objects. It is built on top of basic variable types
called primitives.
List of all primitives in Java:
byte (number, 1 byte)
short (number, 2 bytes)
int (number, 4 bytes)
long (number, 8 bytes)
float (float number, 4 bytes)
double (float number, 8 bytes)
char (a character, 2 bytes)
boolean (true or false, 1 byte)
9801 200 111 | info@neosphere.com.np
Java is a strong typed language, which means variables need to
be defined before we use them.
int myNumber;
myNumber = 5;
9801 200 111 | info@neosphere.com.np
String is not a primitive. It's a real type, but Java has special treatment for String.
// Create a string with a constructor
String s1 = new String("Who let the dogs out?");
// Just using "" creates a string, so no need to write it the previous way.
String s2 = "Who who who who!";
// Java defined the operator + on strings to concatenate:
String s3 = s1 + s2;
int num = 5;
String s = "I have " + num + " cookies"; //Be sure not to use ""
with primitives.
Can also concat string to primitives:
9801 200 111 | info@neosphere.com.np
Operator
Operator in java is a symbol that is used to perform operations
9801 200 111 | info@neosphere.com.np
Operator Precedence
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
9801 200 111 | info@neosphere.com.np
Simple Assignment Operator
=
9801 200 111 | info@neosphere.com.np
Arithmetic Operators
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
9801 200 111 | info@neosphere.com.np
Unary Operators
+ Unary plus operator; indicates positive value (numbers are
positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean
9801 200 111 | info@neosphere.com.np
Equality and Relational Operators
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
9801 200 111 | info@neosphere.com.np
&& Conditional-AND
|| Conditional-OR
?: Ternary (shorthand for if-then-else statement)
9801 200 111 | info@neosphere.com.np
Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
9801 200 111 | info@neosphere.com.np
Conditionals
Java uses boolean variables to evaluate conditions. The boolean values true
and false are returned when an expression is compared or evaluated. For
example:
int a = 4;
boolean b = a == 4;
if (b) {
System.out.println("It's true!");
}
9801 200 111 | info@neosphere.com.np
The if-then Statement
The if-then statement is the most basic of all the control flow statements
void applyBrakes() {
// the "if" clause: bicycle must be moving
if (isMoving){
// the "then" clause: decrease current speed
currentSpeed--;
}
}
9801 200 111 | info@neosphere.com.np
If this test evaluates to false (meaning that the bicycle is not in motion),
control jumps to the end of the if-then statement.
void applyBrakes() {
// the "if" clause: bicycle must be moving
if (isMoving){
// the "then" clause: decrease current speed
currentSpeed--;
}
}
9801 200 111 | info@neosphere.com.np
The if-then-else Statement
void applyBrakes() {
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
}
9801 200 111 | info@neosphere.com.np
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
9801 200 111 | info@neosphere.com.np
The switch Statement
A switch works with the byte, short, char, and int primitive
data types.
It also works with enumerated types (discussed in Enum
Types), the String class, and a few special classes that wrap
certain primitive types: Character, Byte, Short, and Integer
9801 200 111 | info@neosphere.com.np
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
9801 200 111 | info@neosphere.com.np
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
9801 200 111 | info@neosphere.com.np
• The body of a switch statement is known as a switch block.
• A statement in the switch block can be labeled with one or
more case or default labels.
• The switch statement evaluates its expression, then executes
all statements that follow the matching case label.
9801 200 111 | info@neosphere.com.np
The while and do-while Statements
The while statement continually executes a block of statements while a
particular condition is true. Its syntax can be expressed as:
while (expression) {
statement(s)
}
9801 200 111 | info@neosphere.com.np
The while statement evaluates expression, which must return a
boolean value.
9801 200 111 | info@neosphere.com.np
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
9801 200 111 | info@neosphere.com.np
Do…while
class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}
9801 200 111 | info@neosphere.com.np
The for Statement
The for statement provides a compact way to iterate over a range of
values. Programmers often refer to it as the "for loop" because of the
way in which it repeatedly loops until a particular condition is satisfied.
9801 200 111 | info@neosphere.com.np
for (initialization; termination; increment) {
statement(s)
}
9801 200 111 | info@neosphere.com.np
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
9801 200 111 | info@neosphere.com.np
Enhanced for
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}

More Related Content

What's hot (20)

PPTX
Python workshop session 6
Abdul Haseeb
 
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
DOCX
C interview question answer 2
Amit Kapoor
 
DOCX
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
DOCX
Data Structure in C (Lab Programs)
Saket Pathak
 
PPTX
OPERATOR IN PYTHON-PART2
vikram mahendra
 
PPTX
Functions in C
Princy Nelson
 
PDF
C aptitude scribd
Amit Kapoor
 
PDF
C program
Komal Singh
 
PPTX
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
PDF
Data structure lab manual
nikshaikh786
 
PPT
Arrays
Saranya saran
 
PPT
Unit 5 Foc
JAYA
 
PPTX
Unit 3
GOWSIKRAJAP
 
PDF
8 arrays and pointers
MomenMostafa
 
PDF
C programs
Vikram Nandini
 
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
PDF
Functions
Swarup Kumar Boro
 
PPTX
Advance python programming
Jagdish Chavan
 
Python workshop session 6
Abdul Haseeb
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
C interview question answer 2
Amit Kapoor
 
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Data Structure in C (Lab Programs)
Saket Pathak
 
OPERATOR IN PYTHON-PART2
vikram mahendra
 
Functions in C
Princy Nelson
 
C aptitude scribd
Amit Kapoor
 
C program
Komal Singh
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
Data structure lab manual
nikshaikh786
 
Unit 5 Foc
JAYA
 
Unit 3
GOWSIKRAJAP
 
8 arrays and pointers
MomenMostafa
 
C programs
Vikram Nandini
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
Advance python programming
Jagdish Chavan
 

Similar to Java Programming Workshop (20)

PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
PPTX
Java platform
Visithan
 
PPTX
Java class 1
Edureka!
 
PPT
Introduction to Java Programming Part 2
university of education,Lahore
 
PPTX
Java fundamentals
HCMUTE
 
PPTX
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
PPTX
Introduction to Java
Ashita Agrawal
 
PDF
Bt0074 oops with java
Techglyphs
 
PPTX
Android webinar class_java_review
Edureka!
 
PPTX
Fundamental programming structures in java
Shashwat Shriparv
 
PDF
data types.pdf
HarshithaGowda914171
 
PPSX
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
PPT
java tutorial 2
Tushar Desarda
 
PPTX
Learning core java
Abhay Bharti
 
PPTX
MODULE_2_Operators.pptx
VeerannaKotagi1
 
PPTX
Javascript
Sheldon Abraham
 
PPT
Chapter 2&3 (java fundamentals and Control Structures).ppt
henokmetaferia1
 
PPTX
JAVA programming language made easy.pptx
Sunila31
 
PPTX
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Java platform
Visithan
 
Java class 1
Edureka!
 
Introduction to Java Programming Part 2
university of education,Lahore
 
Java fundamentals
HCMUTE
 
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
Introduction to Java
Ashita Agrawal
 
Bt0074 oops with java
Techglyphs
 
Android webinar class_java_review
Edureka!
 
Fundamental programming structures in java
Shashwat Shriparv
 
data types.pdf
HarshithaGowda914171
 
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
java tutorial 2
Tushar Desarda
 
Learning core java
Abhay Bharti
 
MODULE_2_Operators.pptx
VeerannaKotagi1
 
Javascript
Sheldon Abraham
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
henokmetaferia1
 
JAVA programming language made easy.pptx
Sunila31
 
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Ad

More from neosphere (7)

PDF
Digital Marketing in Business
neosphere
 
PDF
CCNP Presentation- neosphere
neosphere
 
PDF
Digital marketing
neosphere
 
PDF
Career in Software Development
neosphere
 
PDF
Building career in Information Technology
neosphere
 
PPTX
Career as network specialist
neosphere
 
PPTX
Career in Ethical Hacking
neosphere
 
Digital Marketing in Business
neosphere
 
CCNP Presentation- neosphere
neosphere
 
Digital marketing
neosphere
 
Career in Software Development
neosphere
 
Building career in Information Technology
neosphere
 
Career as network specialist
neosphere
 
Career in Ethical Hacking
neosphere
 
Ad

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 

Java Programming Workshop

  • 2. 9801 200 111 | [email protected] Application Fundamentals
  • 3. 9801 200 111 | [email protected] Types of Software https://whatis.techtarget.com/definition/system-software System Software https://searchsoftwarequality.techtarget.com/definition/application Application Software Application Software Games, MS Office, Browser etc. System Software Operating System, Tools & Utilities, Compilers Hardware Disk, CPU, RAM
  • 4. 9801 200 111 | [email protected] Types of Application Web Desktop Mobile ▪ Java ▪ .NET ▪ PHP ▪ Python ▪ Others ▪ Java ▪ .NET ▪ Python ▪ Others ▪ Android (java, Kotlin) ▪ iOS (Swift) L a n g u a g e s
  • 5. 9801 200 111 | [email protected] We require database to store data
  • 6. 9801 200 111 | [email protected] Driver We require JDBC (Java Database Connectivity) Driver to perform operation with Databases
  • 7. 9801 200 111 | [email protected] It is intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
  • 8. 9801 200 111 | [email protected] Java is Object Oriented
  • 9. 9801 200 111 | [email protected] public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } Compile: javac filename.java Run: java ClassName
  • 10. 9801 200 111 | [email protected] javac HelloWorld.java Compile: java HelloWorld Run:
  • 11. 9801 200 111 | [email protected] This is the entry point of our Java program. the main method has to have this exact signature in order to be able to run our program. public again means that anyone can access it. static means that you can run this method without creating an instance of HelloWorld. void means that this method doesn't return any value. main is the name of the method.
  • 12. 9801 200 111 | [email protected] • The HelloWorld.java file is known as source code file. • It is compiled by invoking tool named javac.exe, which compiles the source code into a .class file. • The .class file contains the bytecode which is interpreted by java.exe tool. • java.exe interprets the bytecode and runs the program.
  • 13. 9801 200 111 | [email protected] Package in Java A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:
  • 14. 9801 200 111 | [email protected] Java Packages • Built-in Packages (packages from the Java API) • User-defined Packages (create your own packages)
  • 15. 9801 200 111 | [email protected] Importing packages import package.name.Class; // Import a single class import package.name.*; // Import the whole package
  • 16. 9801 200 111 | [email protected] Scanner Class in Java import java.util.Scanner; class MyClass { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); System.out.println("Enter username"); String userName = myObj.nextLine(); System.out.println("Username is: " + userName); } }
  • 17. 9801 200 111 | [email protected] To import a whole package, end the statement with an asterisk sign (*). import java.util.*;
  • 18. 9801 200 111 | [email protected] Methods in Java • A method is a block of code which only runs when it is called. • You can pass data, known as parameters, into a method.
  • 19. 9801 200 111 | [email protected] modifier returnType nameOfMethod (Parameter List) { // method body }
  • 20. 9801 200 111 | [email protected] Variables and Types Although Java is object oriented, not all types are objects. It is built on top of basic variable types called primitives. List of all primitives in Java: byte (number, 1 byte) short (number, 2 bytes) int (number, 4 bytes) long (number, 8 bytes) float (float number, 4 bytes) double (float number, 8 bytes) char (a character, 2 bytes) boolean (true or false, 1 byte)
  • 21. 9801 200 111 | [email protected] Java is a strong typed language, which means variables need to be defined before we use them. int myNumber; myNumber = 5;
  • 22. 9801 200 111 | [email protected] String is not a primitive. It's a real type, but Java has special treatment for String. // Create a string with a constructor String s1 = new String("Who let the dogs out?"); // Just using "" creates a string, so no need to write it the previous way. String s2 = "Who who who who!"; // Java defined the operator + on strings to concatenate: String s3 = s1 + s2; int num = 5; String s = "I have " + num + " cookies"; //Be sure not to use "" with primitives. Can also concat string to primitives:
  • 23. 9801 200 111 | [email protected] Operator Operator in java is a symbol that is used to perform operations
  • 24. 9801 200 111 | [email protected] Operator Precedence Operators Precedence postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
  • 25. 9801 200 111 | [email protected] Simple Assignment Operator =
  • 26. 9801 200 111 | [email protected] Arithmetic Operators + Additive operator (also used for String concatenation) - Subtraction operator * Multiplication operator / Division operator % Remainder operator
  • 27. 9801 200 111 | [email protected] Unary Operators + Unary plus operator; indicates positive value (numbers are positive without this, however) - Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ! Logical complement operator; inverts the value of a boolean
  • 28. 9801 200 111 | [email protected] Equality and Relational Operators == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to
  • 29. 9801 200 111 | [email protected] && Conditional-AND || Conditional-OR ?: Ternary (shorthand for if-then-else statement)
  • 30. 9801 200 111 | [email protected] Bitwise and Bit Shift Operators ~ Unary bitwise complement << Signed left shift >> Signed right shift >>> Unsigned right shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR
  • 31. 9801 200 111 | [email protected] Conditionals Java uses boolean variables to evaluate conditions. The boolean values true and false are returned when an expression is compared or evaluated. For example: int a = 4; boolean b = a == 4; if (b) { System.out.println("It's true!"); }
  • 32. 9801 200 111 | [email protected] The if-then Statement The if-then statement is the most basic of all the control flow statements void applyBrakes() { // the "if" clause: bicycle must be moving if (isMoving){ // the "then" clause: decrease current speed currentSpeed--; } }
  • 33. 9801 200 111 | [email protected] If this test evaluates to false (meaning that the bicycle is not in motion), control jumps to the end of the if-then statement. void applyBrakes() { // the "if" clause: bicycle must be moving if (isMoving){ // the "then" clause: decrease current speed currentSpeed--; } }
  • 34. 9801 200 111 | [email protected] The if-then-else Statement void applyBrakes() { if (isMoving) { currentSpeed--; } else { System.err.println("The bicycle has already stopped!"); } }
  • 35. 9801 200 111 | [email protected] class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
  • 36. 9801 200 111 | [email protected] The switch Statement A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer
  • 37. 9801 200 111 | [email protected] public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break;
  • 38. 9801 200 111 | [email protected] case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); }
  • 39. 9801 200 111 | [email protected] • The body of a switch statement is known as a switch block. • A statement in the switch block can be labeled with one or more case or default labels. • The switch statement evaluates its expression, then executes all statements that follow the matching case label.
  • 40. 9801 200 111 | [email protected] The while and do-while Statements The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as: while (expression) { statement(s) }
  • 41. 9801 200 111 | [email protected] The while statement evaluates expression, which must return a boolean value.
  • 42. 9801 200 111 | [email protected] class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }
  • 43. 9801 200 111 | [email protected] Do…while class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }
  • 44. 9801 200 111 | [email protected] The for Statement The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied.
  • 45. 9801 200 111 | [email protected] for (initialization; termination; increment) { statement(s) }
  • 46. 9801 200 111 | [email protected] class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }
  • 47. 9801 200 111 | [email protected] Enhanced for class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } }