1. What are the different types of variables in Java?
Ans.
In Java variables are classified into two types.
a. Primitive variables
b. Reference variables
Primitive varibales further classified into following types:
i. Static variables
ii. Instance variables
iii. Local variables
Static variables:
- static variables are also called as Fields or Class variables.
- The variables which are declared at class level and shared by all instances of that class are called "static variables"
- There is only one local copy of static variables exist and shared by all instances of that class
- These variables exists as long as the corresponding class exists in the JVM.
- If your not performing any external initialization then these variables will get initialized with default values
Instance Variables:
- These are also called as members or attributes
- These variables has object scope
- These variables has default values
- The variables which are created for every instance of the class are called "Instance variables".
- The values for these variables varies from instance to instance & these are exists as long as the corresponding object exists on the heap.
Local Variables:
- The variables which are declared inside of any method or block are called "local variables".
- For the local variables there is no default values story.Before using a local variable we should perform external initialization violation leads to COMPILETIME EXCEPTION
- The only allowed modifire for the local variables is final.
2. byte b=1;
b++;
System.out.println( b);
what is the output of above program?
Ans:
The above program prints 2 on the console,here 'b' by default converted to byte type.
3. byte b=1;
b=b+1;
System.out.println( b);
what is the output of above program?
Ans:
The program will not get compiled and gives compilation error saying "Possible loss of precision".This happens because b=b+1 ==> byte = byte + int. This is wrong because byte + int is always int not a byte.
4. Why do we use shift operators in Java?
Ans:
We can use shift operators in J2ME for compression of data and to get high resolution.
5. what is the output of following statement.
System.out.println(10/0);
Ans:
The above line of statement compiles without any issues but at run time give an ArithmaticException.
6. what is the output of following statement.
System.out.println(10.0/0);
Ans.
The above line of statement gives an output of Infinity.Because in Float and Double classes some constants are already defined for an infinite value.
Ans.
In Java variables are classified into two types.
a. Primitive variables
b. Reference variables
Primitive varibales further classified into following types:
i. Static variables
ii. Instance variables
iii. Local variables
Static variables:
- static variables are also called as Fields or Class variables.
- The variables which are declared at class level and shared by all instances of that class are called "static variables"
- There is only one local copy of static variables exist and shared by all instances of that class
- These variables exists as long as the corresponding class exists in the JVM.
- If your not performing any external initialization then these variables will get initialized with default values
Instance Variables:
- These are also called as members or attributes
- These variables has object scope
- These variables has default values
- The variables which are created for every instance of the class are called "Instance variables".
- The values for these variables varies from instance to instance & these are exists as long as the corresponding object exists on the heap.
Local Variables:
- The variables which are declared inside of any method or block are called "local variables".
- For the local variables there is no default values story.Before using a local variable we should perform external initialization violation leads to COMPILETIME EXCEPTION
- The only allowed modifire for the local variables is final.
2. byte b=1;
b++;
System.out.println( b);
what is the output of above program?
Ans:
The above program prints 2 on the console,here 'b' by default converted to byte type.
3. byte b=1;
b=b+1;
System.out.println( b);
what is the output of above program?
Ans:
The program will not get compiled and gives compilation error saying "Possible loss of precision".This happens because b=b+1 ==> byte = byte + int. This is wrong because byte + int is always int not a byte.
4. Why do we use shift operators in Java?
Ans:
We can use shift operators in J2ME for compression of data and to get high resolution.
5. what is the output of following statement.
System.out.println(10/0);
Ans:
The above line of statement compiles without any issues but at run time give an ArithmaticException.
6. what is the output of following statement.
System.out.println(10.0/0);
Ans.
The above line of statement gives an output of Infinity.Because in Float and Double classes some constants are already defined for an infinite value.
No comments:
Post a Comment