College of Science Department of Computer Programing Fundamentals (Java) First Stage
College of Science Department of Computer Programing Fundamentals (Java) First Stage
Department of Computer
Programing Fundamentals (Java)
First Stage
Prepared by:
Ari M.Saeed
2018 - 2019
Outline
Variables.
Data Types.
Literals.
Exercises.
References.
2
Variables
Variable is name of reserved area allocated in memory. In
other words, it is a name of memory location. It is a
combination of "vary + able" that means its value can be
changed.
int data=10;//Here data is variable (field)
3
Declaring and Initializing Variables
• A variable is a container that holds values.
• Every variable must be declared to use a data type.
4
Declaring and Initializing variables
int numberOfDays;
int numberOfDays = 7;
char letter=‘A’;
5
Choosing Variable Names
The rules and conventions for naming variables:
• If the name consists of only one word, spell that word in all
lowercase letters.
• They cannot start with a digit but digits can be used after the
first character (Name1, n2ame are valid).
7
Keywords or Reserved Words
8
Declaring Valid and Invalid Variables
• Declaring some variables to show rules and conventions
for naming variables:
Parameters
• A parameter is a variable that you pass in to a method.
• The args variable is the parameter variable in
the main method public static void main(String[] args).
11
Kinds of Variables
Class Variable
(Static Field)
class Hotel{
int noOfGuests=12;
Parameter
short rentRoom=35;
Local Variable
}
}
12
Data Types
• Every variable in java has a data type. Data types specify
the size and type of values that can be stored. The varieties
of data types available allow the programmer to select the
type appropriate to the needs of the application. Data types
in Java under various categories are shown in figure below.
13
Primitive Data Types
• The eight primitive data types supported by the Java are:
14
Primitive Data Types
• It's not always necessary to assign a value when a field is
declared.
15
Primitive Data Types and Default Values
16
Literals
• A literal is a value that can be written directly into a Java
program.
• new keyword is not used when initializing a variable of a
primitive type.
Integer Literals:
17
Literals
• Values of the integral types byte, short, int, and long can be
created from int literals.
• Values of type long that exceed the range of int can be
created from long literals.
19
Literals
Floating-Point Literals
• A floating-point literal is of type float if it ends with the
letter F or f.
• otherwise its type is double and it can optionally end with
the letter D or d.
double d1 = 123.4;
double d2 = 1.234e2; // same value as d1, but in scientific notation
float f1 = 123.4f; // same value as d1, but in scientific notation
20
Character and String Literals
• Literals of types char and String may contain any Unicode (UTF-
16) characters.
• Use 'single quotes' for char literals.
• Use "double quotes" for string literals.
• A few special escape sequences for char and String literals.
ESCAPE SEQUENCES REPLACED
\b (backspace)
\t (tab)
\n (line feed)
\f (form feed)
\r (carriage return)
\" (double quote)
\' (single quote)
\\ (backslash)
21
Using Underscore Characters in Numeric Literals
• underscore characters (_) uses to separate groups of digits in
numeric literals.
float pi = 3.14_15F;
• Prior to an F or L suffix.
23
Using Underscore Characters in Numeric Literals
Valid or
Examples Why?
invalid
float pi1 = 3_.1415F; Invalid adjacent to a decimal point
float pi2 = 3._1415F;
long Number1 = 999_L; Invalid prior to an L suffix
24
Exercises
Why compiler never assigns a default value to an uninitialized local
variable ? Explain by using java example code.
Is there any error in the following code? If yes, Correct it.
class Secondexe{
25
References
Java in a Nutshell, 6th Edition, By David Flanagan,2015
http://java.sun.com/docs/books/tutorial
http://www.vogella.com
http://journals.ecs.soton.ac.uk/java/tutorial
26