A Java program contains at least one class definition.

Size: px
Start display at page:

Download "A Java program contains at least one class definition."

Transcription

1 Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public static void main(string[] args) { System.out.println("Hello, world!") ; This code defines a class named Hello. The definition of Hello must be in a file Hello.java. The method main is the code that runs when you call java Hello. When we give an element in a program a name, we call that name an identifier. In the previous example, Hello was an identifier. In Java, identifiers: Always start with a letter. Can include letters, digits, underscore ( ) and the dollar sign symbol ($). Must be different from any Java reserved words (or keywords). Keywords that we ve seen so far include: public, static, class and void. Case Sensitivity Whitespace Comments Identifiers and keywords in Java are case sensitive. In other words, capitalization matters. Keywords are always in lowercase. The following identifers are all different: foobar Foobar FooBar FOOBAR Even so, it would probably be a bad idea to use more than one of them in the same program. We use the word whitespace to describe blanks, tabs and newline characters. The Java compiler ignores whitespace except when it is used to separate words. E.g.: y=m*x+b;total=total+y; Is the same as: y = m*x + b ; total = total + y ; But which is easier to read? To make our code understandable to those who come after us, we comment sections whose purpose is not immediately obvious. // this comment ends at a newline /* this comment goes until it is explicitly ended with a: */ The Java compiler treats comments as if they were whitespace it ignores them unless they separate words. I.e., foo/*hi, mom!*/bar becomes foo bar, not foobar. Variables Types Booleans A variable is a location in memory where data is stored. Every variable is associated with an identifier. We can assign a value to a variable using the assignment operator =. x = 12 ; The assignment operator means take the value on the right-hand side and store it in the memory location on the left-hand side. The values a variable can take on and the operations that can be performed on it are determined by its type. Java has five categories of types: Booleans Characters Integers Floating-point numbers References to objects Boolean variables can only take on the values true or false. They are often used to test for conditions in a program. boolean t = true ; boolean f = false ;

2 Characters Integers Integers Types Character variables can store one character. A character value is a character surrounded by single quotes: char q = Q ; Some special characters are: \n newline \t tab \ single quote \" double quote \\ backslash The integers are the infinite set: Z = {..., 1, 0, 1,.... Obviously, we can t represent all of the integers in one variable. We have to choose an n-bit encoding that can represent 2 n integers. The Java integer types represent both positive and negative integers. An n-bit integer x can represent the range: 2 n 1 x < 2 n 1 The Java integer types are: E.g.: byte short int long byte b = 127 ; short s = ; int i = 2 ; 8 bits 16 bits 32 bits 64 bits Integer Literals Integer Conversions Integer Conversions: 2 An integer value, or literal, can be written in decimal, hex or octal (base-8): A hex literal starts with 0x, e.g.: 0x1F (= ) An octal literal starts with just 0, e.g.: 072 (= ) A decimal literal is just a regular number that doesn t start with 0, e.g.: 123 Integer literals are by default of type int. A long literal ends with L. If an int literal is small enough to fit into a byte or a short, it will be automatically converted. The same is true for long literals and int, byte and short. byte b = 0x7F ; /* 7 bits, OK */ short s = 0x7FFF ; /* 15 bits, OK */ int i = 0x L /* 29 bits, OK */ byte b2 = 0xFF ; /* Error: 255 > 127 */ int i2 = 0x ABCDEFL /* Error: way too big, 57 bits */ If a literal is too big for its target variable, you must explicitly convert it using a type cast. The number is converted by truncating the extra bits, which is probably not what you want. /* 0x100 = 256 */ byte b = (byte) 0x100 ; /* b now equals 0! */ An int literal can always be assigned to a long variable its value will be the same as if it was assigned to an int variable. Floating-Point Numbers Floating-Point Types Floating-Point Literals Floating-point numbers are used to represent the reals (R), i.e., numbers that may have fractional parts. The floating-point representation uses a form of scientific notation: = = = = A floating-point variable uses some of its bits to store the exponent and some of them to store the fractional part (or significand). Thus, floating-point numbers are constrained by both magnitude and precision. The Java floating-point types are: float double 32 bits 64 bits Floating-point literals are decimal numbers with an optional decimal point You may also include an exponent n, which multiplies the literal by 10 n : 1.234e2 9.9e-1 55e0.02e1 A floating-point literal is by default of type double. A float literal ends with F.

3 Floating-Point Conversions The only automatic conversion between floating-point types is the assignment of a float value to a double. double d = 1.23F ; /* OK */ float f = 5.99 ; /* Error: cannot assign double to float */ When an integer literal is assigned to a floating-point type, it is automatically promoted to floating-point, even if that means a loss of precision. float f = 2 ; /* OK, f = 2.0 */ float f2 = L ; /* OK, but f2 = */ Objects You may have heard that Java is an object-oriented programming language. What does that mean? An object is a collection of data and operations that manipulate that data. public class Circle { int x ; int y ; double radius ; Object-Oriented Programming In object-oriented programming (or OOP), we try to define all of our data as objects, and we define the program as interaction between those objects. public class HappyFace { Circle head ; SemiCircle smile ; void draw() { head.draw() ; smile.draw() ; Object-Oriented Programming: 2 OOP encourages us to think of objects as modules reusable components. If we have Circle, we can use it to make HappyFace. If we have HappyFace, we can use it to make StickMan. public class StickMan { HappyFace head ; Line body ; Line arm1, arm2 ; Line leg1, leg2 ; Defining Object Types An object is usually a noun, a thing. To define an object, we first need to define what kind of a thing it is. In Java, we use the keyword class: public class Car { String make ; int model_year ; Color color ; int max_occupants ; Defining Object Types: 2 Now that we ve defined a class, we can create instances of the class (i.e., objects). In order to create an instance, we need to tell Java how to initialize the object. We do this using a constructor. public class Car { public Car(String mk, int yr, Color c, int max) { make = mk ; model_year = yr ; color = c ; max_occupants = max ; Defining Objects Now we can use the keyword new to invoke the constructor and create an instance of the class. We can create any kind of a car we need by changing the parameters to the constructor. Car suv = new Car("Ford Explorer", 1999, Color.BLUE, 5) ; Car mini = new Car("Cooper Mini", 2002, Color.RED, 4) ; The Type of an Object An object s type is its class. A Car variable can only take the value of a Car object, or an object that is compatible with Car. (We ll talk about what it means for an object to be compatible next week.) Car suv = new Car("Ford Explorer", 1999, Color.BLUE, 5) ; Car c = suv ; /* OK: suv is a Car */ StickMan s = suv ; /* Error: suv is not a StickMan */ References We said earlier that one of the categories of Java types is references to objects. What is a reference? We said that a variable is a location in memory. When we declare an integer variable x and assign it a value 123, the location in memory set aside for x contains the binary representation of 123. x n + 1 n n 1 true e23

4 References: 2 A variable of an object type is a reference variable. When we declare a Car variable suv, the location in memory set aside for suv contains a reference to the location of the data members of suv. suv n + 1 n n e23 ref X "Ford Explorer" 1999 Color.BLUE 5 Reference Assignment Assignment means copy the value on the right-hand side into the memory location on the left-hand side. If you assign one variable to another, the value is copied and that is that. int x = 0 ; int y = x ; /* x and y both equal 0 */ x = 2 ; /* x now equals 2, y still equals 0 */ Reference Assignment: 2 When you assign the value of one reference variable to another, it s the reference that s copied, not the object. This can lead to surprising results. Point p = new Point(0,0) ; Point q = p ; /* p and q now refer to the same object */ p.x = 2 ; /* p.x now equals 2 q.x now equals 2, as well */ Reference Assignment: 2 When you copy the reference value, you still have only one copy of the object data. You just have two references to it. p q 0xFFFFFFFF ref ref false x: 2 y: 0 This may seem confusing at first, but there s a simple reason for it: copying data is slow. We avoid it when we can. Strings String is a class that has special support in Java. A string literal is surrounded by double quotes. String hamlet = "To be or not to be" ; String is a reference type, but you don t have to use the new operator to create an instance. You can assign a string literal to a String variable directly, as above. Strings: 2 Character and string values may seem confusingly similar. A char value is a single character, surrounded by single quotes: char c1 = C ; char c2 = \n ; A String value is a sequence of characters surrounded by double quotes. A String may be empty. String s1 = "C" ; String s2 = "" ; String s3 = "LastName\tFirstName\tGrade\n" ; /* Words separated by tabs, closed with a newline. */ Arithmetic Java provides five basic arithmetic operators: + addition - subtraction * multiplication / division % remainder There are also unary + and - operators. The operators can be applied to any of the integer or floating-point types. Operator Precedence Multiplication, division and remainder have higher precedence than addition and subtraction. All higher-precedence operators are evaluated before any lower-precedence operators. Operators at the same precedence are evaluated left-to-right. Parentheses can be used to override operator precedence. x+y*z = x+(y*z) a*b+c%d = (a*b)+(c%d) Assignment is the lowest-precedence operator of all. Unary + and - are higher-precedence than multiplication. Integer Arithmetic Integer division is grade-school division: fractional results round toward zero. 9/2 = 4-9/2 = -4 Integer division and remainder obey the rule: (x/y)*y + x%y = x 9%2 = 1-9%2 = -1

5 Overflow and Underflow If the result of an operation is greater than the maximum value (overflow) or less than the minimum value (underflow) of its type, the result wraps around. Integer.MAX VALUE + 1 = Integer.MIN VALUE Integer.MIN VALUE - 1 = Integer.MAX VALUE All arithmetic is performed with int and long values. byte and short values are automatically promoted. Typecasting The result of an integer arithmetic operation is always an int or long. This means you have to cast back to a smaller type when you assign a result. byte b = 1 ; byte b2 = b + 1 ; /* Error: can t assign int to byte */ byte b3 = (byte)( b + 1 ) ; /* OK: b3 now equals 2 */ byte b4 = (byte)( Byte.MAX_VALUE + 1 ) ; /* OK: b4 now equals Byte.MIN_VALUE */ Floating-point Arithmetic Floating-point arithmetic works pretty much how you would expect, except you have to be careful about precision. float f = 1.0e8F, g = 1.0e-8F ; float x = f + g ; /* x now equals 1.0e8 */ float y = (f - f) + g ; /* y now equals 1.0e-8 */ float z = f - (f + g) ; /* z now equals 0 */ Special Floating-point Values Floating-point arithmetic has several special values that behave in interesting ways. +Inf -Inf Infinity +0.0 Zero Negative Infinity -0.0 Negative Zero NaN Not a Number Special Floating-point Values: 2 Here s how to get and what you get from, these special values: 1/0.0 = Inf 1/-0.0 = -Inf 1/Inf = 0.0 1/-Inf = -0.0 Inf/Inf = NaN 1+Inf = Inf 1%Inf = 1 Inf*0.0 = NaN 1-Inf = -Inf Inf%1 = NaN Increment and Decrement Adding or subtracting one from a number is a common operation. Java provides a short-hand in the increment (++) and decrement (--) operators. Increment and decrement can be used as prefix or postfix operators. In prefix form, increment means add one to this variable and use the incremented value in this expression. In postfix form, increment means add one to this variable, and use the previous value in this expression. Aside: C also has increment and decrement. The developers of C++ wanted to indicate it was an incremental improvement on C. Increment and Decrement: Examples int i = 0, j ; i++ ; /* i = 1 */ i-- ; /* i = 0 */ j = i++ ; /* i=1, j=0 */ j = ++i ; /* i=2, j=2 */ j = --i ; /* i=1, j=1 */ j = i-- ; /* i=0, j=1 */ Increment and Decrement: Precedence Increment and decrement are higher-precedence than any operator we ve seen so far. Postfix is higher-precedence than prefix. Highest Lowest = ++, -- (postfix) ++, -- (prefix), +, - (unary) *, /, % +, - Comparison Operators Java provides the following operators for comparing numbers: > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to!= Not equal to == and!= can be applied to boolean values, but >, <, >= and <= cannot. The result of a comparison operation is a boolean value.

6 Reference Comparison == and!= can be applied to reference variables. They test whether the references are equal, not the data members. Point x = new Point(0,0) ; Point y = new Point(0,0) ; Point z = x ; boolean b = x==y ; /* false, x and y are references to different objects */ boolean b2 = x==z ; /* true, x and z are references to the same object */ The instanceof Operator Occasionally we want to investigate the type of an object. The instanceof operator tells us if a reference variable is an instance of a class. Point x = new Point(0,0) ; boolean a = x instanceof Point ; /* true, x is a Point */ boolean b = x instanceof Car ; /* false, x is not a Car */ Comparison Operators: Precedence The relational operators have higher precedence than the equality operators. All of the comparison operators have lower precedence than the arithmetic operators. Highest Lowest = ++, -- (postfix) ++, -- (prefix), +, - (unary) *, /, % +, - <, >, <=, >=, instanceof ==,!= Logical Operators Java provides the following logical operators: & AND OR ˆ XOR! NOT && short-circuit AND short-circuit OR The operands of a logical operator are boolean values and the result is also a boolean. AND AND is true if and only if both of its operands are true. a b a&b false false false false true false true false false true true true OR OR is true if one of its operands are true. a b a b false false false false true true true false true true true true XOR XOR is true if and only if exactly one of its operands is true. a b aˆb false false false false true true true false true true true false NOT NOT inverts its operand. a!a false true true false Short-circuit AND and OR The logical AND and OR operators ( & and ) always evaluate both of their operands. In some cases, this can be wasteful. If the first operand of AND is false, then AND must be false. Short-circuit AND ( && ) doesn t evaluate the second operand if the first is false. If the first operand of OR is true, then OR must be true. Short-circuit OR ( ) doesn t evaluate the second operand if the first is true.

7 Short-circuit Operators: Example boolean a = true, b = false ; int i = 0, j = 1, k = 2 ; boolean c = a (i=j) < k ; /* c = true, i = 0 */ boolean d = a && (i=j) < k ; /* d = true, i = 1 */ boolean e = a && (i=k) < j ; /* e = false, i = 2 */ Logical Operators: Precedence AND has higher precedence than OR. The short-circuit operators have lower precedence than their ordinary equivalents. All of the logical operators have lower precedence than arithmetic and comparison. Highest Arithmetic, etc. <, >, <=, >=, instanceof ==,!= & ˆ Lowest = && Bitwise Operators Java provides the following operators for manipulating bit patterns: & Bitwise AND Bitwise OR ˆ Bitwise XOR << Shift left >> Shift right signed >>> Shift right unsigned &, and ˆ are overloaded operators. They are logical operators when applied to boolean values, and bitwise operators when applied to integers. Bitwise AND Bitwise AND applies the AND operation to every pair of bits in the operands. A 0 is treated as false and a 1 is treated as true. Bitwise OR Bitwise OR applies the OR operation to every pair of bits in the operands. Bitwise XOR Bitwise XOR applies the XOR operation to every pair of bits in the operands. 0xF6 & 0x93 = AND = xF6 0x93 = OR = xF6 ˆ 0x93 = XOR = Shift left The shift left operator shifts the bits in its first operand as indicated by its second operand. 0xF6 << 2 = << Shift right signed The way integers are represented in Java, the highest-order bit is the sign of the number. If we shift right the same way we shift left (bringing in zeroes), the sign might changed. The shift right signed operator brings in bits that match the sign >> 2 0xF6 >> 2 = Shift right unsigned The shift right unsigned operator ignores the sign bit and brings in zeroes >>> 2 0xF6 >>> 2 =

8 Using Bitwise Operators Bitwise AND is good for turning bits off: int i = 0xC7 & 0xFE ; /* i = 0xC6 */ Bitwise OR is good for turning bits on: int i = 0xC7 0x08 ; /* i = 0xCF */ Bitwise XOR is good for flipping bits: int i = 0xC7 0x09 ; /* i = 0xCE */ Bitwise Operators: Precedence The precedence of bitwise AND, OR and XOR match their logical equivalents. The precendence of the shift operators is between the arithmetic and comparison operators. Highest Lowest Unary operators *, /, % +, - <<, >>, >>> <, >, <=, >=, instanceof ==,!= Logical operators, assignment String Concatenation The + is also overloaded. When applied to two strings, it concatenates them. I.e., it appends the second to the first. String a = "Flower" ; String b = "Power" ; String c = a + b ; /* c = "FlowerPower" */ String Concatenation: 2 All of the basic types are automatically converted to String under the concatenation operator. int i = 2 ; double x = 5.2 ; String s = "i = " + i + " and x = " + x ; /* s = "i = 2 and x = 5.2" */ Assignment Operators All of the binary operators have corresponding compound assignment operators. += %= &= -= >>= ˆ= *= <<= = /= >>>= x op= y is equivalent to x = x op y. All of the compound assignment operators have the same precedence as assignment.

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Declaration and Memory

Declaration and Memory Declaration and Memory With the declaration int width; the compiler will set aside a 4-byte (32-bit) block of memory (see right) The compiler has a symbol table, which will have an entry such as Identifier

More information

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

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

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Topic Notes: Bits and Bytes and Numbers

Topic Notes: Bits and Bytes and Numbers Computer Science 220 Assembly Language & Comp Architecture Siena College Fall 2010 Topic Notes: Bits and Bytes and Numbers Binary Basics At least some of this will be review, but we will go over it for

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

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

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

More information

Types and Expressions. Chapter 3

Types and Expressions. Chapter 3 Types and Expressions Chapter 3 Chapter Contents 3.1 Introductory Example: Einstein's Equation 3.2 Primitive Types and Reference Types 3.3 Numeric Types and Expressions 3.4 Assignment Expressions 3.5 Java's

More information

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers Computer Science 324 Computer Architecture Mount Holyoke College Fall 2007 Topic Notes: Bits and Bytes and Numbers Number Systems Much of this is review, given the 221 prerequisite Question: how high can

More information

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

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

COMP2121: Microprocessors and Interfacing. Number Systems

COMP2121: Microprocessors and Interfacing. Number Systems COMP2121: Microprocessors and Interfacing Number Systems http://www.cse.unsw.edu.au/~cs2121 Lecturer: Hui Wu Session 2, 2017 1 1 Overview Positional notation Decimal, hexadecimal, octal and binary Converting

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Topic Notes: Bits and Bytes and Numbers

Topic Notes: Bits and Bytes and Numbers Computer Science 220 Assembly Language & Comp Architecture Siena College Fall 2011 Topic Notes: Bits and Bytes and Numbers Binary Basics At least some of this will be review for most of you, but we start

More information

Inf2C - Computer Systems Lecture 2 Data Representation

Inf2C - Computer Systems Lecture 2 Data Representation Inf2C - Computer Systems Lecture 2 Data Representation Boris Grot School of Informatics University of Edinburgh Last lecture Moore s law Types of computer systems Computer components Computer system stack

More information

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

More information

COMP2611: Computer Organization. Data Representation

COMP2611: Computer Organization. Data Representation COMP2611: Computer Organization Comp2611 Fall 2015 2 1. Binary numbers and 2 s Complement Numbers 3 Bits: are the basis for binary number representation in digital computers What you will learn here: How

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one. http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

More information

Java Programming Fundamentals. Visit for more.

Java Programming Fundamentals. Visit  for more. Chapter 4: Java Programming Fundamentals Informatics Practices Class XI (CBSE Board) Revised as per CBSE Curriculum 2015 Visit www.ip4you.blogspot.com for more. Authored By:- Rajesh Kumar Mishra, PGT (Comp.Sc.)

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

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

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz) SOFTWARE DEVELOPMENT 1 Operators 2018W (Institute of Pervasive Computing, JKU Linz) OPERATORS Operators are required to form expressions. Depending on the number of operands they take, they are called:

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

More information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

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

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 Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

More information

SECTION II: LANGUAGE BASICS

SECTION II: LANGUAGE BASICS Chapter 5 SECTION II: LANGUAGE BASICS Operators Chapter 04: Basic Fundamentals demonstrated declaring and initializing variables. This chapter depicts how to do something with them, using operators. Operators

More information

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

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

4 Programming Fundamentals. Introduction to Programming 1 1

4 Programming Fundamentals. Introduction to Programming 1 1 4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,

More information

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

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

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

COMP Primitive and Class Types. Yi Hong May 14, 2015 COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto CS 170 Java Programming 1 Expressions Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 What is an expression? Expression Vocabulary Any combination of operators and operands which, when

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013 Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Chapter 2 Working with Data Types and Operators

Chapter 2 Working with Data Types and Operators JavaScript, Fourth Edition 2-1 Chapter 2 Working with Data Types and Operators At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics

More information

Primitive Data Types: Intro

Primitive Data Types: Intro Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( ) Sir Muhammad Naveed Arslan Ahmed Shaad (1163135 ) Muhammad Bilal ( 1163122 ) www.techo786.wordpress.com CHAPTER: 2 NOTES:- VARIABLES AND OPERATORS The given Questions can also be attempted as Long Questions.

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

More about Binary 9/6/2016

More about Binary 9/6/2016 More about Binary 9/6/2016 Unsigned vs. Two s Complement 8-bit example: 1 1 0 0 0 0 1 1 2 7 +2 6 + 2 1 +2 0 = 128+64+2+1 = 195-2 7 +2 6 + 2 1 +2 0 = -128+64+2+1 = -61 Why does two s complement work this

More information

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1 CS61B Lecture #14: Integers Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 1 Integer Types and Literals Type Bits Signed? Literals byte 8 Yes Cast from int: (byte) 3 short 16 Yes None. Cast

More information

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture) COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1 Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you

More information

CHAPTER 3 Expressions, Functions, Output

CHAPTER 3 Expressions, Functions, Output CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number

More information

Chapter 4. Operations on Data

Chapter 4. Operations on Data Chapter 4 Operations on Data 1 OBJECTIVES After reading this chapter, the reader should be able to: List the three categories of operations performed on data. Perform unary and binary logic operations

More information

Operators in java Operator operands.

Operators in java Operator operands. Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,

More information

DaMPL. Language Reference Manual. Henrique Grando

DaMPL. Language Reference Manual. Henrique Grando DaMPL Language Reference Manual Bernardo Abreu Felipe Rocha Henrique Grando Hugo Sousa bd2440 flt2107 hp2409 ha2398 Contents 1. Getting Started... 4 2. Syntax Notations... 4 3. Lexical Conventions... 4

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29 CS 31: Introduction to Computer Systems 03: Binary Arithmetic January 29 WiCS! Swarthmore Women in Computer Science Slide 2 Today Binary Arithmetic Unsigned addition Subtraction Representation Signed magnitude

More information

Information Science 1

Information Science 1 Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

More information

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore CS 115 Data Types and Arithmetic; Testing Taken from notes by Dr. Neil Moore Statements A statement is the smallest unit of code that can be executed on its own. So far we ve seen simple statements: Assignment:

More information

Numerical computing. How computers store real numbers and the problems that result

Numerical computing. How computers store real numbers and the problems that result Numerical computing How computers store real numbers and the problems that result The scientific method Theory: Mathematical equations provide a description or model Experiment Inference from data Test

More information

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a) Operators Representing and storing primitive data types is, of course, essential for any computer language. But, so, too, is the ability to perform operations on data. Java supports a comprehensive set

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Numerical Data CS 180 Sunil Prabhakar Department of Computer Science Purdue University Problem Write a program to compute the area and perimeter of a circle given its radius. Requires that we perform operations

More information

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3 Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Instructor: Nicole Hynes nicole.hynes@rutgers.edu 1 Fixed Point Numbers Fixed point number: integer part

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

CIS133J. Working with Numbers in Java

CIS133J. Working with Numbers in Java CIS133J Working with Numbers in Java Contents: Using variables with integral numbers Using variables with floating point numbers How to declare integral variables How to declare floating point variables

More information