Outline Character Strings Variables and Assignment Primitive Data

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Variables and assignment l Variable l A name for a location in memory holding data value l Every variable has a type l l It depends on the intended use l Example: § use the int type for a variable storing integer values A variable declaration l => reserve portion of memory large enough to hold the value

Variable declaration l A variable l must be declared before using it by specifying l The variable’s name l And the type of information that it will hold data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration

Variables and assignment: general rules l A variable can be given an initial value l in the declaration int sum = 0; int base = 32, max = 149; l When a variable l l is referenced in a program, its current value is used You can change the value of an existing variable l Using the assignment operator (=) l l lucky_number=13; (if the variable has been declared) See Piano. Keys. java

Assignment l An assignment statement l Changes the value of a variable l The assignment operator is the = sign total = 55; l l The expression on the right is evaluated l And the result is stored in the variable on the left l The value that was in total is overwritten See Geometry. java

Sample programs /* Input: Geometry. JAVA */ public class Geometry { public static void main (String[ ] args) { int sides = 5; // declaration with intialization System. out. println(“ A pentagon has ”+ sides + “sides. ”); sides = 10; // assignment statement System. out. println(“A decagon has ” + sides + “sides. ”); } } // Output: A pentagon has 5 sides A decagon has 10 sides

Constants l A constant l is an identifier that is similar to a variable l except that it holds the same value l During its entire existence l By giving the value a name => explain role in program l is named using uppercase letters l l To distinguish them from regular variables In Java, we use the final modifier to declare a variable final int MIN_HEIGHT = 69;

Constants (cont’d) l The compiler l Produces an error message l if you attempt to change the value of a constant l This prevents coding errors l Because the only valid place to change their value l is the initial assignment

Why to use constants? l Three reasons for using constants l Giving a constant a value a name helps explain its role l l l Give meaning to otherwise unclear values § For example, MAX_LOAD means more than the literal 250 Compiler protects constant values l Avoid inadvertent errors by other programmers l Prevent changing a constant value throughout a program They facilitate program maintenance l If a constant is used in multiple places l Its value need only be updated in one place

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Primitive data types l There are eight primitive data types in Java l Four of them represent integers l l byte, short, int, long Two of them represents floating point numbers l float, double l One of them represents characters l char l And one of them represent Boolean values l boolean

JAVA Primitive data types l All the numeric types differ l By the amount of memory space used l l To store a value of that type Design programs so that space is not wasted Type Storage Min Value Max Value byte short int long 8 bits 16 bits 32 bits 64 bits -128 -32, 768 -2, 147, 483, 648 < -9 x 1018 127 32, 767 2, 147, 483, 647 > 9 x 1018 float double 32 bits 64 bits +/- 3. 4 x 1038 with 7 significant digits +/- 1. 7 x 10308 with 15 significant digits

Integers and floating point types l By default l JAVA assumes all integer literals are of type int l To define a literal of type long l l L or l is appended to the end of the value Example: long counted_Stars = 86827263927 L; l JAVA assumes floating point literals are of type double l If we need to treat a floating point as a float l l we append f or F to the end of the value Example: float ratio = 0. 2363 F;

characters l A char variable stores a single character l Character literals are delimited l by single quotes l Example 'a' l '7' '$' ', ' 'n' Data type char represents a single character l l 'X' Example: char top. Grade = ‘A’; Characters include l Uppercase, lowercase letters; punctuation ; etc. .

Boolean type l Declaration l l Boolean variables have only two valid values l l l Example: boolean flag = true; true and false This type is used to represent situation with 2 states l Example: a light bulb being on (true) or off (false)

Integers and floating point types l By default l JAVA assumes all integer literals are of type int l To define a literal of type long l l L or l is appended to the end of the value Example: long counted_Stars = 86827263927 L; l JAVA assumes floating point literals are of type double l If we need to treat a floating point as a float l l we append f or F to the end of the value Example: float ratio = 0. 2363 F;

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes
- Slides: 18