1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
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.
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.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©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 Computers and Programming Lecture 4: Mathematical Operators New York University.
1 More on Assignment and Console Input Overview l Increment & Decrement Operators l Short-cut Operators l Casting l Math class l Console Input (TextIO.
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”
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
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.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
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.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
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,
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
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.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CSC Programming I Lecture 5 August 30, 2002.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Primitive Variables.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
Variables, Operators, and Expressions
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Fundamental of Java Programming Basics of Java Programming
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Expressions and Assignment
elementary programming
Data Types and Expressions
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment and Decrement operators l Short hand operators l The Math Class l Math Functions Example l Casting

2 Primitive Data Types Java has eight primitive data types as described below. Other information is represented in Java as an Object. TypeSize/FormatRange byte 8-bit two's complement-128 to 127 short 16-bit two's complement-32,768 to 32,767 int 32-bit two's complementabout –2 billion to 2billion long 64-bit two's complementabout –10E18 to +10E18 float 32-bit IEEE E38 to +3.4E38 double 64-bit IEEE E308 to 1.7E308 char 16-bit Unicode characterA single character boolean true or false

3 Variable Declaration You can declare a variable to hold a data value of any of the primitive types. int counter; int numStudents = 583; long longValue; long numberOfAtoms = L; float gpa; float batchAverage = 0.406F; double e; doublepi = 0.314; char gender; char grade = ‘B’; boolean safe; boolean isEmpty = true;

4 Variable Declaration (cont.) public class Example1 { public static void main ( String[] args ) { int payAmount = 123; System.out.println("The variable contains: " + payAmount ); }

5 Arithmetic Operators OperatorUseDescription +op1 + op2 Adds op1 and op2 -op1 - op2 Subtracts op2 from op1 *op1 * op2 Multiplies op1 by op2 /op1 / op2 Divides op1 by op2 % op1 % op2 Computes the remainder of dividing op1 by op2 public class Example2 { public static void main ( String[] args ) { int hoursWorked = 40; double payRate = 10.0; System.out.println("Hours Worked: " + hoursWorked ); System.out.println("pay Amount : "+ (hoursWorked*payRate)); }

6 Arithmetic Operation Modes of operation : If operand1 and operand2 are of same data type, then the resultant value of the operation will be of that same data type. If operand1 and operand2 are of different data type like real and integer, then the resultant value of the operation will be of real data type. e.g. 1/2 gives 0 1.0/2 gives /2.0 gives 0.5 Operand1Operand2Result Real Whole Real WholeReal

7 Arithmetic Expressions l Definition: An expression is a sequence of variables, constants, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value. l In Java, Arithmetic expressions are evaluated very similar to the way they are evaluated in algebra. Operator Meaning Precedence - unary minus highest + unary plus highest * multiplication middle / division middle % remainder middle + addition low - subtraction low e.g / 4  / 2 – 9  -4

8 Associativity of Operators l In Java, all binary operators except for the assignment operators are evaluated in left to right order. 2 * 7 * *

9 Evaluating Expressions l The following example computes the roots of a quadratic equation, assuming that the equation has real roots. l It uses the formula: 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); }

10 Assignment Statement variable = expression; l The value of constants / variables / expression in the right side of the = operator, is assigned to the variable in the left side of the = operator. l Examples: a = 5; b = a; c = a + b; l The left hand side of the = operator must be a variable. l It cannot be a constant or an expression. l Example of an invalid assignment statement : a + b = c ;

11 Assignment Statement (cont.) l Java allows multiple assignment. int width = 100, height = 45; l By default, Java takes whole numbers to be of type int and real numbers to be of type double. However, we can append a letter at the end of a number to indicate its type. l Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but we should prefer L): float maxGrade = 100f ; // now holds ‘100.0’ double temp = 583d ; // holds double precision float temp = 5.5 ; // ERROR! // Java treats 5.5 as a double long x = 583l ; // holds 583, but looks like 5,381 long y = 583L ; // This looks much better!

12 Increment or Decrement Operators Increment/decrement operations ( count = count +1 ) are very common in programming. Java provides operators that make these operations shorter. OperatorUse Description ++ op++ Increments op by 1; ++ ++op Increments op by 1; -- op-- Decrements op by 1; -- --op Decrements op by 1 ; Examples : 1. What is the value of j and i after executing the following code? i = 1; j = 5; j = ++i;

13 Increment or Decrement Operators (cont.) 2. What is the value of j and i after executing the following code? i = 10; j = 50; j = i--; 3. What is the value of j and i after executing the following code? i = 5; j = 10; i++; ++j;

14 Short Hand Operators Java also provides a number of operators that can be used as a short-cut for performing arithmetic operations on a variable and assigning the result to the same variable. Operator 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 : Instead of writing a = a + 5; We can write a += 5;

15 The Math class PIThe best approximate value of PI abs (a) Returns the absolute value of a, a 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 (a) Returns the exponential number e raised to the power of a log (a) Returns the natural logarithm (base e) of a max (a, b) min (a, b) Returns the greater/smaller of two values, a and b can double, float, int or long pow (a, b) Returns the first argument raised to the power of the second random () Returns a random value in the range 0.0 and 1.0 round (a) Returns the closest long to the argument (double or float) toDegrees(a) toRadians(a) Converts an angle in radians to the degrees Converts an angle in degrees to the radians

16 Math Functions Example Public class Expressions { 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); System.out.println("Circum. =" + circumference); }

17 Casting We learnt earlier that the following division 5 / 2 results in 2 Because the / operator is operating between 2 integer type constants, the result will be an integer. To get 2.5, we need to convert either 1 or both the operands to double. Then the division will look like 5.0 / 2.0 But what if we have integer variables to divide each other, like a / b ? For this, cast operator is used. (double) a / (double) b

18 Casting (cont.) Conversion of primitives is accomplished by (1) assignment and/or (2) explicit casting: int total = 100; float temp = total; //temp now holds When changing type that will result in a loss of precision, an explicit ‘cast’ is needed. This is done by placing the new type in parenthesis: float total = 100F; int temp = total; // ERROR! int start = (int) total;