Introduction To Java Programming and Operators
Introduction To Java Programming and Operators
(JAVA)
Instructor: Russel R. Guiñares
01
HISTORY
INTRODUCTION
JAVA was developed by Sun
Microsystems Inc in 1991, later acquired
by Oracle Corporation. It was developed
by James Gosling and Patrick Naughton.
It is a simple programming language.
Writing, compiling and debugging a
program is easy in Java. It helps to
create modular programs and reusable
code.
HISTORY
History of Java
Java is an object oriented programming language
developed by Sun Microsystems in early 1990 by
developers James Gosling, Mike Sheridan and Patrick
Naughton. In 1991 James Gosling and his friends
formed a team called Green Team to further work on
this project. The original idea was to develop this
programming language for digital devices such as
television, set-top box, remote etc. Later this idea was
dropped and Java is developed for internet
programming.
Original Developer and Programming Name
HISTORY
Programming Language renamed
HISTORY
Again renamed from Oak to Java in 1995:
• The project is again renamed from Oak to Java in
1995 as Oak was a trademark for other
organization
HISTORY
JAVA VERSION HISTORY
JAVA SE 16 &
JAVA SE 17
JDK 1.0 J2SE 1.2 JAVA SE 5 – JAVA SE 12 (March &
(January) (December) JAVA SE10 & JAVA SE September)
13
Platform Multi-
Secure Object- Robust
Independent Simple Distributed threading Portable
Oriented
03
PHASES
Phases of Program Execution
Compilation of program is
Writing of the program is of done by javac compiler, javac is In third phase, JVM executes
course done by java the primary java compiler the bytecode generated by
programmer like you and me. included in java development compiler. This is called
kit (JDK). It takes java program program run phase.
as input and generates java
bytecode as output.
04
JAVA TERMINOLOGY
JAVA TERMINOLOGY
Java Virtual Machine (JVM)
• Executes the bytecode generated by compiler.
• Each operating system has different JVM, however the
output they produce after execution of bytecode is same
across all operating systems. That is why we call java as
platform independent language.
Bytecode
• javac compiler of JDK compiles the java source code into
bytecode so that it can be executed by JVM. The
bytecode is saved in a .class file by compiler.
JAVA TERMINOLOGY
The Java Virtual machine (JVM) is the virtual machine that
runs on actual machine (your computer) and executes Java
byte code. The JVM doesn’t understand Java source code,
that’s why we need to have javac compiler that compiles
*.java files to obtain *.class files that contain the byte codes
understood by the JVM. JVM makes java portable (write
once, run anywhere). Each operating system has different
JVM, however the output they produce after execution of
byte code is same across all operating systems.
JAVA TERMINOLOGY
Java Development Kit (JDK)
• As the name suggests this is complete java development
kit that includes JRE (Java Runtime Environment),
compilers and various tools like JavaDoc, Java debugger
etc.
• In order to create, compile and run Java program you
would need JDK installed on your computer.
JAVA TERMINOLOGY
Java Runtime Environment (JRE)
• JRE is a part of JDK which means that JDK includes JRE.
When you have JRE installed on your system, you can run
a java program however you won’t be able to compile it.
JRE includes JVM, browser plugins and applets support.
When you only need to run a java program on your
computer, you would only need JRE.
JVM vs JRE vs JDK
JRE JRE
Java Development Kit (JDK)
Difference between JDK, JRE & JVM
Sample Java Program:
package SampleProgram;
main:
public class helloWorld { It is the method name. This is the
public static void main(String[] args) { entry point method from which the
JVM can run your program.
System.out.println("Hello World!");
} (String[] args):
• Single-line Comment: It starts with a pair of forwarding slash (//). For example:
• Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols. For example:
Documentation Comment: It starts with the delimiter (/**) and ends with */. For example:
Package Declaration
The package declaration is optional. In this section, we declare the package name in which the class is placed.
Note that there can be only one package statement in a Java program. It must be defined before any class
and interface declaration. It is necessary because a Java class can be placed in different packages and
directories based on the module they are used. For all these classes package belongs to a single parent
directory. We use the keyword package to declare the package name. For example:
Import Statements
The import statement represents the class stored in the other package. We use the import keyword to import
the class. We use the import statement in two ways, either import a specific class or import all classes of a
particular package. In a Java program, we can use multiple import statements. For example:
Interface Section
It is an optional section. We can create an interface in this section if required. We use the interface keyword
to create an interface. An interface is a slightly different from the class. It contains only constants and method
declarations. Another difference is that it cannot be instantiated. We can use interface in classes by
using the implements keyword. An interface can also be used with other interfaces by using the extends
keyword. For example:
Class Definition
It is vital part of a Java program. It contains information about user-defined methods, variables, and constants.
Every Java program has at least one class that contains the main() method. For example:
Class Variables and Constants
The variables and constants store values of the parameters. It is used during the execution of the program.
We can also decide and define the scope of variables by using the modifiers. It defines the life of the
variables. For example:
Main Method Class
It is essential for all Java programs. Because the execution of all Java programs starts from the main()
method. In other words, it is an entry point of the class. It must be inside the class. Inside the main method,
we create objects and call the methods. We use the following statement to define the main() method:
For example:
05
DATATYPES AND VARIABLES
Variables in Java
A variable is a name which is associated with a value that
can be changed.
Example:
int i=10;
How to Declare a variable in Java?
To declare a variable follow this syntax:
variable
Data type
How to Declare a variable in Java?
Similarly we can assign the values to the variables while
declaring them, like this:
char ch = 'A’;
int number = 100;
Example:
int num ber = 100;
Example:
For lengthy variables names that has more than one words do it like
this:
int smallNumber;
int bigNumber;
Categories:
1. Primitive Data Types
2. Non-Primitive Data Types
Primitive Data Types in Java
In Java, we have eight (8) primitive data types:
• boolean
• char
• byte
• short
• int
• long
• float
• double
Primitive Data Types in Java
byte, short, int and long data types are used for storing
whole numbers.
byte
This can hold whole number between -128 and 127.
Mostly used to save memory and when you are certain that
the numbers would be in the limit specified by byte data
type.
Default size of this data type: 1 byte.
Default value: 0
Example using “byte” data type
class JavaExample {
public static void main(String[] args) {
byte num;
num = 113;
System.out.println(num);
} Output:
}
113
Primitive Data Types in Java
short
This is greater than byte in terms of size and less
than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
short num;
num = 150;
System.out.println(num);
} Output:
}
150
Primitive Data Types in Java
int
Used when short is not large enough to hold the
number, it has a wider range: -2,147,483,648 to
2,147,483,647
Default size: 4 byte
Default value: 0
Primitive Data Types in Java
long
Used when int is not large enough to hold the value,
it has wider range than int data type, ranging from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Default value: 0
Example:
Example using “long” data type
class JavaExample {
public static void main(String[] args) {
System.out.println(num);
}
}
Output:
-12332252626
Primitive Data Types in Java
double
Sufficient for holding 15 decimal digits
size: 8 bytes
Example using “double” data type
class JavaExample {
public static void main(String[] args) {
float
Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
Example using “float” data type
class JavaExample {
public static void main(String[] args) {
boolean
holds either true of false.
Example using “boolean” data type
class JavaExample {
public static void main(String[] args) {
boolean b = false;
System.out.println(b);
}
}
Output:
false
Primitive Data Types in Java
char
holds characters.
size: 2 bytes
Example using “char” data type
class JavaExample {
public static void main(String[] args) {
char ch = 'Z';
System.out.println(ch); }
}
Output:
Z
Literals in Java
Ellen M. Guiñares
Literals in Java
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals in Java
Note: Always suffix float value with the “f” else compiler
will consider it as double.
Char and String Literals in Java
char ch = 'Z';
String str = “Second Session";
06
OPERATORS IN JAVA
Operators in Java
Operator is a symbol that instructs the compiler to perform a specific
action.
Operator and Operand
In any operation, there is an operator and operands.
Example:
1) Arithmetic Operators
2) Assignment Operators
3) Unary Operators
4) Logical Operators
5) Relational operators
Arithmetic Operators
Basic arithmetic operators are: +, -, *, /, %
Example using Arithmetic Operators
Output:
public class ArithmeticOperatorDemo { num1 + num2 = 120
num1 – num2 = 80
public static void main(String[] args) { num1 * num2 = 2000
num1 / num2 = 5
int num1 = 100; num1 % num2 = 0
int num2 = 20;
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1;
System.out.println("+= Output: "+num2);
Output:
num2 -= num1;
System.out.println("-= Output: "+num2); = Output: 10
num2 *= num1; = Output: 20
System.out.println("*= Output: "+num2);
-= Output: 10
num2 /= num1;
System.out.println("/= Output: "+num2);
*= Output: 100
/= Output: 10
num2 %= num1;
System.out.println("%= Output: "+num2); } %= Output: 0
}
Unary Operators
Unary operators are the one that needs a single operand and are
used to increment a value, decrement or negate a value.
Unary Operators
As the name suggests, The Unary operators in Java involve
single operand. Java supports following unary operators:
• Unary minus(-)
• Increment(++)
• Decrement(- -)
• NOT(!)
• Bitwise Complement(~)
Example using Unary Operators
public class UnaryOperatorDemo {
//increment
num1++;
//decrement
num2--; Output:
Opposite of num1: -100
System.out.println("num1++ is: "+num1);
System.out.println("num2-- is: "+num2); num1++ is: 101
} num2-- is: 199
}
Logical Operators
Logical Operators are used to evaluate the outcome of
conditions. There are three logical operators: AND (&&), OR
(||) and NOT (!). The AND and OR operators are used when
multiple conditions are combined and we need to evaluate
the outcome as a whole.
Example using Logical Operators
public class UnaryOperatorDemo { Output:
b1 && b2: false
public static void main(String[] args) { b1 || b2: true
!(b1 && b2): true
boolean b1 = true;
boolean b2 = false;
Output:
b1 && b2: false
b1 || b2: true
!(b1 && b2): true
07
JAVA BASIC INPUT AND OUTPUT
System.out.println()
System.out.println(); or
System.out.print(); or
• In Java, you can simply
System.out.printf();
use either the
following to send
output to standard
System is a class
output (screen).
out is a public static field that
accepts output data
System.out.println()
In Java, we use System.out.println() statement to display a
message, string or data on the screen. It displays the
argument that we pass to it.
System.out.println(“Welcome");
System.out.print(“To”);
System.out.print(“ACLC”);
}
}
Output:
Welcome
To
ACLC
Displaying the variable values using
System.out.println()
}
Output:
}
The product of 10 and 20 is: 200
Java Input
import java.util.Scanner;
Java Input
System.out,print(“Enter an integer:”);
int number = input.nextInt();
System.out,println(“You entered:” + number);
}
} Output:
Enter an integer: 23
You entered 23
Get Integer input from the User
import java.util.Scanner;
public class javaExample{