Default Variable Values
String = null; (programmed to nothing)
float = 0.0;
booleans = false;
int/char = 0;
1)Instance Variables:
->Declared in class but not in method
->Have default value
class book{
private String name; (null)
private int size; (defalut value size = 0)
}
2)Local Variables
->Declared within method
->Not have default value (not initialized & should be initialized before used)
class add{
void result(){
int a;
int b;
//a and b should have value otherwise it won't compile gives error.
int result = a + b;
}
}
👉 Comparing variable
*Primitive Type:-
->two variable is same if bit pattern same
->size not matter
int a = 3;
byte b = 3;
if(a == b){//true}
*Reference type ->if two reference variable refer to same object
Dog a = new Dog();
Dog b = new Dog();
Dog c == a;
if(a == b){//false};
if(c == a){//true};
if(b == c){//false};
Note:->use == for comparing two primative or reference variable if refers to same object
->Use .equals() for two different object if equal (ex. if two String object have value "Fred").
0 Comments