Professional Documents
Culture Documents
Used when we don’t want to modify the value from object to object.
class VariableDemo
{
public static void main(String [] rk)
{
int x = 10;//if used without initialization will give error(int x)
System.out.print(x);
}
}
Brainstorming 2
What will be the output of the following Program?
class VariableDemo
{
static int x;
public static void main(String [] rk)
{
System.out.print(x);
}
}
Variable Using Object
class VariableDemo
{
int x=100;
public static void main(String [] rk)
{
VariableDemo v1=new VariableDemo();
System.out.print(v1.x);
}
}
Different Object References
class VariableDemo
{
int x=100;
public static void main(String [] rk)
{
VariableDemo v1=new VariableDemo();
System.out.print(v1.x);
VariableDemo v2=new VariableDemo();
v2.x=200;
System.out.print(v2.x);
VariableDemo v3=new VariableDemo();
v3.x=400;
System.out.print(v3.x);
}
}
Brainstorming 3
What will be the output of the following Program?
class VariableDemo
{
static int x;
public static void main(String [] rk)
{
System.out.print(VariableDemo.x);
//OR System.out.print(x);
}
}
class Abc
{
static String college=“LPU";//static variable
static int x=10;//static variable
int std_id;//instance variable
public static void main(String a[])
{
Abc a1=new Abc();
Abc a2=new Abc();
a1.std_id=1;
a2.std_id=2;
System.out.println(a1.std_id);
System.out.println(a2.std_id);
System.out.println(Abc.x);
System.out.println(college);//no need to call through classname, because it is static and called in the
static method i.e. main
}
}
Class VarDemo
{
static int i=0;
VarDemo()
{
i++;
System.out.println(i);
}
Public static void main(String a[])
{
VarDemo v1=new VarDemo();
VarDemo v2=new VarDemo();
VarDemo v3=new VarDemo();
VarDemo v4=new VarDemo();
VarDemo v5=new VarDemo();
VarDemo v6=new VarDemo();
}
}
Data Types
• Data types represent the different values to
be stored in the variable.
• In Java, there are two types of data types:
1.Primitive data types
2.Non-primitive data types
Why char uses 2 byte in java and what is \u0000 ?
It is because java uses Unicode system than ASCII code system.
The \u0000 is the lowest range of Unicode system.
Java Variable Example: Add Two Numbers
class Simple
{
public static void main(String[] args)
{
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
Output:
20
Java Variable Example: Widening
class Simple
{
public static void main(String[] args)
{
int a=10;
float f=a;
System.out.println(a);
System.out.println(f);
}
} Output:
10
10.0
Java Variable Example: Narrowing (Typecasting)
class Simple
{
public static void main(String[] args)
{
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
System.out.println(f);
System.out.println(a);
}
} Output:10.5
10
Java Variable Example: Overflow
class Simple
{
public static void main(String[] args)
{
//Overflow
int a=130;
byte b=(byte)a;
System.out.println(a);
System.out.println(b);
}
} Output:130
-126
Java Variable Example: Adding Lower Type
class Simple
{
public static void main(String[] args)
{
byte a=10;
byte b=10;
//byte c=a+b;//Compile Time Error: because a+b=20 will be int
byte c=(byte)(a+b);
System.out.println(c);
}
} Output: 20