Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Introduction To Java Programming and Operators

The document provides information about the Java programming language: 1. Java was developed by Sun Microsystems in 1991 and later acquired by Oracle Corporation. It was created by James Gosling and Patrick Naughton as a simple yet robust programming language. 2. The document discusses the history and development of Java, its key features such as being platform independent and object oriented, and basic Java terminology like the Java Virtual Machine, bytecode, and the Java Development Kit. 3. A sample "Hello World" Java program is presented to demonstrate how a basic Java program works, including class and method definitions.

Uploaded by

Jozef Portillo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Introduction To Java Programming and Operators

The document provides information about the Java programming language: 1. Java was developed by Sun Microsystems in 1991 and later acquired by Oracle Corporation. It was created by James Gosling and Patrick Naughton as a simple yet robust programming language. 2. The document discusses the history and development of Java, its key features such as being platform independent and object oriented, and basic Java terminology like the Java Virtual Machine, bytecode, and the Java Development Kit. 3. A sample "Hello World" Java program is presented to demonstrate how a basic Java program works, including class and method definitions.

Uploaded by

Jozef Portillo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

PROGRAMMING 1

(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

James Gosling is considered the


original developer of Java language. He
is also known as father of Java. He
started on this in early 1990s. He
originally named it Greentalk and the
extension was .gt.
James Gosling

HISTORY
Programming Language renamed

When Green team (James


Gosling, Mike Sheridan and
Patrick Naughton) started
working on it, it was renamed
to Oak. Oak is a tree name,
which is considered a symbol
of strength and is a national
James Gosling Mike Sheridan Patrick Naughton tree in many countries such as
U.S.A., France, Germany,
Romania, etc.

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

Original and First Version of Java:


• JDK first version alpha & beta was released in
1995 by Sun Microsystems.

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

2004-2017 2018 2019


1995 1996 1997 1998 2000-2002 2020 2021 2022

JDK Alpha J2SE 1.3 & 4


JDK 1.1 JAVA SE 11 JAVA SE 14 & SE 15 JAVA SE 18
& Beta (May 2000 &
(February) (September) (March & (March)
September)
02
FEATURES
Main Features of JAVA

Platform Multi-
Secure Object- Robust
Independent Simple Distributed threading Portable
Oriented
03
PHASES
Phases of Program Execution

Step 1 Step 2 Step 3


Write the Program Compile the Program Run the Program

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

Libraries and Libraries and


compiled class compiled class Compiler,
files. files. Debugger and
Other
JVM rt.jar JVM rt.jar Development
Tools

JRE JRE
Java Development Kit (JDK)
Difference between JDK, JRE & JVM
Sample Java Program:

package SampleProgram;

public class helloWorld {


public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Output: Hello World!


How the First Java Program works?
package SampleProgram;

This is the first line of our java


public class helloWorld { program. Every java application
must have at least one class
public static void main(String[] args) { definition that consists of class
System.out.println("Hello World!"); keyword followed by class name.
When I say keyword, it means that
} it should not be changed, we should
use it as it is. However the class
}
name can be anything.
How the First Java Program works?
public:
package SampleProgram;
This makes the main method public
that means we can call the method
public class helloWorld { from outside the class
public static void main(String[] args) {
static:
System.out.println("Hello World!");
} We do not need to create object for
static methods to run. They can run
} itself.
How the Hello World Java Program works?
void:
package SampleProgram; It does not return anything.

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):

} Used for command line arguments


that are passed as strings. We will
cover that in a separate post.
How the Hello World Java Program works?
package SampleProgram;

public class helloWorld {


public static void main(String[] args) {
System.out.println("Hello World!");
}
This method prints the string inside
} the double quotes on the console
and inserts a newline after.
Structure of a Java Program

Let's see which elements are included in the structure


of a Java program. A typical structure of a Java
program contains the following elements:
• Documentation Section
• Package Declaration
• Import Statements
• Interface Section
• Class Definition
• Class Variables and Constants
• Main Method Class
• Methods and Behaviors
Documentation Section
It includes basic information about a Java program. The information includes the author's name, date of
creation, version, program name, company name, and description of the program. It improves the
readability of the program. To write the statements in the documentation section, we use comments. The
comments may be single-line, multi-line, and documentation comments.

• 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:

data_type variable_name = value;

Note: Value is optional because in Java, you can declare


the variable first and then later assign the value to it.
How to Declare a variable in Java?
Ex:
int num;

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;

Or we can do it like this:


char ch;
int number;
Variables naming convention in Java
• Variables naming cannot contain white spaces.

Example:
int num ber = 100;

It is invalid because the variable name has space in it.

• Variable name can begin with special characters such as $


and _
Variables naming convention in Java
• Variable name can begin with special characters such as $
and _

Example:

String _fname; String _fname = "Ellen";


char $middleInitial; char $middleInitial = 'M';
Variables naming convention in Java
3. As per the java coding standards the variable name should begin
with a lower case letter.
Example:
int number;

For lengthy variables names that has more than one words do it like
this:
int smallNumber;
int bigNumber;

(start the second word with capital letter).


Variables naming convention in Java
4. Variable names are case sensitive in Java.
Example:

String _fname = "Ellen";

System.out.println("Firstname: " + _Fname);

It will return an error statement since variables “_fname” and


“_Fname” are different.
Data Types in Java
Data type defines the values that a variable can take, for
example if a variable has int data type, it can only take
integer values.

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.

float and double are used for fractional numbers.

char is used for storing characters(letters).

boolean data type is used for variables that holds either


true or false.
Primitive Data Types in Java

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

Ex: short num = 45678;


Example using “short” data type
class JavaExample {
public static void main(String[] args) {

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) {

long num = -12332252626L;

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) {

double num = -42937737.9d;


System.out.println(num);
}
}
Output:
-4.29377379E7
Primitive Data Types in Java

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) {

float num = 19.98f;


System.out.println(num);
}
}
Output:
19.98
Primitive Data Types in Java

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

A literal is a fixed value that we assign to a variable in a


Program.

int num = 10; //Here value 10 is a Integer literal.


char initial =‘A’; //Here A is a char literal
Integer Literals in Java

Integer literals are assigned to the variables of data


type byte, short, int and long.

byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals in Java

Float literals Used for data type float and double.


double num1 = 22.4;
float num2 = 22.4f;

Note: Always suffix float value with the “f” else compiler
will consider it as double.
Char and String Literals in Java

Char and String literals Used for char and String


type.

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:

In a+b, the “+” symbol is the operator and a & b are


operands.
Types of Operator in Java
Operators in Java are classified in following five categories:

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;

System.out.println("num1 + num2: " + (num1 + num2) );


System.out.println("num1 - num2: " + (num1 - num2) );
System.out.println("num1 * num2: " + (num1 * num2) );
System.out.println("num1 / num2: " + (num1 / num2) );
System.out.println("num1 % num2: " + (num1 % num2) );
}
}
Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
These are used in Java to assign values to variables.
Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
These are used in Java to assign values to variables.
Example using Assignment Operators
public class AssignmentOperatorDemo {

public static void main(String[] args) {

int num1 = 10;


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 {

public static void main(String[] args) {


int num1=100;
int num2=200;

//minus(-) unary operator


int inverseNum = -num1;
System.out.println("Opposite of num1: "+inverseNum);

//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;

System.out.println("b1 && b2: " + (b1&&b2));


System.out.println("b1 || b2: " + (b1||b2));
System.out.println("!(b1 && b2): " + !(b1&&b2));
}
}
Relational Operators
Relational operators are used to compare two operands. In Java, we
have following relational operators:
Example using Logical Operators
public class LogicalOperatorDemo {

public static void main(String[] args) {


int a = 10;
int b=20;
System.out.println("b1 && b2: " + (b1&&b2));
System.out.println("b1 || b2: " + (b1||b2));
System.out.println("!(b1 && b2): " + !(b1&&b2)); }
}

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: It is a final class defined in the java.lang


package.
• out: It is an instance of PrintStream type and its access
specifiers are public and final
• println(): It is a method of PrintStream class.
Displaying Strings using System.out.println()

public class outputExplained {

public static void main(String[] args) {

System.out.println(“Welcome");
System.out.print(“To”);
System.out.print(“ACLC”);
}
}
Output:
Welcome
To
ACLC
Displaying the variable values using
System.out.println()

public class javaExample{

public static void main(String[] args) {

int a = 10, b = 20;

System.out.print("The product of ");


System.out.println(a + " and " + b + " is: " + a*b);

}
Output:
}
The product of 10 and 20 is: 200
Java Input

• Java provides different ways to get input from the user.


• In order to use the object of , we need to
import java.util.Scanner package.

import java.util.Scanner;
Java Input

• Then, we need to create an object of the Scanner class.


We can use the object to take input from the user.

//create an object of Scanner;


Scanner input = new Scanner(System.in);

//take input from the user


int number = input.nextInt();
Get Integer input from the User
Import java.util.Scanner;
public class javaExample{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

//Getting float input


System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);

// Getting double input


System.out.print("Enter double: ");
Output:
double myDouble = input.nextDouble(); Enter float: 2.345
System.out.println("Double entered = " + myDouble);
Float entered: 2.345
// Getting String input Enter double:-23.4
System.out.print("Enter text: ");
String myString = input.next();
Double entered: -23.4
System.out.println("Text entered = " + myString); Enter text: What’s up!?
}
}
Text entered: What’s up!?

You might also like