Khana-e Noor University
Computer Science Faculty
Fundamentals Of
Programming (Java)
Chapter 2
Outline
Data types in Java Language
Variables in Java Language
Access Modifiers
Data types in Java
Data types specify how we enter data
into our programs and what type of data
we enter.
Java language has some predefined set
of data types to handle various kinds of
data that we can use in our program.
These datatypes have different storage
capacities.
Data Types in Java
Java language supports 2 different type
of data types:
Primary (Built-in) data types:
These are fundamental data types in
Java namely :
integer (int), floating point (float),
character (char), void, …
Data Types in Java
Derived data types:
Derived data types are nothing but
primary datatypes but a little twisted
or grouped together
like array and pointer. These are
discussed in details later.
Data types and Keywords
Data types and ranges
Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
int 4bytes -2147483648 to 2147483647
short 2bytes -32768 to 32767
long 4bytes -2,147,483,647 to 2,147,483,647
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
Variables in Java Language
Java variable is a named location in a
memory where a program can manipulate
the data.
This location is used to hold the value of
the variable.
The value of the Java variable may get
change in the program.
Java variable might be belonging to any of
the data type like int, float, char,… etc.
Rules of naming variables
Variable name must begin with letter or
underscore.
Variables are case sensitive
They can be constructed with digits, letters.
No special symbols are allowed other than
underscore.
Examples: int number; double _score;
char Alphabet;
Variable Initialization and
Declaration
Variables should be declared in the Java
program before to use.
Memory space is not allocated for a
variable while declaration.
Variable initialization means assigning a
value to the variable.
Variable Initialization and
Declaration
Scope Rules in Java
A scope in any programming is a region of the
program where a defined variable can have its
existence and beyond that variable it cannot
be accessed.
Inside a function or a block which is
called local variables.
Outside of all functions which is
called global variables.
Local Variables
Variables that are declared inside a function or
block are called local variables.
They can be used only by statements that are
inside that function or block of code.
Local variables are not known to functions
outside their own.
Here all the variables a, b, and c are local to
main() function.
Local Variable
Instance variables
Instance variables are declared in a
class, but outside a method or any
block.
Access Control Modifiers
Java provides a number of access modifiers to
set access levels for classes, variables, methods
and constructors.
The three access levels are:
1.Visible to the class only (private).
2.Visible to the world (public).
3.Visible to the package and all subclasses
(protected).
Access Control Modifiers
Access Control Modifiers
Any Questions?