You are on page 1of 31

AY 2019-2020

COMPUTER
PROGRAMMING 1
Prepared By:

Prof. Jomariss B. Plan, MSIT, SMRIIT


CCS Faculty
Week 3: INTRODUCTION TO JAVA
Learning Outcomes
At the end of the topic session, the students
should be able to:
 Describe the features of Java technology
such as the Java virtual machine, garbage
collection and code security
 Describe the different phases of a Java
program.
 Identify java literals and data types.
 Write the simplest java program.
History of Java
 Java was originally designed for interactive
television, but it was too advanced technology
for the digital cable television industry at the
time.
 Green Team- initiated this project to develop a
language for digital devices.
 James Gosling, Mike Sheridan, and Patrick
Naughton (June, 1991) of Sun Microsystems.
History of Java
 Firstly, it was called “Greentalk” by James
Gosling.
 After that it was called “Oak” and was
developed as part of the Green Project.
 In 1995, Oak was renamed as “Java”.
 Currently, Java is used in internet
programming, mobile devices, e-business
solutions, etc.
Java Technology:
An Application and Runtime Environment
Java technology applications are typically general-purpose
programs that run on any machine where the Java runtime
environment (JRE) is installed. There are two main deployment
environments:
1.The JRE supplied by the Java 2 Software Development Kit
(SDK) contains the complete set of class files for all the Java
technology packages, which includes basic language classes,
GUI component classes, and so on.
2.The other main deployment environment is on your web
browser. Most commercial browsers supply a Java technology
interpreter and runtime environment.
Phases of a Java Program
 The following figure describes the process of compiling and
executing a Java program
Phases of a Java Program
Java Features
Java Virtual Machine
- an imaginary machine that is implemented
by emulating software on a real machine
and provides the hardware platform
specifications to which you compile all Java
technology code.
- Bytecode - a special machine language that
can be understood by the Java Virtual
Machine (JVM)
Java Features
Garbage Collection
- responsible for freeing any memory that
can be freed. This happens automatically
during the lifetime of the Java program.
Java Features
Code Security
- Code security is attained in Java through
the implementation of its Java Runtime
Environment (JRE).
- JRE runs code compiled for a JVM and
performs class loading (through the class
loader), code verification (through the
bytecode verifier) and finally code
- execution
My First Java Program

public class Hello {


public static void main( String[] args ) {
System.out.println(“Hello world”);
}
}
Coding Guidelines

 Your Java programs should always end with


the .java extension.
 Filenames should match the name of your
public class. So for example, if the name of
your public class is Hello, you should save it in
a file called Hello.java.
 You should write comments in your code
explaining what a certain class does, or what a
certain method do.
Java Identifiers

 Identifiers – are tokens that represent


names of variables, methods, classes,
etc.
Examples of identifiers are:
Hello, main, System, out.
 Java identifiers are case-sensitive. – This
means that the identifier Hello is not the
same as hello.
Java Identifiers

 Identifiers must begin with either a letter,


an underscore “_”, or a dollar sign “$”.
Letters may be lower or upper case.
Subsequent characters may use numbers
0 to 9.
 Identifiers cannot use Java keywords like
class, public, void, etc.
Java Keywords

 Keywords are predefined identifiers


reserved by Java for a specific purpose.
 You cannot use keywords as names for
your variables, classes, methods ... etc.
 The next line contains the list of the
Java Keywords.
Java Keywords
Java Literals

 Literals are tokens that do not change or


are constant.
 The different types of literals in Java are:
– Integer Literals
– Floating-Point Literals
– Boolean Literals
– Character Literals
– String Literals
Primitive Data Types

 The Java programming language defines


eight primitive data types.
– boolean (for logical)
– char (for textual)
– byte
– short
– int – long (integral)
– double
– float (floating point).
Primitive Data Types: Boolean

 A boolean data type represents two states:


true and false.

An example is, boolean result = true;

The example shown above, declares a


variable named result as boolean type and
assigns it a value of true.
Primitive Data Types: Char

 A character data type (char), represents a single


Unicode character.
It must have its literal enclosed in single
quotes(’ ’).
To represent special characters like ' (single
quotes) or " (double quotes), use the escape
character \.
For example, '\'' //for single quotes '\"' //for
double quotes
Primitive Data Types: String

 A String represents a data type that


contains multiple characters.
 It has its literal enclosed in double
quotes(“”).
For example,
String message=“Hello world!”;
Primitive Data Types: Integral
Primitive Data Types: Float and Double
Variables

 A variable is an item of data used to store the


state of objects.
 A variable has a:
– data type
The data type indicates the type of value
that the variable can hold.
– name
The variable name must follow rules for
identifiers.
Declaring and Initializing Variables

 Declare a variable as follows:


<data type> <name> [=initial value];

Note: Values enclosed in <> are required


values, while those values in [] are
optional.
Example 1
Example 2
public class PrintTypes {
public static void main (String[ ] args) {
boolean b = false;
char c = 'R';
byte j = 127;
short k = 32767;
int m = 2147483647;
long n = 9223372036854775807L;
float x = 3.1416F;
double y = 3.141592653589793238;
System.out.print("\n b = " + b);
System.out.print("\n c = " + c);
System.out.print("\n j = " + j);
System.out.print("\n k = " + k);
System.out.print("\n m = " + m);
System.out.print("\n n = " + n);
System.out.print("\n x = " + x);
System.out.print("\n y = " + y);
}}
Example 2 OUTPUT
b = false
c=R
j = 127
k = 32767
m = 2147483647
n = 92233720368547807
x = 3.1416
y = 3.14159265389738
Example 3
public class Shirt {
public static void main ( String args[ ] ) {
int shirtID = 0;
String description = " –description required– ";
char colorcode = 'U';
double price = 0.0;
int quantityInStock = 0;
System.out.println ("Shirt ID: " + shirtID);
System.out.println ("Shirt description: " + description);
System.out.println ("Color Code: " + colorcode);
System.out.println ("Shirt price: " + price);
System.out.println ("Quantity in stock: " +
quantityInStock);
}
}
REFERENCES:
https://javabeginnerstutorial.com/core-java-tutorial/history-
of-java
http://geeksforgeeks.org/literals-in-java/amp/

You might also like