The following code will not compile, as, 3.5 is a double. Makes sense. It needs an explicit casting. float f; f = 3.5; But, the code below would just compile fine, eventhough 5 is an integer by default. byte b; b = 5; Why the compilier is not enforcing an explicit casing in the second scenario? What is the reason for ...

How to do this ? 1. I have a data object like this: public class data { public int id; public String name; //getters and setters } 2. I have a String[] of this data object. String[] values = request.getParameterValues(""); //values are values from jsp with data object. 3. How do I do extract the id and name fields from this ...

Hi I have a hopefully easy question. If i have collection which contains objects of specific type and then i iterate through this collection am i right in saying the iterator will return an object of class Object? Can I then cast the objects back to their original datatypes? is that possible without loosing any variable information? Thanks

My JDK version is 1.4.2_04 I saw a book said : byte b = 27; the compiler would put it in the cast automatically into byte b = (byte) 27; but if I tried byte b = 128; the compiler complianed : possible loss of precision but if I tried byte b = (byte) 128; is ok. my question is if ...

If this has been asked recently feel free to direct me to the right post... I understand it is possible to downcast a parent object to a child object. For example: Asuusming child extends parent Parent p = new Parent(); Child c = new Child(); c = (Child)p; When would this be necessary? And when would this be a bad idea? ...

there are two classes the one called quiz and it defined as the father it has a method called "num" the other class called quiz1 defined as the son it has the same method called "num" and it has a method called "num1" so i guess that the signature of quiz is public class quiz1 extends quiz we have a variable ...

As mentioned in the other topic, the first rule that you are referring to, is related to assignment of compile time constants. During assignments, if the compiler is able to figure out, at compile time obviously, that the value will fit, then no cast is needed. And there is no such related rule for method calls. Henry

Casting T to MyType is pointless, because that's what it's declared as. It also doesn't help you because the result of the cast is MyType, which does not implement Cloneable. You need to cast to Cloneable, not MyType. I wasn't trying to write some meaningful code here, I would never write such code in real life, but I was just trying ...

I am new to Java. i would appreciate if someone can help with the following. i am wondering why declaring "ageSeconds" as below works long ageSeconds = (long)ageDays*24*60*60; and why it doesnt work when you declare using the the parenthesis as following long ageSeconds = (long)(ageDays*24*60*60); would appreciate a detailed answer. Thanks

Strictly speaking if the method returns a Collection, then no amount of casting will turn it into a Collection while still being type safe. The problem is that a Collection could contain a Cat and if you cast it to a Collection, it would contain an invalid value. You could use raw types (i.e. simply "Collection") to "forget" the generic type ...

The first one does integer division (intVar1/intVar2), which always produces an integer result. Then this is multiplied by 100, then cast to a double, then cast back to an int. The second one first casts intVar1 to a double, then divides by intVar2. A double divided by an int is a double. Then you take it from there. Note: It has ...

There is still a certain amount of ambiguity in your statement. When you say generic, do you mean Java Generics specifically, or are you using the word figuratively speaking? Generally, when you do use Java Generics, it's because you are avoiding casts, not to purposely add them. Also, you can't just make any statement generic (Java Generics) that you want. The ...