You are on page 1of 23

JAVA

PROGRAMMING
BASIC

CSC 238 – Object Oriented Programming


HISTORY OF JAVA
 Initially designed at Sun Microsystems by James
Gosling in 1991 as a language that was embedded in
consumer electronic items. The language was named
as OAK.

 In 1993, the growth of internet phenomena, OAK was


adapted into internet to create dynamic and
interactive web pages.

 Sun created web browser named HotJava was


programmed using OAK language called JAVA.

 JAVA not only used for web application but also as a


general purpose programming language
Why JAVA?
Characteristics of JAVA
No Characteristic Description
1. Simple Java is partially modeled on C++, but greatly
simplified and improved
2. Dynamic Java was designed to adapt to an evolving
environment. New code can be loaded on the fly
without recompilation.

3. Object Oriented programming in Java is centered on creating


objects, manipulating objects, and making
objects work together.

4. Distributed involves several computers working together on


a network. Java is designed to make distributed
computing easy.

5. Interpreted Java programs are compiled into the Java


Virtual Machine code called bytecode, and it is
machine independent.
No Characteristi Description
c
6. Robust Reliability because it puts a lot of emphasis on early
checking for possible errors. Java has a runtime
exception-handling feature to provide programming
support for robustness.

7. Secure As an Internet programming language, Java is used


in a networked and distributed environment. Java
implements several security mechanisms to protect
your system against harm caused by stray program.
8. Portable Java is architecture neutral. It can run in any
platform without being recompiled. The Java
environment is portable to new hardware and
operating systems
9. Performance Java’s performance is sometimes criticized
compared to C++. Because Java is interpreted, the
bytecode is not directly executed by the system, but
is run through the interpreter.

10. Multithread a program’s capability to perform several tasks


simultaneously.
C++ and JAVA
Executing C++
Program :

Program in PC
myProg. C++ Machine
Compiler language
cpp
for PC

Source Program in
C++ SUN
File Compiler Machine
for SUN language

C++ Program in MAC


Compiler for Machine
language
MAC
Executing JAVA
Program :
myProg.jav JAVA JAVA
myProg.clas
a Compli Interpret
s
er er
Source File
(Java JVM/JRE
Library
Program)
files

The compiled program is


in the form of bytecode –
the same for all machines
C++ VS JAVA
Feature C++ Java

1. Design goals To design a distributed For programming


OS consumer device
   
2. Designer Bjarne Stroustrup. James Gosling
AT&T Bell Labs in 1979 et.al.
Sun Microsystems
3. Language Hybrid OO = structured in
OO1990
paradigm only
paradigm + OO   

4. Prior language Syntax based on C. Syntax based on


influence Efficient styles based on C++, approach
Simula67 based on
Smalltalk
5. Strengths A high performance and  
Cross-platform
powerful language compatibility
 
6. Compilation Compilation-based – like Compiled +
most language Interpreted based
C++ source file -> Java source file ->
binary file (*.exe) Java bytecode ->
JVM to execute
 
7. Architecture- Platform-dependent Platform-
neutral and binary code cannot be independent
portability executed on a difference bytecode can run
machine on any machine
supporting JVM
8. Simplicity More complex – pointer, Simpler
  – no
  operator overloading pointer, no
and etc. overloading and
etc.
9. About 1~20 times faster  
Much slower than
Performance than equivalent JAVA C++, but good
  code enough to run
  interactively for
most application.
1 Was not designed for  Originally designed
0. Robustness robustness for writing highly
    reliable or robust
software.
 
1 Security Memory is handled at Byte-code is
1. compile-time by verified at run-
compiler time to ensure
  security
restrictions are not
violated.
Memory layout is
handled at run-
1 Networking Not a network-capable time
A by JVM.
network-capable
2.   language.  
language.
Network programming : Network
harder programming:
  easier.
 

1 Multi-threaded Single-threaded – rely Multi-treaded


3. (concurrent on external libraries for language –
programming) multithreading provides native
multithreading
support.
 
Identifiers
 Identifier is a name given to variable,
constant and method name other than
reserved word
 It can be alphabet, numeric, underscore(_)
or dollar sign ($)
 The name must begin with alphabet,
underscore(_) or dollar sign($)
 It is case sensitive
 Cannot be JAVA reserved word
 Example valid identifier name :
total
_total
$total
total5
JAVA Reserved Word
abstract continue goto package synchronized
assert default if private this
Boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
Data Types
 Primitive data types
◦ Simple data types, also referred to as built-in types
◦ Different category
 Boolean type – boolean
 Integral type – byte, int, long, char
 Floating type – float, double

 Reference data type


◦ Either with a class, array or interface
◦ Refers to object
◦ Different category
 Class types – such as String
 Array types
 Interface types

 Constant data types


◦ Declare using keyword final
eg. final double PI = 3.142;
◦ Constant declared inside a class using the keyword static
eg. static final double PI = 3.142;
String
Stringclass is part of java.lang
package which is automatically
included during compilation
Some operation used in String class
◦ toUpperCase()
◦ toLowerCase()
◦ length()
◦ charAt()
◦ substring()
◦ replace()
Example of String
operation
String name;
name = “Abraham Lincoln”;
name = name.toUpperCase(); //ABRAHAM LINCOLN
name = name.toLowerCase(); //abraham lincoln
int x = name.length(); //return the how many characters
=15
name = name.charAt(2); //return a character in position 2
Name = name.substring(5); //return the whole word
starting position 5
// = ham Lincoln
Name = name.substring(5, 3) //return the word of length
3 starting
// from position 5 = ham

**position in a string always starts with 0


Control Structure
 Selection structure
◦ Make decision – branching statement
◦ Similar to C++
 if statement
 if…else statement
 Nested if statement
 switch statement

 Iteration structure
◦ Repetition – similar to C++
 for statement
 while statement
 do…while statement
 Nested loop
 break and continue
Array
An array is a collection of data
value
An array is an indexed value of the
same type
Array of primitive data types
◦ Declaration
datatype variablename []; or datatype []
variablename;
◦ Array creation
eg. variablename = new dataype[size of array];
example

double[] rainfall; //declare an array


rainfall = new double[12]; //create array
Packages
 Consist of one or more individual classes
that are stored in the same directory.
 Those classes packaged are usually related
in providing similar objects and methods
 it facilitates software reuse by allowing
programs to import classes from other
packages libraries to be shared by many
users
 For example to use PrintStream class in
contained in java.io package, you need to
include keyword import in front of the
package name
import java.io.*;

Then you can use the print() and println() method provided by the
PrintStream class
Input Statement
Using I/O console
◦ Using Scanner class
◦ Create a Scanner object by passing an
input stream to the constructor
Scanner s = new Scanner(System.in);
◦ You can use these method to read the data
from the keyboard :
 Integer data value = nextInt()
 Decimal data value = nextDouble();
 String data value = next();

◦ Example :
Scanner s = new Scanner(System.in);
int num = s.nextInt();
String name = s.next();
 Using dialog box
◦ You need to use javax.swing.JOptionPane package
◦ The method used is showInputDialog, and it has
String parameter which is a message displayed on the
monitor.
◦ The method returns String value if user enters a value
and null value if user cancels the input
◦ The String value can be converted to numeric by the
used of these method
 Integer = Integer.parseInt()
 Double = Double.parseDouble()
 Foat = Float.parseFloat()
◦ Example
import javax.swing.*;
--
String input = JOptionPane.showInputDialog(null, “Enter
an integer number “);
int num = Integer.parseInt();
Output Statement
Using I/O Console
◦ By using System.out object through
the method print() or println()

◦ Example
int total = 4 +5;
System.out.print(“The total is “);
System.out.println(total);
System.out.println(“The total is “ + total);
Using dialog box
◦ You need to use javax.swing.JOptionPane package
◦ The method used is showMessageDialog, and it has
String parameter which is a message displayed on
the monitor screen.

◦ Example
import javax.swing.*;
--
JOptionPane.showMessageDialog(null,”Hello World!”);
String s1 = “JAVA is fun”;
JOptionPane.showMessageDialog(null, s1);

You might also like