Variables

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

VARIABLES

What is a variable ?

• A variable which holds value, during the life of a Java program.

•Every variable is assigned a data type which designates the type and
quantity of value it can hold.

•In order to use a variable in a program you to need to perform 2 steps


--Variable Declaration
--Variable Initialization
Variable Declaration
To declare a variable, you must specify the data type & give the variable a
unique name.
Variable-
Data-type
name

int count;
Variable Initialization
Container
To initialize a variable, you must assign it a valid value. named “count”
holding a value
100

count=100;
100

count
You can combine variable declaration and initialization.

int count=100;
NAMING CONVENTION OF VARIABLES

• can start with underscore(‘_’) but not with digits.

• Should be mnemonic i.e, designed to indicate to the casual observer the


intent of its use.

• Can use _(underscore), digits and letters.

• Should not use any reserved word.


TYPES OF VARIABLES

Local variables Instance variables Static variables


Consider this code snippet
class Guru99
{
int data = 99; //instance variable
static int a = 1; //static variable
void method()
{
int b = 90; //local variable
}
}
WHAT IS WIDENING?
Consider this code snippet
public class Test Can you predict the output?
{
public static void main(String[] args)
{
YOLO
System.out.print("Y" + "O");
System.out.print('L' + 'O');
} YO155
}
Now, try to predict the output
public class Test
{
public static void main(String[] args)
{
YO7679
System.out.print("Y" + "O");
System.out.print('L');
System.out.print('O'); YOLO
}
}
RULES FOR WIDENING PRIMITVE CONVERSION

•The result of adding Java chars, shorts or bytes is an int.


•If either operand is of type double, the other is converted to
double.
•Otherwise, if either operand is of type float, the other is
converted to float.
•Otherwise, if either operand is of type long, the other is
converted to long.
•Otherwise, both operands are converted to type int
WHAT IS NARROWING?
NARROWING OR EXPLICIT TYPE-CASTING

If we want to assign a value of larger data type to a smaller data


type we perform explicit type casting or narrowing.

•This is useful for incompatible data types where automatic conversion


cannot be done.

•Here, target-type specifies the desired type to convert the specified


value to.
Guess the output
public class Test
{
public static void main(String[] argv)
{
Error
char ch = 'c';
int num = 88;
ch = num;
}
}
Now, try to predict the output
class Simple
{
public static void main(String[] args)
{
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f; 10.5
System.out.println(f); 10
System.out.println(a);
}
}
MCQS
Predict the output

char ‘ data types holds a single character from the.................. character


set.

a) ASCII Code
b)Unicode
c) Both (a) & (b)
d) None of these
Predict the output
class mainclass
{ a) 0
public static void main(String args[])
{ b) 1
boolean var1 = true;
c) true
boolean var2 = false;
if (var1) d) false
System.out.println(var1);
else
System.out.println(var2);
}
}
Predict the output
class booloperators a) 0
{
b) 1
public static void main(String args[])
{ c) true
boolean var1 = true;
boolean var2 = false; d) false
System.out.println((var1 & var2));
}
}
Predict the output
class asciicodes
a) 162
{
public static void main(String args[]) b) 65 97
{ c) 67 95
char var1 = 'A';
d) 66 98
char var2 = 'a';
System.out.println((int)var1 + " " + (int)var2);
}
}
Predict the output
class A{ A. 258 325 325
public static void main(String args[]){ B. 258 326 326
byte b;
C. 2 325 69
int i = 258;
double d = 325.59; D. Error
b = (byte) i;
System.out.print(b);
i = (int) d;
System.out.print(i);
b = (byte) d;
System.out.print(b);
}}
class A
Predict the output
{ A. 10 20 10 100
public static void main(String args[])
B. 10 20 10 20
{
int x; C. 10 20 10 10
x = 10;
if(x == 10) D. Error
{
int y = 20;
System.out.print("x and y: "+ x + " " + y);
y = x*2;
}
y = 100;
System.out.print("x and y: " + x + " " + y);
}
}
Predict the output
public class Test
{
static void test(float x) A. float
{
System.out.print("float"); B. double
}
static void test(double x) C. Compilation Error
{
System.out.print("double"); D. Exception is thrown at runtime
}
public static void main(String[] args)
{
test(99.9);
}
}
Predict the output
public class Test
A. 8 7
{
B. 10 7
public static void main(String[] args)
C. Compilation fails with an error
{
at line 3
int i = 010;
D. Compilation fails with an error
int j = 07; at line 5
System.out.println(i); E. None of these
System.out.println(j);
}
}
Predict the output
public class MyClass
(A) 10
{
public static void main(String[] args) (B) 11
{ (C) 12
int a = 10;
(D) Compilation Error
System.out.println(++a++);
}
}
Predict the output
public class Main
(A) 138
{
public static void main(String[] args) (B) 264
{ (C) 41
int a = 5+5*2+2*2+(2*3);
(D) 25
System.out.println(a);
}
}
Predict the output

What do you mean by >>> operator in Java?


A) Left Shift Operator
B) Right Shift Operator
C) Zero Fill Right Shift
D) Zero Fill Left Shift
Predict the output
class char_increment a) E U
{
public static void main(String args[]) b) U E
{
char c1 = 'D'; c) V E
char c2 = 84;
d) U F
c2++;
c1++;
System.out.println(c1 + " " + c2);
}
}
Predict the output
class c
a) Hello c
{
public void main( String[] args ) b) Hello
{ c) Hello world
System.out.println( "Hello" + args[0] );
d) Runtime Error
}
}
Predict the output
class increment a) 25
{
b) 24
public static void main(String args[])
{ c) 32
int g = 3; d) 33
System.out.print(++g * 8);
}
}
class array_output
Predict the output
{
public static void main(String args[]) a) i i i i i
{ b) 0 1 2 3 4
char array_variable [] = new char[10];
c) i j k l m
for (int i = 0; i < 10; ++i)
{ d) None of the mentioned
array_variable[i] = 'i';
System.out.print(array_variable[i] + "" );
i++;
}
}
}
Predict the output
class conversion
{
public static void main(String args[])
{
A) 38 43
double a = 295.04;
int b = 300;
byte c = (byte) a; B )39 44
byte d = (byte) b;
System.out.println(c + " " + d); C) 295 300
}
} D )295.04 300
Predict the output
class mainclass a)66
{
public static void main(String args[]) b)67
{ c)65
char a = 'A';
a++; d)64
System.out.print((int)a);
}
}
class variable_scope Predict the output
{
public static void main(String args[]) a) 5 6 5 6
{
b) 5 6 5
int x;
x = 5; c) Runtime error
{
d) Compilation error
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
Predict the output
class dynamic_initialization
{
public static void main(String args[]) a) 5.0
{
double a, b; b) 5
a = 3.0;
c) 7
b = 4.0;
double c = Math.sqrt(a * a + b * b); d) Compilation Error
System.out.println(c);
}
}
Predict the output
import java.lang.Math;
class dynamic_initialization
{
public static void main(String args[]) a) 5.0
{
double a, b; b) 5
a = 3.0;
c) 7
b = 4.0;
double c = Math.sqrt(a * a + b * b); d) Compilation Error
System.out.println(c);
}
}
Predict the output
class output a) Infinity
{
public static void main(String args[]) b) 0.0
{
c) NaN
double a, b,c;
a = 3.0/0; d) all of the mentioned
b = 0/4.0;
c=0/0.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
THANK YOU

You might also like