Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

L EC. 02: D ATA T YPES AND O PERATORS (2/2) Fall Java Programming.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
1 Operators and Expressions Instructor: Mainak Chaudhuri
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Mathematical Calculations in Java Mrs. C. Furman.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Operators.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Building Java Programs
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Escape Sequences What if we wanted to print the quote character?
OPERATORS (2) CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Building Java Programs
Operators August 6, 2009.
Introduction to Programming – 4 Operators
Expressions and Assignment
elementary programming
Chapter 2 Programming Basics.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Building Java Programs
Names of variables, functions, classes
Building Java Programs
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators

Sahar Mosleh California State University San MarcosPage 2 Java is a strongly types language all operations are types checked by the compiler for type compatibility illegal operations will not be compiled Java contains two sets of data types object-oriented data types non-object-oriented data types Java object- oriented data types are defined by classes. Discussion of these types will be done later in the course

Sahar Mosleh California State University San MarcosPage 3 Primary data types Primary data types in Java are: booleanRepresents true or false value byte8-bit integer charCharacter doubleDouble-precision floating point floatsingle-precision floating point intinteger longLong integer shortShort integer

Sahar Mosleh California State University San MarcosPage 4 Character variables can be handles like integers class CharArithDemo{ public static void main(String args[]) { char ch; ch = ‘X’; System.out.println (“ch contains ” + ch); ch++; // increment ch System.out.println (“ch is now ” + ch); ch = 90; // give ch the value Z System.out.println( “ch is now ” + ch); } A char can be incremented A char can be assigned an integer value Note that although char is not integer type, in some case it can handled as integer Output: Ch contains X Ch is now Y ch is now Z

Sahar Mosleh California State University San MarcosPage 5 Boolean type Boolean types represents true/false. true and false are reserved words in Java class BoolDemo {public static void main(String args[]){ boolean b; b = false; System.out.println(“ b is: ” + b); b = true; System.out.println(“b is: ” + b); // a boolean value can control the if statement b = false; if (b) System.out.println(“This is not executed.”); // outcome of a relational operator is a boolean value System.out.println(“10 > 9 is ” + (10 > 9) ); } Output: b is false b is true 10 > 9 is true

Sahar Mosleh California State University San MarcosPage 6 More on variables Initializing a variable: One way to give a variable a value is through assignment statement For example, int count = 10; // giving count initial value of 10 char ch = ‘X’; // initializing ch to the value of ‘X’ floatf = 1.3F // f is initialized with 1.2 You can also declare two or more variables of the same type using comma-separated list For example, inta, b = 8, c = 19, d; // b and c are initialized

Sahar Mosleh California State University San MarcosPage 7 Operator Java has four different classes of operator: arithmetic, bitwise, relational, and logical Arithmetic operators include: + addition -subtraction *multiplication /division %module ++increment --decrement *, -, +, /, work mainly the same as other languages. % is the module operator. It works both for integer and floating- point numbers

Sahar Mosleh California State University San MarcosPage 8 class ModDemo { public static void main(String args[]) { int iresult, irem; double dresult, drem; iresult = 10 / 3; irem = 10 % 3; drem = 10.0 % 3.0; dresult = 10.0 / 3.0; System.out.println(“Result and remainder of 10/3: ” + iresult + “ ”+ irem); System.out.println(“Result and remainder of 10.0 /3.0: ” + dresult + “ ” + drem); } output: Result and remainder of 10 / 3: 3 1 Result and remainder of 10.0 / 3.0:

Sahar Mosleh California State University San MarcosPage 9 Examples of increment and decrement: X++meansX = X XmeansX = X + 1 Y = X++means Y = X and X= X+1 Y = ++Xmeans X = X+1 and Y = X The same logic works for decrement (X-- )

Sahar Mosleh California State University San MarcosPage 10 Relational operators are: = =!= > = Logical operators are: &And |OR ^XOR ! Not The outcome of the relational operators is a boolean value The result of a logical operation is also of type boolean

Sahar Mosleh California State University San MarcosPage 11 Suppose p and q are two boolean objects (literal, variable, expression), the following truth table holds pqp&qp|q!p!q TTTTFF TFFTFT FTFTTF FFFFTT For example, suppose c is a boolean variables and x=10, y = 20 c = ( x > 15) && (y = =20) In this case since the first operand (x>15) is false, the result of c is false no matter what the result of the second operand (y==20) is.

Sahar Mosleh California State University San MarcosPage 12 The Assignment operator The assignment operator is the single equal sign =. The general format of assignment operator is: var = expression The type of expression should match the type of the variable It is also possible to create a chain of assignments For example: int x, y, z x = y = z = 100; In this case, going from right to left, 100 is assigned to z which in turn z is assigned to y and the value of y is assigned to x Java also supports assignments for the following logical operators +=-=*=%=&=|=^= For example, a+=b means a = a + b