short s = Short.MAX_VALUE;
char c = s;
System.out.println( c == Short.MAX_VALUE);
Correct Option is : B
A. True
B. False
Explanation:
This will not compile because a short VARIABLE can NEVER be assigned to a char without explicit casting. A short CONSTANT can be assigned to a char only if the value fits into a char.
short s = 1; byte b = s; => this will also not compile because although value is small enough to be held by a byte but the Right Hand Side i.e. s is a variable and not a constant. final short s = 1; byte b = s; => This is fine because s is a constant and the value fits into a byte. final short s = 200; byte b = s; => This is invalid because although s is a constant but the value does not fit into a byte. Implicit narrowing occurs only for byte, char, short, and int. Remember that it does not
occur for long, float, or double. So, this will not compile: int i = 129L;The below code compiles fine and contradicts what is said in bold. So what does the bold statement mean then?
Java Code: class BreakTest{
public static void main(String args[])
{
float f=1.0f;
double d=f;
}
} mh_sh_highlight_all('java');
I know that there are 9 implicit objects that are available in a JSP page , except the page implicit object every object has certain utility ,but i am not able to understand the usage of a 'page' implicit object.
I've read it is actually an object of the servlet which is obtained after compiling this JSP page ,moreover a reference of Object class hold this 'page' object so there are only those 11 legacy methods of Object class which anyone can invoke on it and even if the 'page' object is typecasted to our generated servlet class it will also be of no use until there are any instance methods or fields(which are not Thread-safe) declared through <jsp:declaration> tag ,and writing instance method through jsp:declaration will again bring us to the old discussion of avoiding java code from JSPs..
I am trying to compare some items from a generic arraylist with each other, but I keep getting an error stating that I need to cast the values in line 38. However, when I heed the warning and change it to what it wants, I get a warning stating "type safety: Unchecked cast from K to Comparable<K>". Should I ignore this warning or is there a better way to compare the two items? Also, is there another way for me to use compareTo w/o making my class extending/implementing comparable or is that the only way?Here is what I have:
class WordInfo<K, V extends Comparable <K>> { private FileReader fr; private String word; private ArrayList<K> list; private BufferedReader br; private int current = 0;
I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,but Here is a program in which i am getting Class Cast Exception
class Animal { } class Dog extends Animal { } class Cat extends Animal
[code]..
Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?
I'm looking for a heuristic explanation of how to think of an "interface" as a type. I'm used to think of the 'type' of a class coming form its very definition but I often see casting to an interface which I still feel very uncomfortable about.Other than an interface, are there other unusual ways a 'type' may be referred to?
A second basic question: When you user 'super.f()', will Java go up the calling chain until it finds method 'f' (and report an err if none is found) or does it expect to find 'f' immediately at its very first parent?
import java.io.IOException; import java.util.*; public class Guesser { public static void main(String[] args) throws IOException { char[] alphabet = "abcdefghijklmnopqrstuvwxyz1234567890 .,:;'-".toCharArray();
[Code] .....
I'm writing a program which will take a three letter word (for now) and then try to guess the word over and over again until it finds it, then print the word and the amount of tries it took to find it.
The problem: at the moment the program will find the word but not break out of the for loop when it does. I think it doesn't like the char to String conversion somewhere along the line.
how to calculate the child's height in float value fixing value where if you choose male the accurate value, but if you choose female the value will be accurate too.
int heightMother, heightFather; int heightMaleChild, heightFemaleChild; String gender;
There is a sentence in JLS 7 which I can't figure it out. It says :
A cast from a type S to a parameterized type T is unchecked unless at least one of the following conditions holds: -S <: T -All of the type arguments (§4.5.1) of T are unbounded wildcards -T <: S and S has no subtype X other than T where the type arguments of X are not contained in the type arguments of T.
Condition one and two I got it. But the number three is really bugging me. I write some code in order to try to understand it.
class G<X>{} class D<T,U> extends G<T>{} G<String> g = new G<>(); D<String, Integer> dd = (D<String, Integer>) g;
In Eclipse I got no warning but it shouldn't give one ?
Because g has others subtypes than D<String, Integer> (e.g. D<String, List> , D<String, G>)
Am I missing something about the contained type arguments ?
import java.util.*; public class CommonElements { private int comparisons; // number of comparisons made private Comparable[] arrayToSearch; // current array being traversed private Comparable[] commonElements = new Comparable[10]; private int arrayPosition = 0; //keeps track of what index to add an element to common at
[Code] ...
I have trying to get this down to the bar minimum. I am trying to cast the desired object array to a array of comparable. This is all required by the assignment.
I am getting a runtime error that I can not perform the desired cast. What do I need to provide the compiler in order to allow for this casting. I can not change the signature of the method however nothing about the class has been specified do I need to implement comparable? Also I don not now what the client is passing so how would I write a generic compareTo method to compare object of unknown types.