Java Unit i Batu
Java Unit i Batu
• Computer
– Performs computations and makes logical decisions
– Millions / billions times faster than human beings
• Computer programs
– Sets of instructions for which computer processes data
• Hardware
– Physical devices of computer system
• Software
– Programs that run on computers
ployee.
is called a byte. A byte of information is stored by using several bits in specified combination called bit
patterns. A widely used bit pattern for personal computers and data communication is the American Standard
Code for Information Interchange (ASCII).ASCII uses seven bits to represent one character. Each 1 or 0
corresponds to a single bit.
3.Data Field or Item
• The next level in data hierarchy is a field or item of data. A field or item of data is one or
more bytes containing data about the attribute of an entity in an information system. An
entity in a payroll system is an individual employee. Attributes are the employees name,
pay rate and so on. The basic pay is a field or item of data
4.Data Record
• A record is a collection of fields relating to a specific entity. For example, a payroll record
contains fields of data relating to a specific employee.
• An analogy can be made between a computer-based record and an individual folder in a
manual file.
• A folder in a payroll file may contain almost the same information as a record in a
computer-based payroll file.
• The field that identifies a record from all other records in a file is the record key. For
example, the record key in a payroll record is usually an employees provident fund
number or social security number because it is different for each employee.
5.File
• A file is a collection of related records.
6.Database
• A database consists of all the files of an organization. It is structured and integrated
to facilitate update of the files and retrieval of information from them.
• The term has often been used rather loosely.
• Technically, a database consists of those files that are a part of data base
management system. However, a database is often used to refer to all the files of an
organization. Now, all this is called as data hierarchy because databases are
composed of files, files are composed of records, records are composed of filed,
fields composed of data bytes and finally, data bytes are a group of bits.
• This is very well a hierarchical structure.
Computer Organization
• C++
– Evolved from C
• Evolved from BCPL and B
– Provides object-oriented programming capabilities
• Objects
– Reusable software components that model real-world items
• Java
– Originally for intelligent consumer-electronic devices
– Then used for creating Web pages with dynamic content
– Now also used for:
• Develop large-scale enterprise applications
• Enhance WWW server functionality
• Provide applications for consumer devices (cell phones, etc.)
• Classes
– Contain methods that perform tasks
• Return information after task completion
– Used to build Java programs
• Java contains class libraries
– Known as Java APIs (Application Programming Interfaces)
• Fortran
– FORmula TRANslator
• COBOL
– COmmon Business Oriented Language
• Pascal
• Basic
• Structured Programming
– Structured programs
• Clearer than unstructured programs
• Easier to test, debug and modify
– Pascal designed for teaching structured programming
– ADA
• Multitasking
– C
• Internet
– Developed over three decades ago with DOD funding
– Originally for connecting few main computer systems
– Now accessible by hundreds of millions of computers
• World Wide Web (WWW)
– Allows for locating/viewing multimedia-based documents
• A programming language must be simple, easy to learn and use, have good readability
and human recognizable.
• Abstraction is a must-have Characteristics for a programming language in which ability to
define the complex structure and then its degree of usability comes.
• A portable programming language is always preferred.
• Programming language’s efficiency must be high so that it can be easily converted into a
machine code and executed consumes little space in memory.
• A programming language should be well structured and documented so that it is suitable
for application development.
• Necessary tools for development, debugging, testing, maintenance of a program must
be provided by a programming language.
• A programming language should provide single environment known as Integrated
Development Environment(IDE).
• A programming language must be consistent in terms of syntax and semantics.
Introduction to Object Technology
• There are string objects, output objects, input objects, graphics objects, date objects, time
objects, audio objects, video objects, etc.
• a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g.,
calculating, moving and communicating).
• Using a modular, object-oriented design and implementation approach can make software-
development more productive. Object-oriented programs are often easier to understand,
correct and modify.
• Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data. An object-oriented
program can be characterized as data controlling access to code
• All object-oriented programming languages provide mechanisms that help
you implement the object-oriented model. They are encapsulation,
inheritance, and polymorphism
• Object orientation
• Unified Modeling Language (UML)
– Graphical language that uses common notation
– Allows developers to represent object-oriented designs
• Objects
– Reusable software components that model real-world items
– Look all around you
• People, animals, plants, cars, etc.
– Attributes
• Size, shape, color, weight, etc.
– Behaviors
• Babies cry, crawl, sleep, etc.
• UML
– Graphical representation scheme
– Enables developers to model object-oriented systems
– Flexible and extendible
• Batch processing
– One job (task) at a time
– Operating systems developed
• Programs to make computers more convenient to use
• Switch jobs easier
• Multiprogramming
– “Simultaneous” jobs
– Timesharing operating systems
Compiler c rea te s
bytec odes and stores
Pha se 2 Compiler Disk the m on disk.
Primary
Memory
Pha se 3 Class Lo ad er
Primary
Memory
Pha se 4 Bytec ode Ve rifier Byte c ode verifier
c onfirms that a ll
bytec odes are valid
and do not violate
J ava ’s sec urity
restric tions.
..
..
..
Primary
Memory Interpreter read s
Pha se 5 Interp reter bytec odes and
translates them into a
la nguag e that the
c omputer c an
und ersta nd, possib ly
storing d ata values a s
.. the program exec utes.
..
..
• integrated development environments (IDEs) are available from many major software suppliers.
• IDEs provide tools that support the software development process, including editors for writing and
editing programs
• Popular IDEs include Eclipse (www.eclipse.org) and NetBeans (www.netbeans.org).
Phase 2: Compiling a Java Program into Bytecodes
• use the command javac (the Java compiler) to compile a program
• For example, to compile a program called Welcome.java, you’d type in the command window of your
system (i.e., the Command Prompt in Windows, the shell prompt in Linux or the Terminal application
in Mac OS X). If the program compiles, the compiler produces a .class file called Welcome.class that
contains the compiled version of the program.
• The Java compiler translates Java source code into bytecodes that represent the tasks to execute in
the execution phase.Bytecodes are executed by the Java Virtual Machine (JVM)—a part of the JDK
and the foundation of the Java platform.
• Virtual machine (VM) is a software application that simulates a computer but hides the underlying
operating system and hardware from the programs that interact with it. If the same VM is
implemented on many computer platforms, applications that it executes can be used on all those
platforms.
• Java’s bytecodes are portable—without recompiling the source code, the same bytecodes can
execute on any platform containing a JVM that understands the version of Java in which the
bytecodes were compiled.
• The JVM is invoked by the java command.
• For example, to execute a Java application called Welcome, you’d type the command in a command
window to invoke the JVM as java Welcome
Phase 3: Loading a Program into Memory
• JVM places the program in memory to execute it—this is known as loading
• The JVM’s class loader takes the .class files containing the program’s bytecodes and transfers them to
primary memory. The .class files can be loaded from a disk on your system or over a network (e.g., your local
college or company network, or the Internet).
Phase 4: Bytecode Verification
• as the classes are loaded, the bytecode verifier examines their bytecodes to
ensure that they’re valid and do not violate Java’s security restrictions
Phase 5: Execution
• the JVM executes the program’s bytecodes, thus performing the actions specified by
the program .
• JVMs typically execute bytecodes using a combination of interpretation and so-called
just-in-time (JIT) compilation. In this process, the JVM analyzes the bytecodes as
they’re interpreted, searching for hot spots—parts of the bytecodes that execute
frequently. For these parts, a just-in-time (JIT) compiler— known as the Java HotSpot
compiler—translates the bytecodes into the underlying computer’s machine language.
• When the JVM encounters these compiled parts again, the faster machine-language
code executes. Thus Java programs actually go through two compilation phases—one
in which source code is translated into bytecodes (for portability across JVMs on
different computer platforms) and a second in which, during execute the bytecodes are
translated into machine language for the actual computer on which the program
executes.
Your First Program in Java:
Printing a Line of Text, Modifying Your First Java Program
54
1 // Fig. 2.1: Welcome1.java Outline
2 // A first program in Java.
3
4 public class Welcome1 {
5
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.println( "Welcome to Java Programming!" );
10
11 } // end method main
12
13 } // end class Welcome1 Welcome1.java
Program Output
Welcome to Java Programming!
2002 Prentice Hall.
All rights reserved.
55
– Blank line
• Makes program more readable
• Blank lines, spaces, and tabs are white-space characters
– Ignored by compiler
– Saving files
• File name must be class name with .java extension
• Welcome1.java
– Left brace {
• Begins body of every class
• Right brace ends definition (line 13)
8 {
4.1 Method
System.out.println
Program Output
– Line breaks at \n
• Usage
– Can use in System.out.println or System.out.print to create new
lines
• System.out.println( "Welcome\nto\nJava\nProgramming!" );
Welcome
to
Java
Programming!
Notice how a new line is output for each \n
escape sequence.
2002 Prentice Hall.
All rights reserved.
69
2.3.2 Displaying Multiple Lines of Text with a
Single Statement
Escape characters
– Backslash ( \ )
– Indicates special characters be output
Esc ape sequenc e Description
\n Newline. Position the screen cursor to the beginning of the next
line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the
current line; do not advance to the next line. Any characters
output after the carriage return overwrite the characters previously
output on that line.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double-quote character. For
example,
System.out.println( "\"in quotes\"" );
displays
"in quotes"
Fig. 2.5 Some common esc ape sequences.
• Method printf’s first argument is a format string that may consist of fixed text and format specifiers.
public class Welcome4
{
public static void main( String args[] )
{
System.out.printf( "%s\n%s\n","Welcome to", "Java Programming!" );
}
}
Output: Welcome to
Java Programming!
Another Java Application: Adding Integers
• Application reads (or inputs) two integers (whole numbers, like –22, 7, 0 and 1024) typed by
a user at the keyboard, computes the sum of the values and displays the result.
import java.util.Scanner; // program uses class declaration
• Scanner is an import declaration that helps the compiler locate a class that is used in
this program
Scanner input = new Scanner( System.in );
is a variable declaration statement (also called a declaration) that specifies the name
(input) and type (Scanner) of a variable that is used in this program.
• A Scanner enables a program to read data
• new Scanner( System.in ) expression creates a Scanner object that reads data typed
by the user at the keyboard.
• standard input object, System.in, enables Java applications to read information
typed by the user.
Memory Concepts
• Variable names such as number1, number2 and sum actually correspond to
locations in the computer’s memory. Every variable has a name, a type, a size and a
value.
• In the addition program, when the following statement executes
number1 = input.nextInt(); // read first number from user
the number typed by the user is placed into a memory location to which the compiler
assigned the name number1.
• Suppose that the user enters 45. The computer places that integer value into location
number1. Whenever a value is placed in a memory location, the value replaces the
previous value in that location. The previous value is lost.
Arithmetic
• Most programs perform arithmetic calculations.
Arithmetic Expressions in Straight-Line Form
• Arithmetic expressions in Java must be written in straight-line form to facilitate entering
programs into the computer. Thus, expressions such as “a divided by b” must be written as
a / b, so that all constants, variables and operators appear in a straight line.
Parentheses for Grouping Subexpressions
• Parentheses are used to group terms in Java expressions in the same manner as in
algebraic expressions. For example, to multiply a times the quantity b + c, we write
a*(b+c)
• If an expression contains nested parentheses, such as ( ( a + b ) * c )
• the expression in the innermost set of parentheses (a + b in this case) is evaluated first.
Rules of Operator Precedence
• Java applies the operators in arithmetic expressions in a precise sequence determined by
the following rules of operator precedence, which are generally the same as those followed in
algebra:
1. Multiplication, division and remainder operations are applied first. If an expression contains
several such operations, the operators are applied from left to right. Multiplication, division and
remainder operators have the same level of precedence.
2. Addition and subtraction operations are applied next. If an expression contains several such
operations, the operators are applied from left to right. Addition and subtraction operators have the
same level of precedence.
Sample Algebraic and Java Expressions
The following is an example of an arithmetic mean (average) of five terms:
• Algebra:
a+b+c+d+e
m = ---------------------------
5
• Java: m = ( a + b + c + d + e ) / 5;
Decision Making: Equality and Relational Operators
• A condition is an expression that can be either true or false.
• Conditions in if statements can be formed by using the equality operators (== and !=) and relational
operators (>, <, >= and <=) summarized in Fig.
• The equality operators associate from left to right. The relational operators all have the same level of
precedence and also associate from left to right.