CHAPTER 2
JAVA BASICS
Variables and
Primitive Data Types
WHAT IS A VARIABLE?
Variables are places where information can be
stored while a program is running.
Their values can be changed at any point over the
course of a program
CREATING VARIABLES
To create a variable, declare its name and the type of
information that it will store.
The type is listed first, followed by the name.
Example: a variable that stores an integer representing
the highest score on an exam could be declared as follows:
int highScore ;
type name
CREATING VARIABLES (CONTINUED)
Now you have the variable (highScore),
you will want to assign a value to it.
Example: the highest score in the class
exam is 98.
highScore = 98;
Examples of other types of variables:
String studentName;
boolean gameOver;
NAMING VARIABLES
The name that you choose for a variable is
called an identifier. In Java, an identifier
can be of any length, but must start with:
a letter (a – z),
a dollar sign ($),
or, an underscore ( _ ).
Therest of the identifier can include any
character except those used as operators in
Java such as + , - , * .
Inaddition, there are certain keywords
reserved (e.g., "class") in the Java language
which can never be used as identifiers.
Naming (Continued)
Javais a case-sensitive language – the
capitalization of letters in identifiers matters.
A rose is not a Rose is not a ROSE
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:
The first letter of a variable name is lowercase
Each successive word in the variable name begins
with a capital letter
All other letters are lowercase
Here are some examples:
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.
All statements in Java are separated by semicolons ;
Example:
System.out.println(“Hello, World”);
You have already used statements to create a variable
and assign it a value.
VARIABLES AND STATEMENTS
One way is to declare a variable and then assign a
value to it with two statements:
int e; // declaring a variable
e = 5; // assigning a value to a variable
Another way is to write a single initialization
statement:
int e = 5; // declaring AND assigning
JAVA IS A STRONGLY-TYPED
LANGUAGE
All variables must be declared with a data type before
they are used.
Each variable's declared type does not change over the
course of the program.
Certain operations are only allowed with certain data
types.
If you try to perform an operation on an illegal data
type (like multiplying Strings), the compiler will report
an error.
DATA TYPES
Every variable in java should has a type
Every expression in java should has some type
Each and every assignment should be checked by
compiler for type compatibility. Because of the above
reasons we can conclude java is strongly typed
programing language.
Is Java Pure OOPL? Assignment
PRIMITIVE DATA TYPES
There are eight built-in (primitive) data types in the Java
language
4 integer types (byte, short, int, long)
Byte:
1 byte= 8 bits( max value that can be represented is +127 and
min value is -128 and range -128 to 127)
Byte is the best choice if we want to handle data in terms of
streams either from the file or from the network(file supported
form or network supported form ) is byte.
Short:
Size 2 byte(16 bits)
The most rarely used data type in java.
When to use?
16 bit processors are very popular in 8085/8086 starting 1995 up
to some time. So, short is most suitable before the processors are
outdated.
CNTUD…
Int: most commonly used data type.
Size: 4 byte(32 bits)
2 floating point types (float, double)
Float:
5 to 6 decimal places of accuracy
follows single precision
Size 4 byte
Double:
14 to 15 precision
Size 8 byte
Boolean (boolean) True/False
Character (char) 2 byte/16 bits
Except char and Boolean remaining data types are
signed data type. They can represent both negative and
positive numbers.
INTEGER DATA TYPES
There are four data types that can be used
to store integers.
The one you choose to use depends on the
size of the number that we want to store.
Data Type Value Range
byte -128 to +127
short -32768 to +32767
int -2147483648 to +2147483647
long -9223372036854775808 to +9223372036854775807
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;
Note: By adding an L to the end of the value in the last
example, the program is “forced” to consider the
value to be of a type long even if it was small
enough to be an int
FLOATING POINT DATA TYPES
There are two data types that can be used
to store decimal values (real numbers).
The one you choose to use depends on the
size of the number that we want to store.
Data Type Value Range
float 1.4×10-45 to 3.4×1038
double 4.9×10-324 to 1.7×10308
Inthis course, we will always use double
when dealing with decimal values.
Here are some examples of when you would want to
use floating point types:
double g = 7.7e100 ;
double tinyNumber = 5.82e-203;
float costOfBook = 49.99F;
Note: In the last example we added an F to the end of
the value. Without the F, it would have automatically
been considered a double instead.
BOOLEAN DATA TYPE
Boolean is a data type that can be used in
situations where there are two options, either
true or false.
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.
Examples statements assign String
variables:
String coAuthor = "John Smith";
String password = "swordfish786";
Strings are not one of the primitive data
types, although they are very commonly used.
Stringsare constant; their values cannot be
changed after they are created.
What data types would you use to store the following
types of information?:
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
The following keywords are reserved in the Java
language. They can never be used as identifiers:
abstract assert boolean break byte
case catch char class const
continue default do double else
extends final finally float for
goto if implements import instanceof
int interface long native new
package private protected public return
short static strictfp super switch
synchronized this throw throws transient
try void violate while
PRIMITIVE DATA TYPES
The following tables show all of the primitive
data types along with their sizes and formats:
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)
double Variables of this kind can have a value from:
4.9e(-324) to 1.7e(+308)
Other Primitive Data Types
char Variables of this kind can have a value from:
A single character
boolean Variables of this kind can have a value from:
True or False
PROGRAM STRUCTURE
JAVASTRUCTURE.PPT