180 likes | 197 Views
Learn about variables, assignments, and constants in Java, including primitive data types and their storage, as well as the use of constants in programming.
E N D
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes
Variables and assignment • Variable • A name for a location in memory holding data value • Every variable has a type • It depends on the intended use • Example: • use the int type for a variable storing integer values • A variable declaration • => reserve portion of memory large enough to hold the value
data type variable name Variable declaration • A variable • must be declared before using it by specifying • The variable’s name • And the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration
Variables and assignment: general rules • A variable can be given an initial value • in the declaration • When a variable • is referenced in a program, its current value is used • You can change the value of an existing variable • Using the assignment operator (=) • lucky_number=13;(if the variable has been declared) • See PianoKeys.java int sum = 0; int base = 32, max = 149;
Assignment • An assignment statement • Changes the value of a variable • The assignment operator is the = sign • The expression on the right is evaluated • And the result is stored in the variable on the left • The value that was in total is overwritten • See Geometry.java total = 55;
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 • A constant • is an identifier that is similar to a variable • except that it holds the same value • During its entire existence • By giving the value a name => explain role in program • is named using uppercase letters • 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) • The compiler • Produces an error message • if you attempt to change the value of a constant • This prevents coding errors • Because the only valid place to change their value • is the initial assignment
Why to use constants? • Three reasons for using constants • Giving a constant a value a name helps explain its role • Give meaning to otherwise unclear values • For example, MAX_LOAD means more than the literal 250 • Compiler protects constant values • Avoid inadvertent errors by other programmers • Prevent changing a constant value throughout a program • They facilitate program maintenance • If a constant is used in multiple places • 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 • There are eight primitive data types in Java • Four of them represent integers • byte, short, int, long • Two of them represents floating point numbers • float, double • One of them represents characters • char • And one of them represent Boolean values • boolean
Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 JAVA Primitive data types • All the numeric types differ • By the amount of memory space used • To store a value of that type • Design programs so that space is not wasted
Integers and floating point types • By default • JAVA assumes all integer literals are of typeint • To define a literal of type long • L or l is appended to the end of the value • Example: long counted_Stars = 86827263927L; • JAVA assumes floating point literals are of type double • If we need to treat a floating point as a float • we append f or F to the end of the value • Example:float ratio = 0.2363F;
characters • A char variable stores a single character • Character literals are delimited • by single quotes • Example • Data type char represents a single character • Example:char topGrade = ‘A’; • Characters include • Uppercase, lowercase letters; punctuation ; etc.. 'a' 'X' '7' '$' ',' '\n'
Boolean type • Declaration • Example:boolean flag = true; • Boolean variables have only two valid values • true and • false • This type is used to represent situation with 2 states • Example: a light bulb being on (true) or off (false)
Integers and floating point types • By default • JAVA assumes all integer literals are of typeint • To define a literal of type long • L or l is appended to the end of the value • Example: long counted_Stars = 86827263927L; • JAVA assumes floating point literals are of type double • If we need to treat a floating point as a float • we append f or F to the end of the value • Example:float ratio = 0.2363F;
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes