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.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
©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.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Using Objects. Overview In this presentation we will discuss: –Classes and objects –Methods for objects –Printing results.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
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.
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.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Primitive Data Types. Identifiers What word does it sound like?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
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.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
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.
Chapter 2 Basic Computation
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Variables and Primative Types
Chapter 2 Basic Computation
Building Java Programs Chapter 2
Using Objects 21-Nov-18.
Chapter 2 Edited by JJ Shepherd
Introduction to Java, and DrJava part 1
Building Java Programs
Building Java Programs Chapter 2
Introduction to Java, and DrJava
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Building Java Programs Chapter 2
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Building Java Programs
Presentation transcript:

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 Operations on primitives The assignment statement How to print results

3 Primitives Primitives are the “basic” data values There are eight types of primitives: boolean -- used for true and false values char -- used for single characters (letters, etc.) byte, short, int, long -- four different kinds of integer (whole number) values float, double -- two different kinds of decimal numbers (numbers with a decimal point)

4 int The most important integer type is int An int is a “whole” number (no decimal point) Numbers occupy memory in the computer Larger numeric types require more memory byte : 1 byte short : 2 bytes int : 4 bytes long : 8 bytes An int can be between about two billion (two thousand million) and negative two billion If you just write a number, such as 25, Java assumes it is an int Hence it is easier to work with int values than with the other integer types ( byte, short, and long ) Use int in preference to other integer types

5 byte and short A byte can be between -128 and 127 A short can be to Why these numbers? These are “round numbers” in binary; for example, is binary for is binary for The first bit is the sign bit: a 1 means it’s a negative number Use byte or short only when You know the numbers are all small There are millions of numbers to remember Extra syntax is needed (will be discussed later)

6 long long integers are for when two billion isn’t large enough for your needs A long can be as long as about 19 digits A long occupies twice as much space as an int Arithmetic on long values is slower Use long only when you need really big numbers Extra syntax is needed (will be discussed later) Even larger numbers are available in Java-- but they are objects, not primitives

7 double A double represents a “real” number Also sometimes called “floating point” These are numbers with a decimal point A double has about 15 digits of accuracy If you just write a real number, such as 1.37, Java assumes it is a double Hence it is easier to work with double values than with float values Use double in preference to float

8 float float is the other kind of “real,” or “floating point” number float has about 8 digits of accuracy Arithmetic with float is not faster Use float only to save space when there are millions of numbers involved Extra syntax is needed (will be discussed later)

9 An aside: approximations Integers are precise, but real numbers are always approximate (inaccurate) Computers always use the binary system internally Many numbers that can be expressed precisely in decimal cannot be represented precisely in binary For example, the numbers 1.1, 1.2, 1.3, and 1.4 can only be approximated in binary Two numbers that look the same may actually be subtly different Never test floating point numbers for equality! Only test for larger or smaller, or for “not larger” or “not smaller” This is not a Java rule—it’s a programming rule

10 Giving names to numbers Sometimes you know what a number is You have 10 fingers There are 24 hours in a day π is Numbers written like this are called literals You can use literals any place in Java that you can use a number Sometimes you need to use names instead: classSize, myBankBalance, myAge, speedometerReading Names like this are called variables The value of a variable may change Sometimes names are simply more convenient, for example, Math.PI instead of

11 Variables Before you use a variable, you must declare it (tell Java what type it is: int, double, char,...) There are two reasons for this: Different types require different amounts of space So Java can prevent you from doing something meaningless (adding 5 to someone’s name, or multiplying two dates together) Before you use a variable, you must also define it (tell Java what value it has) It makes no sense to print out your bankBalance, or to add to your bankBalance, if you don’t have a meaningful, well-defined initial value for bankBalance to start with You might assign an initial value to your variable, or compute a value, or read a value in; but you have to get one somehow

12 Declaring variables You declare variables like this: int classSize; double myBankBalance; When you declare a variable to be a primitive type, Java automatically finds space for it The amount of space Java needs to find depends on the type of the variable Think of a variable as a specially shaped “box,” designed to hold a value of a particular type An int variable is four bytes long and there’s a special place for the sign bit A float variable is also four bytes long, but the bits are used differently--some are used to tell where the decimal point goes

13 Giving values to variables A variable is just a name for some value You have to supply the actual value somehow Java tries to prevent you from using a variable that you haven’t given a value You can assign values like this: classSize = 57; myBankBalance = ; // no "$"!

14 Initializing variables You can give a variable an initial value when you declare it: int classSize = 30; double myBankBalance = 0.0; You can change the value of a variable many times: classSize = 57; myBankBalance = myBankBalance ;

15 Arithmetic Primitives have operations defined for them int and double have many defined operations, including + for addition - for subtraction * for multiplication (Old computers did not have the    character) / for division

16 Order of precedence Operations with higher precedence are done before operations with lower precedence Multiplication and division have higher precedence than addition and subtraction: * 4 is 14, not 20 Operations of equal precedence are done left to right: is 4, not 6

17 Parentheses Operations inside parentheses are done first (2 + 3) * 4 is 20 Parentheses are done from the inside out 24 / (3 * (10 - 6)) is 24 / (3 * 4) is 24 / 12 is 2 Parentheses can be used where not needed 2 + (3 * 4) is the same as * 4 [ ] and { } cannot be used as parentheses!

18 Assignment statements An assignment statement has the form: variable = expression ; Examples: price = 0.69;  (The expression can be as simple as a single literal or variable) area = pi * radius * radius; classSize = classSize + 1; This means “add one to the value in classSize ”

19 Printing out results, part 1 In Java, “print” really means “display in a window on the screen” Printing on actual paper is much harder! There are two commands for printing: System.out.print( x ); Displays x System.out.println( x ); (Pronounced “printline”) Displays x, then goes to the next line

20 Printing out results, part 2 Examples: System.out.print("The sum of x and y is "); System.out.println(x + y); If x and y are both 5, the result will be The sum of x and y is 10 If you print from an application, an output window opens automatically If you print from a browser applet, you have to open the “Java Console” window to see your output

21 A BASIC program Here is a program, written in the BASIC language, to add two numbers and print out the result: PRINT 2+2

22 A Java program Here is the same program, written in Java: public class TwoPlusTwo { public static void main(String args[]) { System.out.println(2 + 2); } }

23 Why is Java so hard? BASIC is a beginner’s language, designed for small programs Java is a professional’s language, designed for large programs Just as a skyscraper needs a better foundation than a doghouse, Java programs need more structural support Java isn’t the best language for everything

24 New vocabulary primitive: one of the 8 basic kinds of values literal: an actual specified value, such as 42 variable: the name of a “box” that can hold a value type: a kind of value that a literal has or that a variable can hold declare: to specify the type of a variable

25 More new vocabulary operation: a way of computing a new value from other values precedence: which operations to perform first (which operations precede which other operations) assignment statement: a statement that associates a value with a name initialize: to assign a “starting” value

26 The End