30
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes

Variables & Datatypes

  • Upload
    isaiah

  • View
    61

  • Download
    0

Embed Size (px)

DESCRIPTION

Variables & Datatypes. Java (like virtually all programming languages) supports the concept of variables. You probably are familiar with the concept of variables from algebra, such as x = 3 - PowerPoint PPT Presentation

Citation preview

Page 1: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

1

Variables & Datatypes

Page 2: Variables &  Datatypes

Java (like virtually all programming languages) supports the concept of variables

You probably are familiar with the concept of variables from algebra, such as

x = 3Where the variable x in this case represents the integer 3 (initially), but can be reassigned to represent another value at any time.

SE Focus Dr. Mark L. Hornick

2

Page 3: Variables &  Datatypes

Brainstorm v = 3.14 w = 1/3 u = 1 c = sin(w) + cos(z) y2 = -1 z = 1 + y

SE Focus Dr. Mark L. Hornick

3

Suppose these are algebraic equalities.

What type of numbers do the variables represent?

Page 4: Variables &  Datatypes

Variables in Java are a similar concept, but different

If we want to use a variable x to represent an integer value of 3, we write

int x = 3;In Java, we have to declare the datatype (i.e. the kind of value) that the variable will represent.

In this case, “int” means that x can represent only integer values [of a specific range].

We say that x is the identifier (i.e. the name) of the variable

SE Focus Dr. Mark L. Hornick

4

Note the semicolon; This is required in Java!

Page 5: Variables &  Datatypes

In Java, we can arbitrarily make up [nearly] any name we like to use as a variable identifier, provided we follow a few rules:1. An identifier may consist of a sequence of one or more letters,

digits, underscores, and dollar signs ( e.g. my_1st_$ )

2. The first character in a Java identifier is generally a letter, rarely an underscore or dollar sign, and may not be a digit:

x_1 (ok) $y2 (ok, but unconventional) _z3 (ok, but unconventional) 1w (illegal)

3. Uppercase and lowercase letters are distinguished; the following are treated as 3 different identifiers:

myValue Myvalue MyValue

4. No spaces are allowed in an identifier.5. A Java Reserved Word may not be used as an identifier.

SE FocusDr. Mark L. Hornick

5

Page 6: Variables &  Datatypes

SE FocusDr. Mark L. Hornick

6

Java’s reserved words cannot be used as identifiersabstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

Page 7: Variables &  Datatypes

7

Java supports various “built-in” primitive datatypes

There are six numeric datatypes in Java:byteshortintlong floatdouble

byte, short, int, and long represent integer values

float and double represent real values

Page 8: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

8

Numeric values have a limited range of allowed values

Why do you think there are four differentdatatypes just to represent integer values?

Page 9: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

9

There are generally accepted rules for naming Java variables that will represent primitive datatypesUse a lowercase letter for the first letter of a

variable identifier, uppercase for subsequent words

Use nouns or noun phrases to represent things that have values

x areaCircle sumOfValues

Identifiers that begin with uppercase letters (or are all uppercase letters) areused for other Java elements we’ll see later on…

Good variable names convey a logical meaning asto the type of data the variables represent.

Page 10: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

10

Don’t use verbs, adverbs, adjectives or meaningless names for variable identifiers getIt Quickly happy FredFlintstone iDontLikeBeets

Names like these don’t convey any meaning to the reader of a program.

Page 11: Variables &  Datatypes

In Java, a variable can be also represent many non-primitive datatypes…

String s;

SE 1011Dr. Mark L. Hornick

11

This declares a variable s of datatype String. String is another predefined, non-primitive Java datatype.

What kind of values can be held by this type of variable???

Page 12: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

12

The String datatype is a built-in class (from the java.lang package) that represents a sequence of charactersString s1 = “a”;

String s2 =“SE-1011”;

String s3 =“123”;

String s4 =“This is a 6 word string.”;

String s5 = “”;There is no fundamental restriction on the

maximum length of a Java String

How does this differ from a int that represents 123?

This represents an empty String

String sequences are surrounded by double-quotes

Page 13: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

13

The char primitive datatype represents only a single character

char c1 =‘1’;char c2 =‘a’;char c3 =‘?’;char c4 =‘B’;char c5 =‘%’;

How does this differ from a int that represents 1?

Characters are surrounded by single-quotes

Page 14: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

14

char c1 = ‘\t’; // tabchar c2 =‘\n’; // newlinechar c3 =‘\r’; // carriage returnchar c4 =‘\”’; // double-quotechar c5 =‘\’’; // single quotechar c5 =‘\\’; // backslash

Use escape sequences to represent special characters

Escape sequences are two-characters consisting of the backslashfollowed by an escape code

Page 15: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

15

String hello = “Hello \n World”;System.out.println( hello );

Escape sequences can be used in Strings too

NOTE: PowerPoint uses distinct single- and double-quote characters that do not appear on your keyboard!

Page 16: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

16

The boolean datatype represents only two logical values: true and false

boolean isOff = true; boolean isCold = false;

The boolean datatype is named after George Boole, an English mathematician/logician who developed Boolean algebra

true and false are both Java reserved words

Page 17: Variables &  Datatypes

Repeat: You must declare a variable’s datatype in Java before you can use that variable

int x; String name; char c1; boolean isDone; float someValue; double areaCircle;

SE-1010Dr. Mark L. Hornick

17

x, name, c1, isDone, someValue, and areaCircle are all identifiers that represent only specific types of data.

So, you cannot assign a numeric value to name, nor can you assign a character string value to x.

Page 18: Variables &  Datatypes

Variables can be initialized at the same time as they are declared

int x = 0; String name = “Mark”; char c1 = ‘a’; boolean isDone= false; float someValue = 3.14F; double areaCircle = 2.6;

SE-1010Dr. Mark L. Hornick

18

The value of a variable can be reassigned at any time, even when it’s initialized.

Page 19: Variables &  Datatypes

A constant is a value that cannot be changed after initialization

final int ZERO = 0; final String YES = “Yeah”; final char QUESTION = ‘?’; final boolean OFF = false; final float E = 2.17F; final double PI= 3.1416;

SE-1010Dr. Mark L. Hornick

19

The value of a constant must be assigned at initialization, and cannot be reassigned afterwards.

Page 20: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

20

Arithmetic operators in Java areused on numeric datatypes (byte, short, int, long, float, and double)

Page 21: Variables &  Datatypes

Examples of arithmetic expressions and assignment using the binary arithmetic operators *, +, -, / and %

int x = 1;int y = x+1; x = x+3-y;int z = x*y*2;z = (x+y)*2+4; // note use of ()int w = 4/2+5-3;w = 5 % 2;

SE-1010Dr. Mark L. Hornick

21

Page 22: Variables &  Datatypes

Increment and Decrement arithmetic operators The ++ and -- unary operators are used to

increment or decrement the value of a variable by 1. x++; // increments x by 1 ++x; // ditto --x; // decrements x by 1 x--; // ditto

SE-1010Dr. Mark L. Hornick

22

Page 23: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

23

Arithmetic ExpressionsPrecedence rules for arithmetic operators and

parentheses

Page 24: Variables &  Datatypes

Integer Overflow The range of an int is -32768 to +32767

So what happens when you add 1 to an int holding a value of 32767??

SE-1010Dr. Mark L. Hornick

24

Page 25: Variables &  Datatypes

Integer Division Integers variables can only hold whole

values, so what happens if you do the following?int x = 2;int y = 3;int z = x/y; // what is the result?

SE-1010Dr. Mark L. Hornick

25

Page 26: Variables &  Datatypes

Variable values can be assigned (and re-assigned) as the result of various operations

int radius = 3;

float areaCircle = radius * 3.14159;radius = 4;areaCircle = radius * 3.14159;

SE-1010Dr. Mark L. Hornick

26

Declaration and assignment of radius

Declaration and assignment of areaCircle

Reassignment of previously declared radius

Reassignment of previously-declared areaCircle

Page 27: Variables &  Datatypes

Q: Would it make sense to declare areaCircle to be an int?

int radius = 3;

int areaCircle = radius * 3.14159;radius = 4;areaCircle = radius * 3.14159;

SE-1010Dr. Mark L. Hornick

27

Declaration and assignment of radius

Declaration and assignment of areaCircle

Reassignment of previously declared radius

Reassignment of previously-declared areaCircle

Page 28: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

28

Details: Primitive variables vs. non-primitive (object) variables

The only difference between a variable for a primitive and a variable for objects is the contents in the memory locations. For primitives, a variable contains the numerical value itself.For objects (like Strings), a variable contains an address

where the object is stored.

10

str1 <Memory address of String object>

xStringobject

Page 29: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

29

Can we use arithmetic operators (like + or -) on object variables?

String s1 = “se”;String s2 = “1011”;String output = s1 + s2;// does this work??

Page 30: Variables &  Datatypes

SE-1010Dr. Mark L. Hornick

30

The only operator defined by Java for a String variable is “+”The “+” operator, when used to “add” Strings,

actually causes the Strings to be concatenated

String s1 = “se”;String s2 = “1010”;String output = s1 + s2;// output is “se1010”

The only operator defined by Java for an object variable is “+”, and it only works on String objects