question « cast « Java Data Type Q&A

1. Java - TypeCasting Question

long a = (long)Math.pow(2, 32);
// a = 4294967296 :)

long a = (int)(long)Math.pow(2, 32);
//a = 0 ?!

long a = (int)Math.pow(2, 32);
//a = 2147483647 WTF??!!!
The first expression is obvious. a is printed as ...

2. Java casting question

I don't understand why I need to cast member variables to the proper type when their types are already declared. For example:

public class SomeClass extends SomethingElse {

  private Funky mFunkyVar;

3. Java casting question

Hi I am unable to understand the following: In java,

long l = 130 L;  
byte b = (byte)l
If you print the value of b, I get -126 .. ? How ...

4. question on understanding "UserInfo user1 = (UserInfo) ois.readObject();"

I am learning java, and studying the following example from

import java.util.*;

public class UserInfo implements Serializable {
   String name = null;

   public UserInfo(String name) {

5. Casting question.

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 ...

6. Casting question

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 ...

7. Casting question

8. casting question

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

9. A cast question

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 ...

10. cast question..plz help...

1. class A { 2. public int getVal() { 3. return 5; 4. } 5. } 6. class B extends A { 7. public int getVal() { 8. return 10; 9. } 10. } 11. public class test{ 12. public static void main(String[] args) { 13. 14. B b = new B(); 15. A a =(A) b; x= a.getVal(); 17.System.out.println( ...

11. Casting Questions

My questions are in the code. Thanks for any help guys. class A { void print() { System.out.println("This is class a"); } } class B extends A { int bnum; void print() { System.out.println("This is class b"); } } public class TestBind { public static void main(String[] args) { A a1 = new A(); B b1 = new B(); A a2; ...

12. CAST question

13. Casting question

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? ...

14. a theoretical question in casting

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 ...

15. Casting question

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

16. Question about casting

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 ...

17. Casting Questions

18. Casting question

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

19. Casting question?

20. Question about casting and inheritances

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 ...

21. Casting question

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 ...

22. Casting Question

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 ...