CompSci 6 5.1 Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
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.
©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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
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
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
LESSON 6 – Arithmetic Operators
CSC Programming I Lecture 5 August 30, 2002.
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
1 Operations Making Things Happen (Chap. 3) Expressions.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
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,
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter 2 Variables.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Identifiers - symbolic names
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Expressions and Assignment
Chapter 2: Java Fundamentals
Data Types and Expressions
Primitive Types and Expressions
Chapter 2 Variables.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Chapter 2 Primitive Data Types and Operations
Problem 1 Given n, calculate 2n
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first four) boolean (1bit) int (4 bytes) double (8 bytes) char (2 bytes)  Constants/Literals (by example): boolean f = false; int i = 32769; double d = ; char c = ’x’; byte (1 byte = 8 bits) short (2 bytes) long (8 bytes) float (4 bytes) byte b = 33; short s = 21; long l = 289L; float = F;

CompSci Named Constants  Probably should name most constants  Should have no “Magic Numbers” in program  By convention, use all caps in the identifier  Use the final keyword  Keeps you from accidentally changing value; final boolean CORRECT = true; final int SQUAD_SIZE = 12; final double MIN_GPA = 2.5; final char FAIL = ’F’; final String AUTHOR = ”Dietolf Ramm”;  Note Constants provided by Java: (from API) Math.PI, Math.E, Integer.MAX_VALUE

CompSci Arithmetic Operators  Arithmetic o +, -, *, /, % ( remainder or mod )  Increment/Decrement o e.g., k++, k--, ++k, --k  Logical (results in boolean value) o =, > o Used only for numbers except == and != o For boolean only: !, &&, ||  String Concatenation o “I’m “ “ years old and live in “ + city  Assignment o variable = expression o variable op= expression o ( shorthand for: variable = variable op expression )

CompSci Operators  Arithmetic  +, -, *, /, % ( remainder or mod )  Work for both integers and reals  Except watch / and % for integers o What is 13/5 ? 13%5 ? 3/5 ? 3%5 ?  Increment/Decrement  e.g., k++, k–- o Written as k++; not k = k++; !!  Can write code like k = 3 * p++ - m / 5;  Usually not a good idea : confuses. Use a separate line for increment of p.

CompSci Operator Precedence  Determines order of operation  For arithmetic, matches grammar school learning o multiplication and division before addition and subtraction o what is the value of / 9.0 * 27.0 ? o (what is the value for the integer version?)  Parentheses override precedence rules (and don’t do harm when not needed)  For equal precedence (e.g., * and / ) work strictly left to right except for assignment and prefix operations which work right to left  Some of the operators, grouped from high to low : ++ --, * / %, + -, >=, == !=, &&, ||, = op=

CompSci Operators  Combining Assignment and Arithmetic  variable op= expression  ( shorthand for: variable = variable op expression )  Thus the following lines contain equivalent statements k = k – 1; k -= 1; k--; q = q * r; q *= r; s = s / 2; s /= 2;

CompSci Assignment  (Familiar by now) x = 13.3; k = k + 7; area = 2.0 * Math.PI * radius * radius;  Vocalize as “gets” or “becomes”  Don’t use “equals”: Not Equality  Casting: (type) int k = 3; double t; t = k; // OK, No information lost int m; double s = 3.5; m = s; // ILLEGAL, information lost (accidentally?) m = (int) s; // OK, information lost – intent shown

CompSci Casting  Implicit Casting  Types on two sides of operator differ ( int and double ) or ( double and int )  Promotes calculation double  Can cascade down the expression  Compare the following two lines; tempC = 5 / 9 * (tempF + 40) – 40; tempF = 9.0 / 5 * (tempC + 40) – 40;  Try for 212 O F or 100 O C

CompSci The Math class  Contains useful static methods  static means the method does not operate on an object  Use class name ( Math ) rather than object name when using the dot operator.  Contains common math functions  Look up Math class in Java API  Use of some common methods double x = Math.sqrt(y) * Math.cos(theta); long j = Math.round(3.8 * Math.pow(h, n));