CT 229. Java Syntax 26/09/2006 CT229

Similar documents
CT 229 Java Syntax Continued

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

JAVA OPERATORS GENERAL

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

Operators. Java operators are classified into three categories:

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Prof. Navrati Saxena TA: Rochak Sachan

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

The Arithmetic Operators

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

Chapter 3: Operators, Expressions and Type Conversion

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

SECTION II: LANGUAGE BASICS

Operators in C. Staff Incharge: S.Sasirekha

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

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

Chapter 4. Operations on Data

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSC 1214: Object-Oriented Programming

The Java language has a wide variety of modifiers, including the following:

Operators in java Operator operands.

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Operators and Expressions

ISA 563 : Fundamentals of Systems Programming

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

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Unit-2 (Operators) ANAND KR.SRIVASTAVA

CS Programming I: Branches

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

More Programming Constructs -- Introduction

Computers Programming Course 6. Iulian Năstac

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Fundamentals of Programming

LECTURE 3 C++ Basics Part 2

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

Declaration and Memory

COMP6700/2140 Operators, Expressions, Statements

Data Types and Variables in C language

CT 229 Java Syntax Continued

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CIS 110: Introduction to Computer Programming

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

Java Basic Programming Constructs

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

Learning the Java Language. 2.1 Object-Oriented Programming

Expression and Operator

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

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Information Science 1

Chapter 2: Using Data

Programming in C++ 5. Integral data types

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

Arithmetic-logic units

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Expressions and Precedence. Last updated 12/10/18

Java Primer 1: Types, Classes and Operators

DEPARTMENT OF MATHS, MJ COLLEGE

Operators and Expressions:

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

JAVA Programming Fundamentals

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

UNIT- 3 Introduction to C++

.Net Technologies. Components of.net Framework

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

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

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

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Computer System and programming in C

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Introduction to Computer Science Midterm 3 Fall, Points

Chapter 6 Primitive types

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

cast int( x float( x str( x hex( int string int oct( int string int bin( int string int chr( int int ord( ch

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

ECE Digital System Design & Synthesis Exercise 1 - Logic Values, Data Types & Operators - With Answers

CS111: PROGRAMMING LANGUAGE II

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Chapter 7. Expressions and Assignment Statements ISBN

Fundamental of Programming (C)

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

Reserved Words and Identifiers

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Zheng-Liang Lu Java Programming 45 / 79

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

Transcription:

CT 229 Java Syntax 26/09/2006 CT229

Lab Assignments Assignment Due Date: Oct 1 st Before submission make sure that the name of each.java file matches the name given in the assignment sheet!!!! Remember: Electronic Submission @ http://ecrg-vlab01.it.nuigalway.ie/uploader/submissions.html Where does Eclipse put my.java files???? By default Eclipse will put your.java files in your F:\ drive. Look in your F:\My Documents\workspace\<name of project> 26/09/2006 CT229 1

Tutorials Tutorial Commence on the Tues 3 rd of October and Thurs 5 th of Oct. Tutorials 1hr/Week BE(EE) Tues AC213 (12-1) BE(E&CE) Tues AC213 (12-1) BSc(IT) Thurs IT203 (12-1) BSc(P&A) Thurs IT203 (12-1) 26/09/2006 CT229 2

Lab Issues How to use the Java API? Code Completion in Eclipse Concatenation of Strings and Variables 26/09/2006 CT229 3

Operands and Operators?? Operand: A quantity upon which a mathematical operation is performed Can be a variable or expression An operator performs a function on 1, 2 or 3 operands Unary (num1++), binary (num1+num2) or ternary operator (grade = (assignmentmark>40)? pass : fail ) Operator types: Arithmetic Relational: relationship between operands (compare size, etc.) Boolean: and, or Assignment: assign value to variable Bitwise: manipulate individual binary bits Others: miscellaneous 26/09/2006 CT229 4

Arithmetic Operators Addition 5 + 2 is 7 Subtraction 5-2 is 3 Multiplication 5 * 2 is 10 Division 5 / 2 is 2 Remainder 5 % 2 is 1 Increment ++x (adds 1 to value of x) Decrement --x (subtracts 1 from x) Have pre-increment and post-increment (and decrement) Pre ++x carried out before expression is evaluated Post x++ carried out after expression is evaluated 26/09/2006 CT229 5

Pre and Post Increment Example Example int x = 5; System.out.println( Value of x is + (++x) ); System.out.println( Value of x is + (x++) ); System.out.println( Value of x is + (x) ); Output The value of x is 6 The value of x is 6 The value of x is 7 26/09/2006 CT229 6

Relational Operators Compare two numerical operands; return true or false Used with if and other decision-making statements Operator Use Returns true if > op1 > op2 op1 is greater than op2 >= op1 >= op2 op1 is greater than or equal to op2 < op1 < op2 op1 is less than op2 <= op1 <= op2 op1 is less than or equal to op2 == op1 == op2 op1 and op2 are equal!= op1!= op2 op1 and op2 are not equal 26/09/2006 CT229 7

Boolean Operators Used with relational operators; normally combine results of relational expressions to form more complex expressions Operator Use Returns true if && op1 && op2 op1 and op2 are both true [and] op1 op2 either op1 or op2 is true [or]!! op op is false [not] & op1 & op2 op1 and op2 are both true [and] op1 op2 either op1 or op2 is true [or] ^ op1 ^ op2 Just one of op1 and op2 is true [xor] 26/09/2006 CT229 8

Boolean Operators: Summary a b a && b a b a ^ b! a false false false false false true false true false true true true true false false true true false true true true true false false Note: C uses 0 and 1 rather than true and false for Boolean operations 26/09/2006 CT229 9

Boolean Operators What is the difference between && and & and The operators && and perform short circuit logical expressions. That is they only evaluate the second operand if required. Example boolean condition1 = false; boolean condition2 = true; if ( (condition1) && (condition2) ) if ( (condition1) & (condition2) ) Example: booleanexample.java 26/09/2006 CT229 10

Assignment Operators Several assignment operators apart from simple = All are abbreviated forms of simple assignment One corresponding to each binary arithmetic operator Left side of assignment must always be a variable (l-value) Operator Use Equivalent To += op1 += op2; op1 = op1 + op2; -= op1 -= op2; op1 = op1 - op2; *= op1 *= op2; op1 = op1 * op2; /= op1 /= op2; op1 = op1 / op2; %= op1 %= op2; op1 = op1 % op2; 26/09/2006 CT229 11

Bitwise Operators Operate on int (or long or byte) variables Treat number as sequence of binary digits (bits) Op. Use Equivalent To & op1 & op2 bitwise and op1 op2 bitwise or ^ op1 ^ op2 bitwise xor ~ ~op2 bitwise complement >> op1 >> op2 shift bits of op1 right by distance op2 << op1 << op2 shift bits of op1 left by distance op2 >>> op1 >>> op2 shift bits of op1 right by distance op2 (unsigned) Note: &, I and ^ can be boolean or bitwise operators, depending on whether operands are boolean or integer 26/09/2006 CT229 12

Bitwise Operators Like boolean operations, except with 0 for false, 1 for true Integers expressed in binary form Calculation done bit-by-bit op1 op2 op1 & op2 op1 op2 op1 ^ op2 ~op1 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 26/09/2006 CT229 13

Example Bitwise Operations Byte-Sized Example NOT ~10110010 -------- 01001101 ~178 = 77 AND Example: bitwise.java 10110010 & 11011011 -------- 10010010 178 & 219 = 146 XOR 10110010 ^ 11011011 -------- 01101001 OR 10110010 11011011 -------- 11111011 178 ^ 219 = 105 178 219 = 251 26/09/2006 CT229 14

Bitwise Right Shift Operators Arithmetic or signed right shift (>>) is used as follows: First operand is divided by 2 raised to the power of the second operand. 2 2 128 >> 1 returns 128/ = 64 256 >> 4 returns 256/ = 16 4-256 >> 4 returns -256/ 2 = -16 The sign is copied during the shift A logical or unsigned right-shift operator (>>>) is: Used for bit patterns The sign bit is not copied during the shift 178 >>> 2 => 10110010 >>> 2 == 101100 1 4 10000000 >> 1 Answer: 1000000 26/09/2006 CT229 15

Bitwise Left-Shift Operator The left-shift bitwise operator (<<) is used as follows: The first operand is multiplied by two and raised to the 1 128 << 1 returns 128 * 2 = 256 10000000 << 1 => 100000000 2 16 << 2 returns 16 * 2 = 64 26/09/2006 CT229 16

Other Operators Operator Description + Concatenates (joins) two String operands name = firstname + surname;? : Shortcut if-else statement result = (mark >= 40)? "Pass" : "Fail"; [] Used to declare arrays, create arrays, and access array elements. Used to form qualified names ( type ) Casts (converts) a value to the specified type instanceof True if first operand (object name) is instance of second operand (class name) 26/09/2006 CT229 17