Top Banner
Introduction to Primitives
26

Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

Introduction to Primitives

Page 2: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

Page 3: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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)

Page 4: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

int• The most important integer type is int

– An int is a "whole" number (no decimal point)

• Numbers occupy memory in the computer– Larger numbers require more memory– 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

• Use int in preference to other integer types

Page 5: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

byte and short

• A byte can be between -128 and 127• A short can be -32768 to 32767• Why these numbers?

– I’ll answer that later in the course

• 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)

Page 6: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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 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

Page 7: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

• Use double in preference to float

Page 8: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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)

Page 9: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

An aside: approximations

• Integers are precise, but real numbers are always approximate (inaccurate)

• 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”

Page 10: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

Giving names to numbers

• Sometimes you know what a number is– You have 10 fingers– π is 3.1415926536– Numbers written like this are called literals– You can use literals anyplace in Java

• Sometimes you need to use names instead:– classSize, myBankBalance, price– Names like this are called variables– The value of a variable may change

Page 11: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

Variables

• Before you use a variable, you must declare it (tell Java what type it is)

• 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 a frog)

• You must also tell Java what its value is– You might compute the value, or read it in

Page 12: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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”

Page 13: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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 = 123.01; // no "$"!

Page 14: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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 + 50.00;

Page 15: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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 thecharacter)

/ for division

Page 16: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

Order of precedence

• Operations with higher precedence are done before operations with lower precedence

• Multiplication and division are done before addition and subtraction 2 + 3 * 4 is 14, not 20

• Operations of equal precedence are done left to right 10 - 5 - 1 is 4, not 6

Page 17: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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 2 + 3 * 4

• [ ] and { } cannot be used as parentheses!

Page 18: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

Assignment statements

• An assignment statement has the form: variable = expression ;

• Examples: price = 0.69; area = pi * radius * radius; classSize = classSize + 1;

• this means “add one to the value in classSize”

Page 19: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

Page 20: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

Page 21: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

A BASIC program

• Here is a program, written in the BASIC language, to add two numbers and print out the result:

PRINT 2+2

Page 22: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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); }}

Page 23: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

Page 24: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

Page 25: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

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

Page 26: Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.

The End