lecture 2 object oriented programming basics of java language mby

22
Lecture 2 Object Oriented Programming Basics of Java Language MBY

Upload: edwin-roberts

Post on 31-Dec-2015

229 views

Category:

Documents


4 download

TRANSCRIPT

Lecture 2Object Oriented Programming

Basics of Java Language

MBY

Agenda• Course Perspective• Interpreter VS Compiler• Programming Essentials• Value• Variable• Data Types in Java• Identifiers• Type conversions• Manipulating Variables• Reserved Words• Manipulating Values• Expression• Operator Precedence and Associatively• Primitive Vs non Primitive data types• Objects

Course Perspective

• Learn programming in a high-level programming language.• Programming has many paradigms

– Procedural– Object-Oriented– Functional– Logic

• We will study Object-Oriented Programmingusing ‘Java’, a popular high-level object-orientedprogramming language.

Interpreter VS Compiler

• An alternative to compiling your program is to interpret your program

• each line of your program is translated into machine language andimmediately executed

• Like translating between natural languages• Compiler: human translator translates book in its entirety and

then• translated book is printed and read• Interpreter: human interpreter translates each spoken

statement in sequence as speaker is speaking

Execution of Java Program

• Java uses both compilation and interpretation in a two-step process• Compiles program into byte codes

– byte code is close to machine language instructions, but not quite — it is a generic “machine language”– does not correspond to any particular machine

• Virtual Machine (VM) interprets byte codes into native machine language and runsit– different VM exists for different computers, since byte code does not correspond to a real machine

• Same Java byte codes can be used on different computers without re-compilingsource code– each VM interprets same byte codes– allows you to run Java programs by getting just byte codes from Web page

• This makes Java code run cross-platform– marketing says, “Write once, run anywhere!”

Graphical presentation of Execution

Programming Essentials

• Key Tools for Programming– Editors: Allows user to enter the program. Notepad, WordPad, MSWord,

etc are all editors.– Compilers/Interpreters: Translates the program into target code (in

machine language).– Debuggers: Allows a programmer to run the program to see the

execution of the program and correct any errors.– Profilers: Used to evaluate program’s performance.– Integrated Development Environment (IDE): Combines editor, compiler, debugger and profiler or a subset into one tool.• Common Java IDEs are Eclipse, Netbeans, BlueJ, and DrJava.• We will use Netbeans IDE for the programming assignments in this course.

A Word of Advice

• Without good command on programmingany qualification in Computer Science,Computer Engineering, InformationTechnology and Software Engineering is“worthless”.

• There is an acute shortage of programmersin the global software market and with timethis shortage is increasing

Addressing Memory

• Each location is 1 byte of memory• 1 byte = 8 bits• Each bit is an electric impulse carrying 1 or 0.• Each byte has a unique address in

hexadecimal format.• CPU reads/writes data from/to memory

using that address.

Variable

• A ‘Variable’ is used to store a value inside a computer.

• A variable is a space in the memory to storea value.

• Three important characteristics of a variable are Type, Name and Value.

Type of a Variable

• Among other advantages a ‘type’ binds thememory to a variable name.

• The type int is of 4 bytes in Java.• Therefore, it can hold maximum of

2,147,483,647 value.• It can also hold values in negative down to

-2,147,483,648.

Variable for Real Numbers

• int cannot hold a real value.• Therefore, a type “double” is used to hold real

values.• Double takes 8 bytes of memory instead of 4

bytes.• Out of the 8 bytes in a double 4 bytes are used to

hold the value before the decimal point and 4bytes for the value after the decimal point

Different Data Types in Java

Data Type Meaning Required Memory • Byte Byte 8 bits• Short Short Integer 16 bits• Int Integer 32 bits• Long Long Integer 64 bits• Float Single Precision Floating Point 32 bits• Double Double Precision Floating Point 64 bits• Char Unicode Character 16 bits

Type Conversion

• Java can perform conversion automatically• int value can be assigned to long.• Depends upon type compatibility• Not all type conversions implicitly allowed.• Cant assign a long value to int.• Solution

– Casting

Type Conversion(Widening conversion)

• Widening conversion is also known as upcasting.• Narrow data types are converted into broad data

type with out loss of information – Both types should be compatible.e.g. Numeric types are not compatible with Boolean and char. – Destination type is larger than source type.Example

• byte int• int long

Type Conversion (Narrowing Conversion)

• Narrowing conversion is also known as downcasting.• Broader data type is converted into narrower

data type with loss of information– Process is called casting (explicit type conversion)– Target variable = (Target-type) Source variable– int b;– float a=50.67– b=(int)a;– Truncation??????

Identifier

• An identifier is a series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ($) that does not begin with a digit and does not containspaces.

• Some valid identifiers are Welcome1, $value, _value, m_inputField1and button7.

• The name 7button is not a valid identifier, because it begins with a digit.

• The name input field is not a valid identifier, because it contains a space.

Java Keywords

abstract boolean break byte casecatch char class continue defaultdo double else extends falsefinal finally float for ifimplements import instanceof int interfaceLong native new null packageprivate protected public return shortstatic super switch synchronized thisthrow throws transient true tryvoid volatile while const goto

Operators

• Mathematical Operators+, -, *, /, %

Comparison Operators==, <, >, <=, >=, !=

Logical Operators||, &&, !

What is the Result of this Expression?

• expression6 + 2 * 3 / 6

a) 7b) 0.5c) 13.0d) 4

Operator precedence and associativity

• To evaluate an arithmetic expression two concepts needs to be understood– Operator PrecedenceOperator precedence controls the order in which operations are performed– Operator Associativity The associativity of an operator specifies the order in which operations of the same precedence are performed

Primitive data types vs. non primitive data types.

• Built in data types, like int, float and char, are also called primitive data types.

• Primitive data types hold only value.• Non-primitive data types are classes like String

or other user defined classes.• Non-primitive data types holds both value and

operations