Chapter_2
Chapter_2
JAVA BASICS
Variables and
Primitive Data Types
WHAT IS A VARIABLE?
int highScore ;
type name
CREATING VARIABLES (CONTINUED)
It
is good practice to select variable names that
give a good indication of the sort of data they
hold
For example, if you want to record the size of a hat,
hatSize is a good choice for a name whereas qqq
would be a bad choice
When naming a variable, the following
convention is commonly used:
pageCount
loadFile
anyString
threeWordVariable
Which of the following are valid variable names?
1)$amount
2)6tally
3)my*Name
4)salary
5)_score
6)first Name
7)total#
STATEMENTS
A statement is a command that causes something to
happen.
Example:
System.out.println(“Hello, World”);
In
this course, we will always use int
when dealing with integers.
Here are some examples of when you would
want to use integer types:
- byte smallValue;
smallValue = -55;
- int pageCount = 1250;
- long bigValue = 1823337144562L;
double g = 7.7e100 ;
double tinyNumber = 5.82e-203;
float costOfBook = 49.99F;
Example:
boolean monsterHungry = true;
boolean fileOpen = false;
CHARACTER DATA TYPES
Character is a data type that can be used to
store a single characters such as a letter,
number, punctuation mark, or other
symbol.
Example:
char firstLetterOfName = 'e' ;
char myQuestion = '?' ;
Notethat you need to use singular
quotation marks when assigning char data
types.
INTRODUCTION TO STRINGS
Strings consist of a series of characters inside
double quotation marks.
1)Population of Ethiopia
2)Approximation of π
3)Open/closed status of a file
int
4)Your name
double
5)First letter of your name
boolean
6)$237.66
String
char
double
Reserved Words
Integers
Data Type Description
byte Variables of this kind can have a value from:
-128 to +127 and occupy 8 bits in memory
short Variables of this kind can have a value from:
-32768 to +32767 and occupy 16 bits in memory
int Variables of this kind can have a value from:
-2147483648 to +2147483647 and occupy 32 bits in memory
long Variables of this kind can have a value from:
-9223372036854775808 to +9223372036854775807 and occupy
64 bits in memory
PRIMITIVE DATA TYPES (CONT)
Real Numbers
Data Type Description
float Variables of this kind can have a value from:
1.4e(-45) to 3.4e(+38)
JAVASTRUCTURE.PPT