0% found this document useful (0 votes)
27 views

Ch. 2 - Lecture4 - Variables, Assignements & Primitive Types

This document outlines the basics of variables and data types in Java. It discusses variable declaration and assignment, as well as the eight primitive data types: byte, short, int, long, float, double, char, and boolean. Each data type stores values within a specified range of memory sizes. Constants are also covered, which are similar to variables but whose values cannot change once declared. Proper use of variables and data types is important for program design and avoiding errors.

Uploaded by

elio saliba
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Ch. 2 - Lecture4 - Variables, Assignements & Primitive Types

This document outlines the basics of variables and data types in Java. It discusses variable declaration and assignment, as well as the eight primitive data types: byte, short, int, long, float, double, char, and boolean. Each data type stores values within a specified range of memory sizes. Constants are also covered, which are similar to variables but whose values cannot change once declared. Proper use of variables and data types is important for program design and avoiding errors.

Uploaded by

elio saliba
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

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

data type variable name

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
int sum = 0;
int base = 32, max = 149;
 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
Assignment
 An assignment statement
 Changes the value of a variable

 The assignment operator is the = sign


total = 55;

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


Type Storage Min Value Max Value

byte 8 bits -128 127


short 16 bits -32,768 32,767
int 32 bits -2,147,483,648 2,147,483,647
long 64 bits < -9 x 1018 > 9 x 1018

float 32 bits +/- 3.4 x 1038 with 7 significant digits


double 64 bits +/- 1.7 x 10308 with 15 significant digits
Integers and floating point
types
 By default
 JAVA assumes all integer literals are of type int

 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
'a' 'X' '7' '$' ',' '\n'
 Data type char represents a single character
 Example: char topGrade = ‘A’;

 Characters include
 Uppercase, lowercase letters; punctuation ; etc..
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)
Outline Character Strings
Variables and Assignment
Primitive Data Types
Expressions
Data Conversion
Interactive Programs
Graphics
Applets
Drawing Shapes

You might also like