0% found this document useful (0 votes)
14 views43 pages

Java Jeevan

The document provides an overview of programming language levels, specifically focusing on machine language, assembly language, and high-level language, with Java as a prominent example. It details the structure of Java programs, including classes and methods, and emphasizes Java's features such as portability, object-oriented design, and its unique compilation and interpretation process. Additionally, it covers Java's primitive data types, operators, error handling, and the development lifecycle of Java code.

Uploaded by

ang.jeevan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views43 pages

Java Jeevan

The document provides an overview of programming language levels, specifically focusing on machine language, assembly language, and high-level language, with Java as a prominent example. It details the structure of Java programs, including classes and methods, and emphasizes Java's features such as portability, object-oriented design, and its unique compilation and interpretation process. Additionally, it covers Java's primitive data types, operators, error handling, and the development lifecycle of Java code.

Uploaded by

ang.jeevan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

Programming Language Levels

 A programming language specifies the words and


symbols that we can use to write a program
 A programming language employs a set of rules that
dictate how the words and symbols can be put together
to form valid program statements
 There are three programming language levels:
 machine language
 assembly language
 high-level language
 Each type of CPU has its own specific machine language
 The other levels were created to make it easier for a
human being to write programs
Language Translators

 Machine language is the only language capable of directly


instructing the CPU. Every non machine language program
instruction must be translated into machine language prior to
execution. Language Translators convert high-level code into
machine language.
 Interpreters translate one program statement at a time, as the
program is running.
 Compilers translate a complete program into machine
language, then the machine language is executed as needed.
Because compiled programs run faster than programs that are
translated line by line by an interpreter, programmers usually
choose compilers to translate frequently run business
programs.
Java - An Introduction

 Java – Programming language by Sun Microsystem.


 Java - CPU Independent language
 Oak -The predecessor of Java
 Java - James gosling, Arthur Van hoff, Andy bechtolsheim and others
and others.
 Java is “C++ -- ++ “
As a whole, Java is...

 Simple
 Architecture-neutral
 Object-Oriented
 Portable
 Distributed
 Interpreted
 Multithreaded
 Robust
 Secure

Characteristics
The Java Platform

A platform is the hardware and software environment in which


a program runs. The Java platform differs from most other
platforms in that it's a software-only platform that runs on top
of other, hardware-based platforms. Most other platforms are
described as a combination of hardware and operating
system. The Java platform has two components:
 The Java Virtual Machine (Java VM)
 The Java Application Programming Interface (Java API)
The Java API is a large collection of ready-made software
components that provide many useful capabilities, such as
graphical user interface (GUI) widgets. The Java API is grouped
into libraries (packages) of related components which allow
you to do various things.
Java 2 Platform
Java Virtual Machine

Java is an unusual language because it is both compiled and


interpreted. "With a compiler, you translate a Java program
into an intermediate language called Java bytecodes--the
platform-independent codes interpreted by the Java
interpreter. With an interpreter, each Java bytecode
instruction is parsed and run on the computer. Compilation
happens just once; interpretation occurs each time the
program is executed. This figure illustrates how this works.
Java Virtual Machine(cont.)
You can think of Java bytecodes as the machine code instructions for
the Java Virtual Machine (Java VM). Every Java interpreter is an
implementation of the Java VM. The Java VM can also be implemented
in hardware.
Java bytecodes help make "write once, run anywhere" possible. You
can compile your Java program into bytecodes on any platform that
has a Java compiler. The bytecodes can then be run on any
implementation of the Java VM. For example, the same Java program
can run on Windows NT, Solaris, and Macintosh.
Java

Write Once, Run Anywhere


Object Oriented Languages -a Comparison

Feature C++ Ada Java

Encapsulation Yes Yes Yes


Inheritance Yes No Yes
Multiple Inherit. Yes No No
Polymorphism Yes Yes Yes
Binding (Early/Late) Both Early Late
Concurrency Poor Difficult Yes
Garbage Collection No No Yes
Class Libraries Yes Limited Yes
Java better than C++ ?

 No Typedefs, Defines, or Preprocessor


 No Global Variables
 No Goto statements
 No Pointers
 No Unsafe Structures
 No Multiple Inheritance
 No Operator Overloading
Removed From C++
 Operator overloading
 Pointers
 Multiple-inheritance of implementation
 Enum, typedef, #define
 Copy constructors, destructors
 Templates
 And other stuff....
Added or Improved over C++
 Interfaces: type Vs. class
 Garbage collection
 Exceptions (More powerful than C++)
 Strings
 Instanceof
 Package
 Multi-threads
Java Integrates
Power of Compiled Languages
and
Flexibility of Interpreted Languages
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
 These terms will be explored in detail throughout the
course
 A Java application always contains a method called main
Java Program Structure

// comments about the class


public class MyProgram
{
class header

class body

Comments can be added almost anywhere


}
Java Program Structure

// comments about the class


public class MyProgram
{

// comments about the method


public static void main (String[] args)
{
method header
method body
}

}
Comments

 Comments in a program are also called inline documentation


 They should be included to explain the purpose of the program and
describe processing steps
 They do not affect how a program works
 Java comments can take two forms:

// this comment runs to the end of the line

/* this comment runs to the terminating


symbol, even across line breaks */

/** This kind of comment is a special


* ‘javadoc’ style comment
*/
Identifiers

 Identifiers are the words a programmer uses in a program


 An identifier can be made up of letters, digits, the underscore
character (_), and the dollar sign
 They cannot begin with a digit

 Java is case sensitive, therefore Total and total are different


identifiers
Reserved Words

abstract default goto operator synchronized


boolean do if outer this
break double implements package throw
byte else import private throws
byvalue extends inner protected transient
case false instanceof public true
cast final int rest try
catch finally interface return var
char float long short void
class for native static volatile
const future new super while
continue generic null switch
Primitive Data Types

In Java, everything is an object with the exception of the


simple data types (primitives). Unlike objects, primitive types
are data objects that:
 Consist of a single value
 Are manipulated by using operators
 Store values (object types store references)
Primitive Data

 There are exactly eight primitive data types in Java


 Four of them represent integers:
 byte, short, int, long
 Two of them represent floating point numbers:
 float, double
 One of them represents characters:
 char
 And one of them represents boolean values:
 boolean
Numeric Primitive Data

 The difference between the various numeric primitive types is


their size, and therefore the values they can store:
 Prefixes: byte, short, int, long, float, double

Type Storage Min Value Max Value

byte 8 bits -128 127


short 16 bits -32,768 32,767
int 32 bits -2,147,483,648 2,147,483,647
long 64 bits < -9 x 1018 > 9 x 1018

float 32 bits +/- 3.4 x 1038 with 7 significant digits


double 64 bits +/- 1.7 x 10308 with 15 significant digits
Characters

 A char variable stores a single character from the Unicode


character set
 A character set is an ordered list of characters, and each
character corresponds to a unique number
 The Unicode character set uses sixteen bits per character,
allowing for 65,536 unique characters
 It is an international character set, containing symbols and
characters from many world languages
 Character literals are delimited by single quotes:
'a' 'X' '7' '$' ',' '\n‘
Characters

 The ASCII character set is older and smaller than Unicode, but is still
quite popular
 The ASCII characters are a subset of the Unicode character set,
including:

uppercase letters A, B, C, …
lowercase letters a, b, c, …
punctuation period, semi-colon, …
digits 0, 1, 2, …
special symbols &, |, \, …
Boolean

 A boolean value represents a true or false condition


 A boolean can also be used to represent any two states,
such as a light bulb being on or off
 The reserved words true and false are the only valid
values for a boolean type
boolean done = false;
Variables

 A variable can be given an initial value in the


declaration

int sum = 0;
int base = 32, max = 149;

When a variable is referenced in a program, its current value is used


Assignment

 An assignment statement changes the value of a variable


 The assignment operator is the = sign

total = 55;

The expression on the right is evaluated and the result is stored in the variable on the left
The value that was in total is overwritten
You can only assign a value to a variable that is consistent with the variable's declared
type
Constants

 A constant is an identifier that is similar to a variable


except that it holds one value for its entire existence
 The compiler will issue an error if you try to change a
constant
 In Java, we use the final modifier to declare a constant

final int MIN_HEIGHT = 69;


 Constants:
 give names to otherwise unclear literal values
 facilitate changes to the code
 prevent inadvertent errors
Arithmetic Expressions

 An expression is a combination of operators and operands


 Arithmetic expressions compute numeric results and make use
of the arithmetic operators:

Addition +
Subtraction -
Multiplication *
Division /
Remainder %
If either or both operands to an arithmetic operator are floating point,
the result is a floating point
Bitwise Operators

Bitwise Op Java Operation Java Expression

~ Unary Not ~a

& And a&b

| Or a|b

^ Xor a^b

>> Righ shift a>>

>>> Right shift with a>>>


zero fill
<< Left shift a<<
Relational Operators

Relational Op Java Operation Java Expression

== Test for a==b


equality
!= a!=b

< a<b

> a>b

<= a<=b

>= >=b

<<
Logical Operators

Logical Op Java Operation Java Expression

& Logical and a&b

| Logical or a|b

!= Not equal to a!=b

! Unary not !a

|| Short circuit or a||b

&& Short circuit a&&b


and
Division and Remainder

 If both operands to the division operator (/) are


integers, the result is an integer (the fractional part is
discarded)
14 / 3 equals? 4

8 / 12 equals? 0

The remainder operator (%) returns the remainder after dividing the second
operand into the first

14 % 3 equals? 2

8 % 12 equals? 8
Operator Precedence

 Operators can be combined into complex expressions

result = total + count / max - offset;

 Operators have a well-defined precedence which determines


the order in which they are evaluated
 Multiplication, division, and remainder are evaluated prior to
addition, subtraction, and string concatenation
 Arithmetic operators with the same precedence are evaluated
from left to right
 Parentheses can always be used to force the evaluation order
Development Environments

 There are many development environments which


develop Java software:
 Sun Java Software Development Kit (SDK)
 Netbeans
 Eclipse
 Borland JBuilder
 Symantec Café
 WebSphere Studio
 Though the details of these environments differ, the
basic compilation and execution process is essentially
the same
Syntax and Semantics

 The syntax rules of a language define how we can put


symbols, reserved words, and identifiers together to
make a valid program
 The semantics of a program statement define what that
statement means (its purpose or role in a program)
 A program that is syntactically correct is not necessarily
logically (semantically) correct
 A program will always do what we tell it to do, not what
we meant to tell it to do
Errors

 A program can have three types of errors


 The compiler will find problems with syntax and other
basic issues (compile-time errors)
 If compile-time errors exist, an executable version of the
program is not created
 A problem can occur during program execution, such as
trying to divide by zero, which causes a program to
terminate abnormally (run-time errors)
 A program may run, but produce incorrect results
(logical errors)
Problem Solving

 The purpose of writing a program is to solve a problem


 The general steps in problem solving are:
 Understand the problem
 Dissect the problem into manageable pieces
 Design a solution
 Consider alternatives to the solution and refine it
 Implement the solution
 Test the solution and fix any problems that exist
Coding Guidelines

 coding standards:
 Variable and method names (also add prefixes discussed
earlier)
 Class names
 Constant names
 Braces
 Comments
 Source File organization
Life Cycle of Java Code

Using Sun Java SDK


Command Line Interface

Editor
Programmer

Source Class Virtual


Compiler Program
File(s) File(s) Machine
(javac) executes
(.java) (.class) (java)

Parts of Sun Java SDK


Lifecycle of Java Code
Program is created in
Phase 1 Editor Disk the editor and stored
on disk.
Compiler creates
Phase 2 Compiler Disk bytecodes and stores
them on disk.
Primary
Memory
Class Loader
Phase 3

Class loader puts


Disk bytecodes in memory.
..
..
..
Primary
Memory
Phase 4 Bytecode Verifier Bytecode verifier
confirms that all
bytecodes are valid
and do not violate
Java’s security
.. restrictions.
..
..
Primary
Interpreter Memory Interpreter reads
Phase 5 bytecodes and
translates them into a
language that the
computer can
understand, possibly
.. storing data values as
.. the program executes.
..

You might also like