1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
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.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Java-02 Basic Concepts Review concepts and examine how java handles them.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Types Four integer types: Two floating-point types:
Chapter 2 Variables.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
IDENTIFIERS CSC 111.
Conversions of the type of the value of an expression
Examples of Primitive Values
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Expressions and Assignment
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Primitive Types and Expressions
Chapter 2 Variables.
Presentation transcript:

1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character type:  char n One boolean type:  boolean

2 Primitive Types, cont.

3 Examples of Primitive Values n Integer values: n Floating-point values: n Character values: `a` `A` `#` ` ` n Boolean values: true false

4 Motivation for Primitive Types n Why are there several different integer types?  storage space  operator efficiency n More generally, why are there different types at all?  reflects how people understand different kinds of data, e.g., letter vs. numeric grades  makes code more readable (which is a big deal)  helps prevent programmer errors

5 Literals Values such as 2, 3.7, or ’ y ’ are called constants or literals. Integer literals can be preceded by a + or - sign, but cannot contain commas. Every integer literal is either of type int or type long. The type of an integer literal can be determined by…looking at it!

6 Integer Literal Type An integer literal is of type long if it is suffixed with an letter L or l; otherwise it is of type int.  note that capital L is preferred Integer literals of type long :  2L777L  -372L1996L  l0l Integer literals of type int :  2777  

7 Floating Point Literals n Floating point literals:  Can be preceded by a + or - sign, but cannot contain commas  Can be specified in (a type of) scientific notation n Examples:  can also be written as 8.65e8  can also be written as 4.83e-4 The number in front of the “e” does not need to contain a decimal point, e.g. 4e-4

8 Floating Point Literal Type Every floating point literal is either of type float or type double. The type of a floating point literal can be determined by…looking at it! An floating point literal is of type float if it is suffixed with an letter F or f; otherwise it is of type double. n Floating point literals of type float:  2.5F0.0f  8.65e8f4e-4 F  3f+35.4f  -16F-16.0F

9 Assignment Compatibilities n Java is said to be strongly typed, which means that there are limitations on mixing variables and values in expressions and assignments. n What is the type of the LHS and RHS for each statement? int x = 0; long y = 0; float z = 0.0f; int w; w = x;// legal; what does it do? x = y;// illegal x = z;// illegal y = z;// illegal z = 3.6;// illegal (3.6 is of type double) y = 25;// legal, but…why?

10 Assignment Compatibilities n Sometimes automatic conversions between types do take place: short s; int x; s = 83; x = s; double doubleVariable; int intVariable; intVariable = 7; doubleVariable = intVariable;

11 Assignment Compatibilities, cont. n In general, a value (or expression) of one numeric type can be assigned to a variable of any type further to the right, as follows: byte --> short --> int --> long --> float --> double but not to a variable of any type further to the left. Makes sense intuitively because, for example, any legal byte value is a legal short value. On the other hand, many legal short values are not legal byte values.

12 Assignment Compatibilities, cont. Example – all of the following are legal, and will compile: byte b = 0; short s; int i; long l; float f; double d; s = b; i = b; l = i; f = l;// This one is interesting, why? d = f; b = 10;

13 Assignment Compatibilities, cont. Example – NONE (except the first) of the following will compile: byte b; short s; int i; long l; float f; double d; d = 1.0;// This one compiles f = d; l = f; i = l; s = i; b = s;

14 Type Casting n A type cast creates a value in a new type from an original type. n A type cast can be used to force an assignment when otherwise it would be illegal (thereby over-riding the compiler, in a sense). n Example: double distance; distance = 9.0; int points; points = distance;// illegal points = (int)distance;// legal

15 Type Casting, cont. The value of (int)distance is 9, but the value of distance, both before and after the cast, is 9.0. The type of distance does NOT change and remains double. What happens if distance contains 9.7?  Any value right of the decimal point is truncated (as oppossed to rounded).

16 Type Casting, cont. n A cast can be performed from any primitive type to any other primitive type, however… n Remember to “cast with care,” because the results can be unpredictable. int x; long z = ?;// ? Could be a computation or input x = (int)z;

17 Arithmetic Operations n Arithmetic expressions:  Formed using the +, -, *, / and % operators  Operators have operands, which are literals, variables or sub-expressions. n Expressions with two or more operators can be viewed as a series of steps, each involving only two operands.  The result of one step produces an operand which is used in the next step.  Java is left-associative.  Most of the basic rules of precedence apply. n Example: int x = 0, y = 50, z = 20; double balance = 50.25, rate = 0.05; x = x + y + z; balance = balance + balance * rate; balance = (balance + balance) * rate;

18 Expression Type n An arithmetic expression can have operands of different numeric types.  x + (y * z) / w  Note that this does not contradict our rules for assignment. n Every arithmetic expression has a (resulting) type.  k = x + (y * z) / w;// Does this compile? n Given an arithmetic expression:  If any operand in the expression is of type double, then the expression has type double.  Otherwise, if any operand in the expression is of type float, then the expression has type float.  Otherwise, if any operand in the expression is of type long, then the expression has type long.  Otherwise the expression has type int.

19 Expression Type, cont. n Example: int hoursWorked = 40; double payRate = 8.25; double totalPay; Then the expression in the assignment: totalPay = hoursWorked * payRate is a double with a value of

20 Operators with integer and floating point numbers n See the program:  