0% found this document useful (0 votes)
29 views

College of Science Department of Computer Programing Fundamentals (Java) First Stage

The document provides an overview of variables, data types, and literals in Java programming, including how to declare and initialize variables, choose descriptive variable names, and define variables as instance, class, local, or parameter types. It also examines the primitive data types in Java and how to represent numeric and character literals using decimal, hexadecimal, binary, octal, floating-point, and escape sequence formats.

Uploaded by

bashdar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

College of Science Department of Computer Programing Fundamentals (Java) First Stage

The document provides an overview of variables, data types, and literals in Java programming, including how to declare and initialize variables, choose descriptive variable names, and define variables as instance, class, local, or parameter types. It also examines the primitive data types in Java and how to represent numeric and character literals using decimal, hexadecimal, binary, octal, floating-point, and escape sequence formats.

Uploaded by

bashdar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

College of Science

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.

• For example, a variable could be declared to use one of the


eight primitive data types: byte, short, int, long, float,
double, char or boolean.

• Before a variable can be used it must be given an initial


value this is called initializing the variable.
• To initialize a variable we use an assignment statement
("=“).

4
Declaring and Initializing variables

int numberOfDays;
int numberOfDays = 7;
char letter=‘A’;

5
Choosing Variable Names
The rules and conventions for naming variables:

• Variable names are case-sensitive. 


• A variable's name can be an unlimited-length sequence.

• Beginning with a letter, the dollar sign "$", or the


underscore character "_". 

• If the name consists of only one word, spell that word in all
lowercase letters.

• If it consists of more than one word, capitalize the first letter


of each subsequent word. For example eyesColor;
6
Choosing Variable Names
• If your variable stores a constant value capitalizing every
letter and separating subsequent words with the underscore
character such as:  static final float PI = 3.14;

• White space is not permitted.

• You cannot use other symbols ( "%","^","&","#").

• They cannot start with a digit but digits can be used after the
first character (Name1, n2ame are valid).

• keyword or reserved word cannot be used.

7
Keywords or Reserved Words

8
Declaring Valid and Invalid Variables
• Declaring some variables to show rules and conventions
for naming variables:

• int _$_; //(valid)


• int ca$h; //(valid)
• float java5share; //(valid)
• char all@hands; //(invalid)
• double 123amc; //(invalid)
• String Total#; //(invalid)
• Int k; //(invalid)
• Integer a; //(valid)
• int while; //(invalid)
9
 Kinds of Variables
 Instance Variables (Non-Static Fields):
• Fields declared without the static keyword.
• Non-static fields are also known as instance variables.
• instance variables mean their values are unique to
each instance of a class(to each object, in other words).

 Class Variables (Static Fields):


• A class variable is any field declared with
the static modifier.
• This tells the compiler that there is exactly one copy of this
variable in existence. For example:
static final float PI = 3.14;
• Additionally, the keyword final could be added to indicate
that the PI will never change.
10
 Kinds of Variables
 Local Variables: 
• A local variable is a variable that’s declared within the
body of a method.
• It can be used this variable only within that method.
• They are not accessible from the rest of the class.

 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{

final static int noOfRooms=5; Instance Variable


(Non-Static Field)

int noOfGuests=12;
Parameter

public static void main(String[] args) {

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:

byte, short, int, long, float, double, char or boolean.

• The Java also provides special support for character strings


via the java.lang.String class.

• Enclosing your character string within double quotes will


automatically create a new String object. For example, 

String s = “This is a string";

14
Primitive Data Types
• It's not always necessary to assign a value when a field is
declared.

• Fields that are declared but not initialized will be set to a


reasonable default by the compiler.

Tips: The compiler never assigns a default value to an


uninitialized local variable because of compile-time error.

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:

• An integer literal is of type long if it ends with the


letter L or l; otherwise it is of type int.

• Use the upper case letter L because the lower case letter l


to distinguish from the digit 1.

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.

Integer literals can be expressed by these number


systems:

• Decimal: Base 10, start from 0 to 9.


• Hexadecimal: Base 16, start from 0 to 9 and the letters A to
F.
• Binary: Base 2, whose digits consists of the numbers 0 and
1.
18
Literals
• The hexadecimal uses the prefix 0x.
• The binary uses prefix  0b.

int decVal = 26; // The number 26, in decimal

int hexVal = 0x1a; // The number 26, in hexadecimal

int binVal = 0b11010; // The number 26, in binary

int octl = 032; // The number 26, in octal

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.

• The floating point types (float and double) can also be


expressed using E or e (for scientific notation), F or f and 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.

Tips: You can place underscores only between digits

long creditCardNumber = 1234_5678_9012_3456L;


long socialSecurityNumber = 999_99_9999L;

float pi = 3.14_15F;

long hexBytes = 0xFF_EC_DE_5E;


long hexWords = 0xCAFE_BABE;
22
Using Underscore Characters in Numeric Literals
You cannot place underscores in the following places:

• At the beginning or end of a number.

• Adjacent to a decimal point in a floating point literal.

• Prior to an F or L suffix.

• In positions where a string of digits is expected.

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

int x1 = 5_2; valid decimal literal


int x3 = 5_______2;
int x2 = 52_; Invalid At the end of a literal
int x7 = 0x52_;
int x4 = 0_x52; Invalid in the 0x radix prefix

int x5 = 0x_52; Invalid at the beginning of a number

int x6 = 0x5_2; valid hexadecimal literal

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{

public static void main(String[] args) {


char symbol = A;
byte salary = 320;
String name=Ahmad;
int binVal =110100b;
System.out.print("symbol = "+symbol+"\n"+"salary = "+salary+"\n");
System.out.print("name = "+name+"\n"+"binVal = "+binVal+"\n");
}
}

25
References
Java in a Nutshell, 6th Edition, By David Flanagan,2015

Introduction to programing with java, John S.Dean,2008

http://java.sun.com/docs/books/tutorial

http://www.vogella.com

http://journals.ecs.soton.ac.uk/java/tutorial

26

You might also like