Chapter 4 – Fundamental Data Types

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

 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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Data types and variables
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
String Escape Sequences
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
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.
Computer Science 111 Fundamentals of Programming I Number Systems.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
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.
FUNADAMENTAL DATA TYPES LEARNING OBJECTIVES: TO UNDERSTAND INTEGEr AND FLOATING POINT NUMBERS TO RECOGNIZE THE LIMITATIONS OF THE NUMERIC TYPES TO UNDERSTAND.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
© 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:
Topic 2 Elementary Programming
Chapter 2 Variables.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Multiple variables can be created in one declaration
Variables and Primative Types
Type Conversion, Constants, and the String Object
Data Structures Mohammed Thajeel To the second year students
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
IDENTIFIERS CSC 111.
Fundamentals of Programming I Number Systems
Chapter 2: Basic Elements of Java
Approximations and Round-Off Errors Chapter 3
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
4.3 Arithmetic Operators and Mathematical Functions
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
Presentation transcript:

Chapter 4 – Fundamental Data Types

To understand integer and floating-point numbers Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware of causes for overflow and roundoff errors To understand the proper use of constants To write arithmetic expressions in Java To use the String type to define and manipulate character strings To learn how to read program input and produce formatted output Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

int: integers, no fractional part: 4.1 Number Types int: integers, no fractional part: 1, -4, 0 double: floating-point numbers (double precision): 0.5, -3.11111, 4.3E24, 1E-14 A numeric computation overflows if the result falls outside the range for the number type: int n = 1000000; System.out.println(n * n); // prints -727379968 Java: 8 primitive types, including four integer types and two floating point types Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Primitive Types Type Description Size int The integer type, with range -2,147,483,648 . . . 2,147,483,647 4 bytes byte The type describing a single byte, with range -128 . . . 127 1 byte short The short integer type, with range -32768 . . . 32767 2 bytes long The long integer type, with range -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 8 bytes double The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits float The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits char The character type, representing code units in the Unicode encoding scheme boolean The type with the two truth values false and true 1 bit Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Number Types: Floating-point Types Rounding errors occur when an exact conversion between numbers is not possible: double f = 4.35; System.out.println(100 * f); // prints 434.99999999999994 Java: Illegal to assign a floating-point expression to an integer variable: double balance = 13.75; int dollars = balance; // Error Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Self Check 4.1 Which are the most commonly used number types in Java? Answer: int and double

Self Check 4.2 Suppose you want to write a program that works with population data from various countries. Which Java data type should you use? Answer: The world’s most populous country, China, has about 1.2 x 109 inhabitants. Therefore, individual population counts could be held in an int. However, the world population is over 6 × 109. If you compute totals or averages of multiple countries, you can exceed the largest int value. Therefore, double is a better choice. You could also use long, but there is no benefit because the exact population of a country is not known at any point in time.

Self Check 4.3 Which of the following initializations are incorrect, and why? int dollars = 100.0; double balance = 100; Answer: The first initialization is incorrect. The right hand side is a value of type double, and it is not legal to initialize an int variable with a double value. The second initialization is correct — an int value can always be converted to a double.

Big Numbers Use number objects BigInteger and BigDecimal in java.math package Essentially no limits on their size and precision Computations with big number objects are much slower than the number types Can’t use familiar arithmetic operators such as *, -, + and / Example: BigInteger n = new BigInteger(“1000000”); BigInteger r = n.multiple(n); System.out.println(r); // Prints 1000000000000 BigDecimal d = new BigDecimal(“4.35”); BigDecimal e = new BigDecimal(“100); BigDecimal f = d.multiple(e); System.out.println(f); // Prints 435.00

Binary Numbers Computers use binary numbers instead of decimal number Binary numbers are easier for computers to manipulate Rounding errors can occur when you convert between binary and decimal numbers. For example, there is no exact representation of the fraction 1/10 in binary. Just like there is no exact representation of the fraction 1/3 in decimal. 4.35 is 100.010110 0110 0110 … in binary