JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Similar documents
The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Operators. Java operators are classified into three categories:

Chapter 3: Operators, Expressions and Type Conversion

CSC 1214: Object-Oriented Programming

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Language Fundamentals Summary

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Informatics Ingeniería en Electrónica y Automática Industrial

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

3. Java - Language Constructs I

Operators and Expressions

Operators in java Operator operands.

SECTION II: LANGUAGE BASICS

More Programming Constructs -- Introduction

JAVA Programming Fundamentals

Introduction to Programming Using Java (98-388)

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Java Notes. 10th ICSE. Saravanan Ganesh

Java Basic Programming Constructs

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CT 229. Java Syntax 26/09/2006 CT229

COMP6700/2140 Data and Types

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Data Handing in Python

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Computers Programming Course 6. Iulian Năstac

COMP Primitive and Class Types. Yi Hong May 14, 2015

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

JAVA OPERATORS GENERAL

DEPARTMENT OF MATHS, MJ COLLEGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Prof. Navrati Saxena TA: Rochak Sachan

Chapter 7. Expressions and Assignment Statements ISBN

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Lecture Set 4: More About Methods and More About Operators

COMP6700/2140 Operators, Expressions, Statements

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

A flow chart is a graphical or symbolic representation of a process.

Information Science 1

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Expressions & Assignment Statements

Chapter 7. Expressions and Assignment Statements

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Object Oriented Software Design

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

LECTURE 3 C++ Basics Part 2

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Introduction to Java & Fundamental Data Types

Basics of Java Programming

The Design of C: A Rational Reconstruction"

Bitwise Data Manipulation. Bitwise operations More on integers

Java Programming Fundamentals. Visit for more.

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Variables and literals

Java Primer 1: Types, Classes and Operators

Unit 2: Java in the small

CT 229 Java Syntax Continued

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Type Conversion. and. Statements

Data Types and Variables in C language

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Expressions and Assignment Statements

Infix to Postfix Conversion

Programming Languages: Part 2. Robert M. Dondero, Ph.D. Princeton University

Expressions and Casting

Introduction. Assessment Test. Part I The Programmer s Exam 1

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Unit-2 (Operators) ANAND KR.SRIVASTAVA

C - Basics, Bitwise Operator. Zhaoguo Wang

Operators in C. Staff Incharge: S.Sasirekha

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Zheng-Liang Lu Java Programming 45 / 79

Index COPYRIGHTED MATERIAL

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

The Design of C: A Rational Reconstruction

Lecture Set 4: More About Methods and More About Operators

UNIT- 3 Introduction to C++

ISA 563 : Fundamentals of Systems Programming

Language Reference Manual simplicity

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

The Warhol Language Reference Manual

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Quick Reference Guide

Expressions and Precedence. Last updated 12/10/18

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Course Outline Introduction to C-Programming

Types and Expressions. Chapter 3

Programming. Elementary Concepts

Continued from previous lecture

Transcription:

JAC444 - Lecture 1 Introduction to Java Programming Language Segment 4 1

Overview of the Java Language In this segment you will be learning about: Numeric Operators in Java Type Conversion If, For, While, Do-While Statements Labeled Break and Labeled Continue Arrays and Strings 2

Numeric operators in Java Prec Operator Description Example 1 ++ Increment by 1(or 1.0) k++ 1 -- Decrement by 1(or 1.0) k-- 1 + Unary plus +value 1 - Unary minus -value 2 * Multiplication x * y 2 / Division x / y 2 % Modulo x % y 3 + Addition x + y 3 - Subtraction x - y 5 < Less than x < y 5 > Greater than x > y 5 <= Less than/equal x <= y 5 >= Greater than/equal x >= y 6 == Equals (identical values) x == y 6!= Is not equal to x!= y 13 op= op assignment(+ =, - =, *=, etc) x += y 3

Bitwise and logical operators Pre Operator Type Description Example 1 ~ Integral Unary bitwise complement ~x 1! Logical Unary logical complement!b 4 << Integral Left shift x << 2 4 >> Integral Right shift (keep sign) x >> 5 4 >>> Integral Right shift (zero fill) x >>> 3 5 instanceof Obj type Tests class membership o instanceof X 6 == Object Equals (same object) x == y 6!= Object Unequal (different object) x!= y 7 & Integral Bitwise AND x & y 7 & Logical Logical AND b1 & b2 8 ^ Integral Bitwise XOR x ^ y 8 ^ Logical Logical XOR b1 ^ b2 9 Integral Bitwise OR x y 9 Logical Logical OR b1 b2 4

Bitwise and logical operators Prec Operator Type Description Examp. 10 && Logical Logical AND (short-circuit) b1 && b2 11 Logical Logical OR (short-circuit) b1 b2 12?: Logical Conditional (ternary) b? x : y 13 = Variable,any Assignment x = 1 13 <<= Binary Left shift with assignment x <<= 2 13 >>= Binary Right shift with assignment x =>> 3 13 >>>= Binary Right shift, zero fill, assign x =>> 4 13 &= Binary Bitwise AND with assign x &= y 13 &= Logical Logical AND with assign b1 &=b2 13 = Binary Bitwise OR with assignment x = y 13 = Logical Logical OR with assignment b1 =b2 13 ^= Binary Bitwise XOR with assignment x ^= y 13 ^= Logical Logical XOR with assignment b1 ^=b2 5

Bitwise logic rules AND 0 1 0 0 0 1 0 1 OR 0 1 0 0 1 1 1 1 XOR 0 1 0 0 1 1 1 0 0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1 0 0 = 0 0 1 = 1 1 0 = 1 1 1 = 1 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 6

Bitwise operations on short primitives (16 bit integers) Binary Operation Decimal Hex 0000 0000 0101 0100 op1 84 0x0054 0000 0001 0100 0111 op2 327 0x0147 0000 0000 0100 0100 op1 & op2 68 0x0044 0000 0001 0101 0111 op1 op2 343 0x0157 0000 0001 0001 0011 op1 ^ op2 275 0x0113 1111 1110 1011 1000 ~op2-328 0xFEB8 7

Results of some bit-shifting 0000 0000 0000 0000 0000 0000 0110 0011 starting a = 99 0 0 0 0 0 0 6 3 a = 0x00000063 0000 0000 0000 0000 0000 0011 0001 1000 after a << 3 (792) 0 0 0 0 0 3 1 8 a = 0x00000318 0000 0000 0000 0000 0000 0000 0001 1000 after a >> 2 (24) 0 0 0 0 0 0 1 8 a = 0x00000018 1111 1111 1111 1111 1111 1111 1001 1101 stating b = -99 F F F F F F 9 D b = 0xFFFFFF9D 1111 1111 1111 1111 1111 1111 1111 1001 after b >> 4 = -7 F F F F F F F 9 b = 0xFFFFFFF9 0000 0000 0000 1111 1111 1111 1111 1111 after b >>> 12 = 1048575 0 0 0 F F F F F b = 0x000FFFFF 8

Type Conversions Java is a strong typed language Implicit conversion for primitive value: any numeric value can be assigned to any numeric value whose type supports a larger range of values. byte short int long float double Explicit conversion casting. boolean type doesn t allow any casting at all. A char can be cast to any integer type and vice versa excepting to a short type. When chart is cast to int type upper bits are filled with zeros. Attention: interger types are converted by chopping off the upper bits. If the larger integer has a value outside the range off the smaller type, dropping the upper bits changes the value, including possibly changing sign. Ex: short x = -129; byte y = (byte)x; What is the value of y??? 9

if, if-else, if else if else 1. if (boolean-expression) { 2. if (boolean-expression) { else { 3. if (boolean-expression) { else if (boolean-expresion ) { else { 10

for Statement A for statement should have the following form: for (initialization; condition; update) { for (k = 0, flag; k < 10 && flag; k++ ) {... Enhanced for loop for (variable : Collection ) {... 11

while, do - while Statements while, do-while and for control looping are classified as iteration statements. while (condition) { do { while (condition); 12

break - Labeled break A break drops out of the bottom of the loop. The break statement with no label attempts to transfer control to the innermost enclosing switch, for, while or do-while of immediately enclosing statement. A labeled break drops out of the bottom of the end of the loop denoted by the label. Ex: out: for (int i = 0; i < 10; i++ ) { for (int k = 0; k < 10; k++) { if (i == k) break out; System.out.println(i); 13

continue Labeled continue A plain continue goes to the top of the innermost loop and continues A labeled continue goes to the label and re-enters the loop right after that label Ex: Calculates the factorials of odd number outerloop: for (int i = 0; i < limit; i++ ) { for (int k = 2; k < i; k++) { if (i % 2) continue outerloop; factory *= i; 14

switch The switch is classified as a selection statement switch (integral-selector) { case integral-value1: break; default: Integral-selector is an expression that produces an integral value. The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (simple or compound) executes. If no match occurs, the default statement executes. 15

Array An array is simply a sequence of either objects or primitives, all the same type and accessed together under one identifier name. Arrays are implicit extensions of Object. Arrays are defined and used with square-brackets indexing operator [ ] int[] integerarray = new int[3]; A Java array is guaranteed to be initialized and cannot be accessed outside of its range. for (int k = 0; k < integerarray.length; k++) integerarray[k] = k; Creating an array of objects, one is really creating an array of references, and each of those references is automatically initialized to null. Student[] room = new Student[3]; 16

Strings Java strings are standard objects with built-in language support. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of String class Strings are constant; their values cannot be changed after they are created. Stringbuffer class supports mutable strings. Because String objects are immutable they can be shared String str = "abc"; is equivalent to: char data[] = {'a', 'b', 'c'; String str = new String(data); 17

String Examples Here are some more examples of how strings can be used: System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2,3); String d = cde.substring(1, 2); int length(); returns the length of this string. char charat(int index); returns the character at the specified index. 18

Conclusions After completion of this lesson you should know: How to write a simple Java Program How to work with primitives: integers and floats To use Java tools like: compiler and interpreter 19