a variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102;...
TRANSCRIPT
A variable is a storage locationfor some type of value.
days
102
taxrate
7.75
int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done = true;
What is a data type?
A data type describes what type of data the variable will hold.
There are 8 basic primitive data types
What is a data type?
byte short intlong float doublechar boolean
int num = 9;double total = 3.4;
TYPE Holds RANGE
int integers (32 bit)
-2 billion to 2 billion
int num = 3;
double decimals/floating number(64 bits)
-1.7E+308 to 1.7E+308
double num1 = 7.4;
boolean {true, false} Holds two values: {true, false}
boolean done = false;
char One character
‘A’
Declarations
A declaration is the introduction of a new name in a program.
All variables must declared in advance
General forms to declare variable. Data type and variable name
int num; double digit; boolean done;
How do you name variables when defining them?
When naming a variable follow these rules and conventions:
1. The variable name must contain NO spaces.
2. The variable name must begin with a lower case letter.
3. If you run two words together, then the first letter of the second word should begin with a capital letter.
4. The remainder of the variable name contains only letters and digits.
Can use _ $ as symbols and can start with _ but not recommended.
IDENTIFIER variables are identifers
Below are some examples of valid variable names:
number sum32 testAverage areaOfTrapezoid
and below are some examples of invalid variable names:
2ndValue test Average question#2
Java is case sensitive.
Brandon does not equal brandon.
Brandon != brandon
Java is case sensitive.(Yes, it’s so important that I said it twice!)
Keywords are reserved words that the language uses for a specific purpose. You cannot use keywordsas identifier names.
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
assignment operator (=). Called initializing the variable.
int num = 1000; // This line declares num as an int variable which holds value "1000".boolean bol = true; // This line declares bol as boolean variable which is set to the value "true". boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = 100000;
Statements: Statements in Java end with a semicolon ;
OOP: Basic Parts of Java Primitive Data Types, Example
// create some integersint x, int y; // declared but not initializedx = 1234; y = 3; //initialized and ready to use
double v = 3.14, w = 5.5; //declare and Initialize at same time