Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Chapter Two –Lesson Two

Outline
 Variables and Their Declaration
 Data types
 Escape Sequences
 Input/output method

12/10/2019
What is A Variable?
2

 A Variable is a named memory Location Used for


storing a value
 They are used store an information which your
program need to do it’s job.
 There three basic types of variables.
1. Local Variable
2. Instance Variable
3. Static Variable

12/10/2019
Local Variable
3

 Local variables are declared in methods, constructors, or


blocks.
 Local variables are created when the method, constructor
or block is entered and the variable will be destroyed once it
exits the method, constructor, or block.
 Access modifiers cannot be used for local variables.
 Local variables are visible only within the declared method,
constructor, or block.
 There is no default value for local variables, so local
variables should be declared and an initial value should be
assigned before the first use

12/10/2019
Example
4

public class Test{

public void pupAge(){

int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){

Test test = new Test();


test.pupAge();
}
}

12/10/2019
Instance Variables
5

 Instance variables are declared in a class, but outside


a method, constructor or any block.
 Access modifiers can be given for instance variables.
 Instance variables have default values.
 For numbers, the default value is 0, for
 Booleans it is false, and for object references it
is null.
 They can be accessed by
ObjectReference.VariableName

12/10/2019
Class/static Variables
6

 Class variables also known as static variables are


declared with the static keyword in a class, but
outside a method, constructor or a block.
 There would only be one copy of each class variable
per class, regardless of how many objects are created
from it.
 Static variables can be accessed by calling with
the class name
ClassName.VariableName.

12/10/2019
Variable Declaration
7

 To use a variable, you declare it by telling the


compiler its name as well as what type of data it can
store
 The variable declaration tells the compiler to allocate
appropriate memory space for the variable based on
its data type
 Syntax:-
 datatype variableName;
 Example
 int count; // Declare count to be an integer variable;
 double radius; // Declare radius to be a double variable;

12/10/2019
Cont…
8

 If variables are of the same type, they can be


declared together, as follows:
 datatype variable1, variable2, ..., variablen;
 Example
 inti, j, k;
 By convention, variable names are in lowercase.
 If a name consists of several words,concatenate
all of them and capitalize the first letter of each
word except the first.

12/10/2019
Datatypes
9

 Based on the data type of a variable, the operating


system allocates memory and decides what can be
stored in the reserved memory. Therefore, by
assigning different datatypes to variables, you can
store integers, decimals, or characters in these
variables.
 There are two data types available in Java:
Primitive Datatypes

 Reference/Object Datatypes

12/10/2019
Primitive Data t ypes
10

 There are eight primitive datatypes supported by


Java.
 Primitive datatypes are predefined by the language
and named by a keyword
 This primitive datatypes are:-
 byte, short,long,int,float,double,boolean,char

12/10/2019
This
11

12/10/2019
Reference Data types
12

 Reference variables are created using defined


constructors of the classes.
 They are used to access objects. These variables are
declared to be of a specific type that cannot be changed.
 Class objects and various type of array variables come
under reference datatype.
 Default value of any reference variable is null.
 A reference variable can be used to refer any object of
the declared type or any compatible type.
Example: Animal animal = new Animal("giraffe");

12/10/2019
Escape Sequences
13

 In java a character preceded by a backslash(\) is an


escape sequence and has special meaning to the java
compiler.
Sequence Description
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the
current line; do not advance to the next line. Any characters output
after the carriage return overwrite the characters previously output
on that line.
\\ Backslash. Used to print a backslash character.
\’ Single quote. Used to print a Single quote character.
\" Double quote. Used to print a double-quote character.

12/10/2019
Input/output Method
14

 Java uses System.out to refer to the standard


output device and System.in to the standard input
device.
 By default the output device is the display monitor,
and the input device is the keyboard
 To perform console output, you simply use the
println method to display a primitive value or a
string to the console.
 Console input is not directly supported in Java, but
you can use the Scanner class to create an object to
read input from System.in.

12/10/2019
15

 Syntax

 Scanner input = new Scanner(System.in);


 The syntax new Scanner(System.in)creates an object of the
Scanner type.
 The syntax Scanner input declares that input is a variable
whose type is Scanner.
 The whole line
Scanner input = new Scanner(System.in)
 creates a Scanner object and assigns its reference to the variable
input.
 An object may invoke its methods. To invoke a method on an
object is to ask the object to perform a task

12/10/2019
16

12/10/2019
Example
17

import java.util.Scanner;
public class ScannerApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = sc.nextInt();
System.out.println("You Entered :" + x);
}
Enter an integer:5
} You Entered :5

12/10/2019
Example-Calculating Area of circle
18

importjava.util.Scanner; // Scanner is in the java.util package


public class ComputeArea {
public static void main(String[] args) {

Scanner input = new Scanner(System.in); // Create a Scanner object


System.out.print("Enter a number for radius: "); // Prompt the user to enter a radius
double radius = input.nextDouble();
double area = radius * radius * 3.14159; // Compute area

System.out.println("The area for the circle of radius "+ radius + " is "+
area); // Display result
Enter a number for radius: 2.5
} The area for the circle of radius 2.5 is 19.6349375
}
12/10/2019
Home Work
19

 Write a java program that reads two integer numbers


from the user and add them together and Display the
result.

12/10/2019
20

Question?

12/10/2019

You might also like