Вы находитесь на странице: 1из 59

WEEK 4

INTRODUCTION OF JAVA
PROGRAMMING
CC102
FUNDAMENTALS OF PROGRAMMING
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

LEARNING OUTCOMES:
At the end of the session, the students should be able
to:
1. Can describe and discuss Java Story, technology,
features and requirements.
2. Can differentiate applet and application.
3. Can identify the different keyword categorically

CC102-FUNDAMENTALS OF PROGRAMMING 2
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

What is Java?
• was created in 1991
• by James Gosling ,
Mike Sheridan
Patrick Naughton
of Sun Microsystems.
• First, it was called “Greentalk” and the file extension was .gt
• After that it was called Oak, and was developed as part of the Green
Project
• its name was changed to Java because there was already a language
called Oak
CC102-FUNDAMENTALS OF PROGRAMMING 3
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Why Java Programming named "Java"?


• According to James Gosling, "Java was one of the top choices along
with Silk".
• Java was so unique, most of the team members preferred Java than
other names.
• Java is an island of Indonesia where first coffee was produced (called
java coffee).
• In 1995, Time magazine called Java one of the Ten Best Products of
1995.
• JDK 1.0 released in(January 23, 1996).

CC102-FUNDAMENTALS OF PROGRAMMING 4
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Version History


• JDK Alpha and Beta (1995)
• JDK 1.0 (23rd Jan 1996)
• JDK 1.1 (19th Feb 1997)
• J2SE 1.2 (8th Dec 1998)
• J2SE 1.3 (8th May 2000)
• J2SE 1.4 (6th Feb 2002)
• J2SE 5.0 (30th Sep 2004)
• Java SE 6 (11th Dec 2006)
• Java SE 7 (28th July 2011)
• Java SE 8 (18th March 2014)
• Java SE 9 (21st Sep 2017)
• Java SE 10 (20th March 2018)

CC102-FUNDAMENTALS OF PROGRAMMING 5
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

The first projects developed using Java


• a personal hand-held
remote control named:
• Star 7
• was part of the:
• Green Project

CC102-FUNDAMENTALS OF PROGRAMMING 6
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

What is Java Technology?


• The Java technology is:
– A programming language
– A Development Environment
– An application environment
-An Deployment environment

CC102-FUNDAMENTALS OF PROGRAMMING 7
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

CC102-FUNDAMENTALS OF PROGRAMMING 8
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Object-oriented
• Java is an object-oriented programming language. Everything in Java is an object.
• Object-oriented means we organize our software as a combination of different
types of objects that incorporates both data and behavior.
• Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

CC102-FUNDAMENTALS OF PROGRAMMING 9
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Simple
• Java is very easy to learn, and its syntax is simple, clean and easy to
understand.
• Secured
• Java is best known for its security. With Java, we can develop virus-free systems. Java is
secured because:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Classloader: 
• Bytecode Verifier: 
• Security Manager: 

CC102-FUNDAMENTALS OF PROGRAMMING 10
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Platform Independent
• is platform independent because it is
different from other languages like C, C++,
etc. which are compiled into platform
specific machines while Java is a write
once, run anywhere language. A platform
is the hardware or software environment
in which a program run.
• There are two types of platforms
software-based and hardware-based.
Java provides a software-based
platform.
CC102-FUNDAMENTALS OF PROGRAMMING 11
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Robust - simply means strong. Java is robust because:
• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• Portable
• Java is portable because it facilitates you to carry the Java
bytecode to any platform. It doesn't require any implementation.
• Architecture-neutral
• Java is architecture neutral because there are no implementation
dependent features, for example, the size of primitive types is
fixed.

CC102-FUNDAMENTALS OF PROGRAMMING 12
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Features
• Dynamic
• Java is a dynamic language. It supports dynamic loading of classes.
• Interpreted
• Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++,
etc.
• High-performance
• Java is faster than other traditional interpreted programming languages because Java bytecode
is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++).
• Multi-threaded
• A thread is like a separate program, executing concurrently.
• Distributed
• because it facilitates users to create distributed applications in Java.

CC102-FUNDAMENTALS OF PROGRAMMING 13
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Features
• High-performance
• Java is faster than other traditional interpreted programming
languages because Java bytecode is "close" to native code. It is still
a little bit slower than a compiled language (e.g., C++).
• Multi-threaded
• A thread is like a separate program, executing concurrently.
• Distributed
• Java is distributed because it facilitates users to create
distributed applications in Java.
CC102-FUNDAMENTALS OF PROGRAMMING 14
WEEK 4
Java Development
Environment
CC102
FUNDAMENTALS OF PROGRAMMING
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Development Environment


• Installing JDK (Java Development Kit) produces
several folders in your system. Each of these
folders holds pertinent files that would be
necessary in building your program. For instance,
the bin folder contains executable files for
compiling your program (javac.exe).

CC102-FUNDAMENTALS OF PROGRAMMING 16
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Development Environment

CC102-FUNDAMENTALS OF PROGRAMMING 17
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Program Cycle


• Use any text editor to create source code — .java file
• Source code compiled according to JVM to get byte
code — .class file
• Byte code executed using an interpreter

Source
Codes Compiler Byte Interpreter Output

CC102-FUNDAMENTALS OF PROGRAMMING 18
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Program Cycle


Class HelloQCU {
Public static void main (String [ ] args) {
System.out.println(“Hello QCU!”);
}
}

Hello QCU!

Class HelloQCU {
Public static void main (String [ ] args) {
System.out.println(“Hello QCU!”);
}
}

CC102-FUNDAMENTALS OF PROGRAMMING 19
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java can be used to create two types of programs:


• Applet
special applications designed to run
within the context of a Web browser
• Application
stand-alone program that does not
need a browser to run

CC102-FUNDAMENTALS OF PROGRAMMING 20
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Applet Application
CC102-FUNDAMENTALS OF PROGRAMMING 21
Java
Code Structure

CC102
FUNDAMENTALS OF PROGRAMMING
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

Java Program Structure


• In the Java programming language:
• A program is made up of one or more classes
• A class contains one or more methods
• A method contains program statements

• A Java application always contains a


method called main

CC102-FUNDAMENTALS OF PROGRAMMING 23
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING

The main() method


When executing a Java application, the JVM loads the
class, and invokes the main() method of this class
The method main() must be declared public, static, and
void.
A source file may have one or more classes. Only one
class (matching the file name) at most may be declared
public

CC102-FUNDAMENTALS OF PROGRAMMING 24
WEEK 2 -3 DATA WAREHOUSE

Organizing Your Java Application


• A Java application is composed of a set of files generally distributed in a
directory structure
1. Source code: filename.java
2. Class file: classname.class (the same w/ filename)
• The compiler searches for a class file when it
encounters a reference to a class in a .java file
• The interpreter, during runtime, searches the .class files
• Both the compiler and the interpreter search for the .class files in the list of
directories listed in the
classpath variable

DM104 - EVALUATION OF BUSINESS PERFORMANCE 25


WEEK 2 -3 DATA WAREHOUSE

A sample source code in java

DM104 - EVALUATION OF BUSINESS PERFORMANCE 26


WEEK 2 -3 DATA WAREHOUSE
Tips: Names

• You can bundle related classes and interfaces into


a group called a package in a directory whose
name matches the name of the package

DM104 - EVALUATION OF BUSINESS PERFORMANCE 27


WEEK 2 -3 DATA WAREHOUSE

Tips: Names

• The qualified name for the class is course.Student, and the path name
to it is course/Student.java
• to import the package:
import course.Student;
• You can import all the classes in the package:
import course.*;

DM104 - EVALUATION OF BUSINESS PERFORMANCE 28


WEEK 2 -3 DATA WAREHOUSE

Tips: Names Advantages


• It makes it easier to find and use classes.
• It avoids naming conflicts. Two classes with the
same name existing in two different packages
do not have a name conflict, as long as they
are referenced by their fully qualified name.
• It provides access control.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 29


WEEK 2 -3 DATA WAREHOUSE

Package/ directory
structure

DM104 - EVALUATION OF BUSINESS PERFORMANCE 30


WEEK 2 -3 DATA WAREHOUSE

Classpath
• When the compiler or the interpreter encounters a class name in
your code, they look for classes in each directory or a JAR (Java
Archive) file listed in the classpath
• Let’s assume that we put the class files in the
c:\app\class\com\netalong\course directory, the classpath must
include this:
path name:
C:\app\class

DM104 - EVALUATION OF BUSINESS PERFORMANCE 31


WEEK 2 -3 DATA WAREHOUSE

How the compiler and interpreter construct a full path


name

DM104 - EVALUATION OF BUSINESS PERFORMANCE 32


WEEK 2 -3 DATA WAREHOUSE

The JAR files


• All the directories of an application can be
compressed into what is called a JAR file.
• A JAR file as a tree of directories.
• can be created with the jar command:
jar -cf myApp.jar topDir
• list directories and files in this JAR file:
jar -tf myApp.jar
• execute your application contained in the JAR file:
java -jar myApp.jar

DM104 - EVALUATION OF BUSINESS PERFORMANCE 33


WEEK 2 -3 DATA WAREHOUSE

First Java Program


class Simple{  
    public static void main(String args[])
{  
    System.out.println("Hello Java");  
   }  
}  

DM104 - EVALUATION OF BUSINESS PERFORMANCE 34


WEEK 2 -3 DATA WAREHOUSE

Compilation Flow:

DM104 - EVALUATION OF BUSINESS PERFORMANCE 35


WEEK 2 -3 DATA WAREHOUSE

Parameters used in First Java Program


• class keyword is used to declare a class in java.
• public keyword is an access modifier which represents visibility.
• static is a keyword. If we declare any method as static, it is known as
the static method.
• void is the return type of the method. main represents the starting
point of the program.
• String[] args is used for command line argument.
• System.out.println() is used to print statement.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 36


WEEK 2 -3 DATA WAREHOUSE

Basic Syntax
• Case Sensitivity − Java is case sensitive, which means
identifier Hello and hello would have different meaning in Java.
• Class Names − For all class names the first letter should be in Upper Case. If several
words are used to form a name of the class, each inner word's first letter should be
in Upper Case.
• Example: class MyFirstJavaClass
• Method Names − All method names should start with a Lower Case letter. If several
words are used to form the name of the method, then each inner word's first letter
should be in Upper Case.
• Example: public void myMethodName()
• Program File Name − Name of the program file should exactly match the class name.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 37


WEEK 2 -3 DATA WAREHOUSE

DM104 - EVALUATION OF BUSINESS PERFORMANCE 38


WEEK 2 -3 DATA WAREHOUSE

Java Keywords
• Java keywords are also known as reserved words.
Keywords are particular words which acts as a key to a
code. These are predefined words by Java so it cannot
be used as a variable or object name.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 39


WEEK 2 -3 DATA WAREHOUSE

Java List Keywords


Abstract class extends import package strictfp volatile
Boolean continu final instanceof private super while
e
Break default finally int protecte switch
d
Byte do float interface public synchronize
d
Case double for long return this
Catch else if new return throws
char enum implements null static transient

DM104 - EVALUATION OF BUSINESS PERFORMANCE 40


WEEK 2 -3 DATA WAREHOUSE

Java Identifiers
• All Java components require names. Names used for classes, variables, and
methods are called identifiers.
• All identifiers should begin with a letter (A to Z or a to z), currency character
($) or an underscore (_).
• After the first character, identifiers can have any combination of characters.
• A key word cannot be used as an identifier.
• Most importantly, identifiers are case sensitive.
• Examples of legal identifiers: age, $salary, _value, __1_value.
• Examples of illegal identifiers: 123abc, -salary.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 41


WEEK 2 -3 DATA WAREHOUSE

Java Variables
• A variable is a container which holds the value
while the java program is executed. A variable is
assigned with a datatype.
• Variable is a name of memory location. There
are three types of variables in java: local,
instance and static.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 42


WEEK 2 -3 DATA WAREHOUSE

Java Variables
• Local Variables
• Local variables are declared in methods, constructors, or A variable declared
inside the body of the method is called local variable. You can use this variable
only within that method and the other methods in the class aren't even aware
that the variable exists.
• A local variable cannot be defined with "static" keyword.

• Instance Variable
• A variable declared inside the class but outside the body of the method, is called
instance variable. It is not declared as static.
• It is called instance variable because its value is instance specific and is not shared
among instances.
DM104 - EVALUATION OF BUSINESS PERFORMANCE 43
WEEK 2 -3 DATA WAREHOUSE

Java Variables
• Static variable
• A variable which is declared as static is called static variable. It cannot
be local. You can create a single copy of static variable and share
among all the instances of the class. Memory allocation for static
variable happens only once when the class is loaded in the memory.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 44


WEEK 2 -3 DATA WAREHOUSE

• Example to understand the types of variables in java

• class A{  
• int data=50;//instance variable  
• static int m=100;//static variable  
• void method(){  
• int n=90;//local variable  
• }  
• }//end of class  

DM104 - EVALUATION OF BUSINESS PERFORMANCE 45


WEEK 2 -3 DATA WAREHOUSE

• Java Variable Example: Add Two Numbers

• class Simple{  
• public static void main(String[] args){  
• int a=10;  
• int b=10;  
• int c=a+b;  
• System.out.println(c);  
• }
• }  
Output: 20
DM104 - EVALUATION OF BUSINESS PERFORMANCE 46
WEEK 2 -3 DATA WAREHOUSE

• class Simple{  
• Java Variable Example: Widening

• public static void main(String[] args){  
• int a=10;  
• float f=a;  
• System.out.println(a);  
• System.out.println(f);  
• }
• }  
Output: 10
10.0
DM104 - EVALUATION OF BUSINESS PERFORMANCE 47
WEEK 2 -3 DATA WAREHOUSE

• Java Variable Example: Narrowing (Typecasting)


class Simple{  
public static void main(String[] args){  
float f=10.5f;  
//int a=f;//Compile time error  
int a=(int)f;  
System.out.println(f);  
System.out.println(a);  
}
}  
Output: 10.5
10
DM104 - EVALUATION OF BUSINESS PERFORMANCE 48
WEEK 2 -3 DATA WAREHOUSE

• Java Variable Example: Overflow


class Simple{  
public static void main(String[] args){  
//Overflow  
int a=130;  
byte b=(byte)a;  
System.out.println(a);  
System.out.println(b);  
}
}  
Output: 130
-126
DM104 - EVALUATION OF BUSINESS PERFORMANCE 49
WEEK 2 -3 DATA WAREHOUSE
• Java Variable Example: Adding Lower
Type class Simple{  
public static void main(String[] args){  
byte a=10;  
byte b=10;  
//byte c=a+b;//Compile Time Error: because a+b=20
 will be int  
byte c=(byte)(a+b);  
System.out.println(c);  
}
}  

OUTPUT:20

DM104 - EVALUATION OF BUSINESS PERFORMANCE 50


WEEK 2 -3 DATA WAREHOUSE

Data Types in Java


• Data types specify the different sizes and values that
can be stored in the variable. There are two types of
data types in Java:
• Primitive data types: The primitive data types
include boolean, char, byte, short, int, long, float
and double.
• Non-primitive data types: The non-primitive data
types include Classes, Interfaces, and Arrays.
DM104 - EVALUATION OF BUSINESS PERFORMANCE 51
WEEK 2 -3 DATA WAREHOUSE

Java Primitive Data Types


• are the building blocks of data manipulation. These are the
most basic data types available in Java language.
• There are 8 types of primitive data types:
• boolean data type
• byte data type
• char data type
• short data type
• int data type
• long data type
• float data type
• double data type

DM104 - EVALUATION OF BUSINESS PERFORMANCE 52


WEEK 2 -3 DATA WAREHOUSE

DM104 - EVALUATION OF BUSINESS PERFORMANCE 53


WEEK 2 -3 DATA WAREHOUSE

Boolean Data Type


• The Boolean data type is used to store only two possible values: true
and false. This data type is used for simple flags that track true/false
conditions.
Byte Data Type
• The byte data type is an example of primitive data type. It isan 8-bit
signed two's complement integer. Its value-range lies between -128 to
127 (inclusive). Its minimum value is -128 and maximum value is 127.
Its default value is 0.

DM104 - EVALUATION OF BUSINESS PERFORMANCE 54


WEEK 2 -3 DATA WAREHOUSE

Short Data Type


• The short data type is a 16-bit signed two's complement
integer. Its value-range lies between -32,768 to 32,767
(inclusive). Its minimum value is -32,768 and maximum value
is 32,767. Its default value is 0.
Int Data Type
• The int data type is a 32-bit signed two's complement
integer. Its value-range lies between - 2,147,483,648 (-2^31)
to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is -
2,147,483,648and maximum value is 2,147,483,647. Its
default value is 0.
DM104 - EVALUATION OF BUSINESS PERFORMANCE 55
WEEK 2 -3 DATA WAREHOUSE

Long Data Type


• The long data type is a 64-bit two's complement integer. Its
value-range lies between -9,223,372,036,854,775,808(-2^63)
to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its
minimum value is - 9,223,372,036,854,775,808and maximum
value is 9,223,372,036,854,775,807.
Float Data Type
• The float data type is a single-precision 32-bit IEEE 754
floating point.Its value range is unlimited. It is recommended
to use a float (instead of double) if you need to save memory
in large arrays of floating point numbers.
DM104 - EVALUATION OF BUSINESS PERFORMANCE 56
WEEK 2 -3 DATA WAREHOUSE

Double Data Type


• The double data type is a double-precision 64-bit IEEE
754 floating point. Its value range is unlimited. The
double data type is generally used for decimal values
just like float.
Char Data Type
• The char data type is a single 16-bit Unicode
character. Its value-range lies between '\u0000' (or
0) to '\uffff' (or 65,535 inclusive).

DM104 - EVALUATION OF BUSINESS PERFORMANCE 57


WEEK 2 -3 DATA WAREHOUSE

References:

• TutorialsPoint.,( 2019). Learn Java Programming


Retrieved from:
https://www.tutorialspoint.com/java.
• JavaTPoint., (2019). Java Tutorial. Retrieved
from: https://www.javatpoint.com/java-tutorial
DM104 - EVALUATION OF BUSINESS PERFORMANCE 58
THE END

CC102
FUNDAMENTALS OF PROGRAMMING

Вам также может понравиться