Академический Документы
Профессиональный Документы
Культура Документы
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
CC102-FUNDAMENTALS OF PROGRAMMING 4
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 5
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 6
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
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
CC102-FUNDAMENTALS OF PROGRAMMING 16
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 17
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
Source
Codes Compiler Byte Interpreter Output
CC102-FUNDAMENTALS OF PROGRAMMING 18
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
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
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
CC102-FUNDAMENTALS OF PROGRAMMING 23
WEEK 4 INTRODUCTION TO JAVA PROGRAMMING
CC102-FUNDAMENTALS OF PROGRAMMING 24
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.*;
Package/ directory
structure
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
Compilation Flow:
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.
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.
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.
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.
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.
• class A{
• int data=50;//instance variable
• static int m=100;//static variable
• void method(){
• int n=90;//local variable
• }
• }//end of class
• 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
OUTPUT:20
References:
CC102
FUNDAMENTALS OF PROGRAMMING