1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
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.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
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.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Data Types, Expressions and Functions (part I)
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC Programming I Lecture 5 August 30, 2002.
Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
C++ Programming: Basic Elements of C++.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
INSPIRING CREATIVE AND INNOVATIVE MINDS
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
IDENTIFIERS CSC 111.
Primitive and Reference Data Values
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Expressions and Assignment
elementary programming
Primitive Types and Expressions
Chapter 2 Variables.
Presentation transcript:

1 Fundamental Data Types

2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator Precedence  The Math Class  Assignment statement  Increment and Decrement operators  Writing Algebraic Expressions in Java  Math Functions: Examples  Casting

3 Primitive Data Types  Java has eight primitive data types as described below.  Other information is represented in Java as Objects. TypeSizeRange byte 1 byte-128 to 127 short 2 bytes-32,768 to 32,767 int 4 bytesabout –2 billion to 2billion long 8 bytesabout –10E18 to +10E18 float 4 bytes-3.4E38 to +3.4E38 double 8 bytes1.7E308 to 1.7E308 char 2 bytesA single character boolean 1 bytetrue or false

4 Variable Declaration  A variable can be declared to hold a data value of any of the primitive types.  A variable is a named memory location in which a value is stored.  A variable name is a sequence of letters and digits starting with a letter. int counter; int numStudents = 583; long longValue; long numberOfAtoms = L; float gpa; float batchAverage = 0.406F; double e; doublepi = 3.14; char gender; char grade = ‘B’; boolean safe; boolean isEmpty = true;

5 Numbers and Constants  By default, whole numbers are int and real numbers are double.  However, we can append a letter at the end of a number to indicate its type.  Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L): float maxGrade = 100f; double temp = 583d; float temp = 5.5; // Error as 5.5 is double float temp = 5.5f; long y = 583L; double x = 2.25e-6;  One use of the modifier final is to indicate symbolic constants.  By convention, symbolic constants are written in uppercase letters. Underscores separate words: final double SPEED_OF_LIGHT = 3.0E+10; final double CM_PER_INCH = 2.54; final int MONTH_IN_YEAR = 12;  Float and double numbers may be expressed in scientific notation: number * 10 exponent as: number E integerExponent or number e integerExponent

6 Arithmetic Operators OperatorDescription + Adds op1 and op2 - Subtracts op2 from op1 * Multiplies op1 by op2 / Divides op1 by op2 %Remainder of dividing op1 by op2  A simple arithmetic expression has the form: op1 Operator op2 where:

7 Arithmetic Operators (Cont’d)  The operators give results depending on the type of the operands.  If operand1 and operand2 are integer, then the result is also integer. But if either operand1 and/or operand2 is double, then the result is double.  Examples: Arithmetic expressionValue 1 / / / / / % 106

8 Arithmetic Operator Priority  An expression is a sequence of variables, constants, operators, and method calls that evaluates to a single value.  Arithmetic expressions are evaluated according to the priority rules.  All binary operators are evaluated in left to right order.  In the presence of parenthesis, evaluation starts from the innermost parenthesis. OperatorsPriority (Precedence) + - (unary)1 * / %2 + - (binary)3 ExpressionValue % 24 (2 – 5) * 5 / –

9 The Math class  Many mathematical functions and constants are included in the Math class of the Java library. Some are: Function /constantMeaning sqrt(x)Returns the square root of x. abs(x)Returns the absolute value of x, x can be double, float, int or long. cos(a), sin(a), tan(a) Returns the trigonometric cosine/sine/tangent of an angle given in radians exp(x)Returns the exponential number e raised to the power of x log(x)Returns the natural logarithm (base e) of x max(x, y), min(x, y)Returns the greater/smaller of two values, x and y can be double, float, int or long pow(x, y)Returns x y PIThe approximate value of PI  Syntax to call a function in the Math class: Math.functionName(ExpressionList)  Syntax to access a constant in the Math class: Math.ConstantName  Example: Math.PI * Math.max(4 * y, Math.abs(x – y))

10 Assignment Statement variable = expression;  The expression in the right is evaluated and the result is assigned to the variable in the left.  The left side must be a variable.  Examples: a = 5; b = a; b = b + 12; // valid: assignment operator is not equals operator c = a + b; a + b = c; // invalid: left side not a variable  Syntax:

11 Assignment Statement (cont’d)  To exchange (or to swap) the contents of two variables, a third variable must be used.  Example: double x = 20.5, y = -16.7, temp; temp = x; x = y; y = temp;

12 Short Hand Assignment Operators  Java provides a number of short hand assignment operators: Short-FormEquivalent 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  Example: a += 5; // equivalent to a = a + 5;

13 Increment and Decrement Operators  Increment/decrement operations are very common in programming. Java provides operators that make these operations shorter. OperatorUseDescription ++op++Increments op by 1; ++++opIncrements op by 1; --op--Decrements op by 1; ----op Decrements op by 1;  Example: int y = 20; x = 10, z; y++ ; z = x + y;

14 Writing Algebraic Expressions in Java  All operators must be explicit especially multiplications.  For a fraction, you must use parenthesis for the numerator or denominator if it has addition or subtraction. Algebraic expressionJava expression z = (4 * x + y) / x2 – 2 * y z = Math.sqrt(x + Math.pow(y, 2))

15 Example1  The following example computes the roots of a quadratic equation using the formula: Algorithm: »a = 1 »b = -5 »c = 6 »root1 = (-b + sqrt(b * b – 4 * a * c ) ) / ( 2 * a) »root2 = (-b - sqrt(b * b – 4 * a * c ) ) / ( 2 * a) »print root1, root2  Java code: public class QuadraticEquation { public static void main(String[] args) { double a = 1, b = -5, c = 6; double root1 = (-b + Math.sqrt(b*b - 4*a*c))/(2*a); double root2 = (-b - Math.sqrt(b*b - 4*a*c))/(2*a); System.out.println("The roots are: "+root1 + ","+root2); }

16 Example2  The following example calculates the area and circumference of circle.  Algorithm: »radius = 3 »area = pi * radius 2 »circumference = 2 * pi * radius »print area, circumference public class Circle { public static void main(String[]args) { double area, circumference; int radius = 3; area = Math.PI * Math.pow(radius, 2); circumference = 2 * Math.PI * radius; System.out.println("Area = " + area + “ square cm”); System.out.println("Circumference = " + circumference + “ cm”); }

17 Casting  A cast is an explicit conversion of a value from its current type to another type.  The syntax for a cast is: (type) expression  Two of the cases in which casting is required are: 1. To retain the fractional part in integer divisions: int sumOfGrades; int numberOfStudents; double average; //... average = (double) sumOfGrades / numberOfStudents; 2. When a type change will result in loss of precision int sum = 100; float temp = sum; //temp now holds float total = 100F; int temp = total; // ERROR int start = (int) total; int long float double loss of precision Note: The cast operator has higher priority than all arithmetic operators.