More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
In Java, type casting is classified into two types, Widening Casting(Implicit) widening-type-conversion and Narrowing Casting (Explicitly done) narrowing-type-conversion.
Widening or Automatic type converion - Automatic Type casting take place when,the two types are compatible and the target type is larger than the source type
Example :
public class Test {
public static void main(String[] args) {
int i = 100;
long l = i; //no explicit type casting required
float f = l;//no explicit type casting required
System.out.println("Int value " i);
System.out.println("Long value " l);
System.out.println("Float value " f);
}
}
Narrowing or Explicit type conversion - When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.
Example :
public class Test{
public static void main(String[] args) {
double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l;//explicit type casting required
System.out.println("Double value " d);
System.out.println("Long value " l);
System.out.println("Int value " i);
}
}
Help us improve. Please let us know the company, where you were asked this question :
Q697. In what terms Java is better language than c and c++ ?
Ans. Java manages pointers intrinsically and gives no control over pointer arithmetic to coders and hence saves the applications from many possible problems like dangling pointers and memory leaks.
Java is platform independent and hence any application can be run on any hardware with the suitable JVM without building it separately for different hardwares.
Help us improve. Please let us know the company, where you were asked this question :
Ans. We have followed this practice in one of the project. The only downside we felt was the need to merge the new snapshot version to master after the release.
The Benefit is that it's easy to just ignore the branch if complete rollback happens and hence master remains in sync with production.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Unreachable code is a code segment that can never be executed in any case. For example -
int x = 5;
return;
System.out.println(x);
In this code , 2nd line will return the control out of the method and hence 3rd line will never get executed and hence is unreachable code. This will be caught at compile time only.
Help us improve. Please let us know the company, where you were asked this question :
Ans. yes, we can.
exa: public class Java{
public static void main(String... args) {
JAva j= null;
j.greeting(); // call with null reference
}
public static void greeting() {
System.out.println("Hello World");
}
} // output: Hello World
Help us improve. Please let us know the company, where you were asked this question :