Cast « Integer « Java Data Type Q&A

1. Why casting from a stack to int is not running in Java?

I'm trying to cast a char that is stored in stack to an interger and this is what I did.

operands = new StackLinked();

if ( (a == '0') || (a == '1') ...

2. Java implicit conversion of int to byte

I am about to start working on something the requires reading bytes and creating strings. The bytes being read represent UTF-16 strings. So just to test things out I ...

3. How to cast a double to an int in Java?

I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99 Any ideas? :D

4. How can I change an int to a String for this object?

I'm learning Java here, and using a "GLabel" object. It's in the ACM Graphics library, and you can read about it here: In short, the GLabel prints a string. ...

5. How are integers casted to bytes in Java?

I know Java doesn't allow unsigned types, so I was wondering how it casts an integer to a byte. Say I have an integer a with a value of 255 and ...

6. Casting objects to Integer,string ,

I have one small problem. I have list of types(int,string,..)

ArrayList<Class> typeList;
and I have some input values;
ArrayList<Object> values;
How to cast some value to some type if I know which type from ...

7. Java Convert 4 bytes to int

i was wondering if the solution for this documented here is still the solution or is there any other way getting an int from 4 bytes? thank you. EDIT: im getting the ...

8. How to cast an Object to an int in java?

How can I cast an Object to an int in java?

9. how to cast string to integer at runtime

I am using reflection in java. I am getting to know the type of method parameter I am passing at run time. So I am fetching the parameter value from file ...

10. How to convert an integer value to string?

How do I convert an integer variable to a string variable in Java?

11. Integer or double return value

I have an Integer value been passed in and then it is divided by 100, so result could either be an int or double so not sure if cast it or ...

12. Why does casting Double.NaN to int not throw an exception in Java?

So I know the IEEE 754 specifies some special floating point values for values that are not real numbers. In Java, casting those values to a primitive int does not ...

13. Integer to byte casting in Java

In Java we can do

byte b = 5;    
But why can't we pass same argument to a function which accepts byte
public void testByte (byte b)
  System.out.println("Its ...

14. Why can't we cast an integer object to a string object?

Why can't we cast an integer object to a string object? Please Explain in detail.

Integer obj = new Integer(10);
Does covariant return types come into the picture here?

15. Java Can not Cast from String to Integer_Sovled

HashMap<String, String> config = Feeds.config; 

String num =  config.get("NumOfFeeds");


feedsAmount = ((Integer)num).intValue();

I've also tried Integer.parseInt(num) and Integer.decode(num) and Integer.valueof(bu) Results as follows: 40
Exception in thread "main" java.lang.NumberFormatException: For input string:"40"
at java.lang.NumberFormatException.forInputString(
at java.lang.Integer.parseInt(
at ...

16. Casting Problem int to String

I can't cast an int to a string so I can compare the values. I get an incovertable types error when In try the assign the value of i to j as a string so I can check that the value matched in an option select box. Can someone point me in the right direction. for( int i=1; i<=31; i++) { ...

17. Casting int value to Byte

thats the way -ve numbers are represented. if the 8th bit is 0 then its a +ve number and you "convert" the remaining 7 bits to find the integer value. if the 8th bit is 1 then we are looking at a -ve number. so you 2's complement the remaining 7 bits, get integer value and put a - in front. ...

18. Cast Object to int[]

Hi, Welcome to JavaRanch! Yes, it's possible, but only if the object actually is an int[]. In other words, this is legal: Object o = new int[5]; int[] ia = (int[]) o; When casting object references, the cast never makes any change to the object itself; it's only an instruction to the compiler to treat the object as a different type. ...

19. from int to Integer casting?

Mike, The cast exmaple that you have given below won't work with the given question. The count variable is of type int. Compiler would give an error message saying Invalid cast from int to Integer Integer i = (Integer) obj; //cast Integer i =(Integer)count; // Error -> Invalid cast from int to Integer

20. cast a String to an int, how?

Hi, Can someone tell me how to convert a type String to a type int. I have values in a hashtable and I am using the 'put' method to put a value in the hashtable. Here is the function public void setAgehashtable( int i, int age) { this.age=age; ageHashtable.put(new Integer(i),new Integer(age)); } Then I'm calling the 'getAgehashtable( int i)' method, like ...

21. Cast of double to int after mathematical operation

In this example a divide an double through an int. According to numeric promotion result of this operation is an double which I explicitly cast to int to assign it to int y. public class Test60 { public static void main(String ar[]) { double i=4; int j=3; int y; y = (int) i/j; //Numeric promotion: math operation involving int //and double ...

22. Casting an int to a byte

Hi all, I am running a simple test to see what happens when i cast an int with a value of -129 into a byte. My understanding is that when narrowing into an integral type that only the n lowest order bits are used where n is the size of the type to which you are casting. This means when casting ...

23. casting int to Integer

Hi there, been kicking around a program that will dump random numbers into an array. I have the numbers working correctly and I have the array's converted into objects rather than primitive types due to the return in the method. I am just wondering why I can't cast my primitive type of num as an Integer? Or maybe I am just ...

24. casting int to Byte

hi all, i have doubt regarding one question mentioned below: Given: byte b=3; byte c=7; Which of the following statements will not compile? 1. c=b+c; 2. c=+b; 3. c+=b; 4. c=-b; out of these 1,2 and 4 require explicit casting to byte, but 3 one is fine. i'm not able to understand why third one is fine, because c+=b means c ...

25. cast from string to integer

26. casting an int to a byte in the return statement

This question is from the self-test exam in K&B SCJP 1.5 Certification text. I have a question about code fragment E. The book says that code fragment E is not a correct answer (I agree with this). I think that there is an additional reason why code fragment E is not a valid answer. byte m2(short s) { return 42; } ...

28. Casting of int[] to Object[]

That would be technically impossible. If you create an array as int[], the ints are stored directly in the array. If you create it as Object[] or Integer[], the references are stored in the array and the object elsewhere (on the heap). So suppose you had: // Class A private Object[] getArray() { return new int[]{1, 2, 3, 4, 5}; // ...

29. Casting Integer to byte

Hi all, I am studying casting of Primitive types , but i cannot understand how can we cast an integer whose range is from 0-65535 into a byte whose rannge is 0-256 . We will never be able to cast integer values greater than 256 into bytes . However the book says we will only get a loss of precision . ...

30. casting from int to byte

31. Java 7 Int to String casting

getAttribute returns an instance of type Object. So before casting it directly, its good to check instance of or better you can invoke toString on that instance (but do check for null before invoking toString). There isn't any changes in Java 7 which would cause such a problem. And the exception you are seeing might be due to the fact the ...

32. Why is there (Integer) casting?

// Obtaining an array from an array list import java.util.*; class ArrayListToArray { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add(new Integer(1)); al.add(new Integer(2)); al.add(new Integer(5)); al.add(new Integer(9)); System.out.println("The contents of the array list are" + al); Object ia[] = al.toArray(); int sum = 0; for(int i = 0; i < ia.length; i++) sum += ((Integer)ia[i]).intValue(); System.out.println("The ...

33. Casting int[][] to int[]

34. casting an int

35. Re: casting double to int

36. Re: casting double to int

38. Casting string to int problem

Hi, I have retrieved a parameter from a form which is originally an int value but on retrieval you have to name it a string like: String NextBid = request.getParameter("nextBid"); I want to cast this to an int so i tried: int nextbid = Integer.parseInt(NextBid); but it gave the error: java.lang.NumberFormatException: For input string: "nextBid" .... Any ideas? Cheers

39. Casting from an object to an int

I'm not quite sure what your problem is. If you store objects of a class in the LinkedList you just store them and retrieve them and access whatever information they hold. If you're using generics you don't have to cast at all. If you want to store ints it's a special case. These will be automatically converted to their corresponding wrapper ...

40. casting from object to int issue

Hi Paul Thanks for the quick response... I'm having to rewrite this assignment from the ground up and have 88 hours to submit... so timely advice is most welcome. The vector is being populated from a database query... I'm pulling the contents of the query to populate my variables... if that makes sense.

42. how do you cast from color to int

i loaded an image into memory, when i move it on the screen with the mouse over other drawings, a certain color of the image must turn into another color and be half transparent. it works but i dont set pixel with the exact color i want, since the graphics program i use gives me the 3 rgb values of the ...

43. can not cast from object to int

Hi All, I have the following bunch of code Session session; session = getSessionFactory().openSession(); Query query; int MaxPageId = 0; List list; try{ query = session.createQuery("select max( from EmpBean emp"); list = query.list(); MaxPageId = (int) list.get(0); return MaxPageId; } catch(Exception e){ System.out.println(e.getMessage()); } return MaxPageId; The query executed successfully but when i get the max id in a integer variable ...

44. unchecked cast from Integer to T

45. Casting string to integer

46. int cast within an if statement

47. double to int casting

Hi, This cast is not working the way I expect: int y = (int)100000000000000000d; y is Integer.MAX_VALUE but: int y = (int)(long)100000000000000000d; y is 1569325056 as expected (The extra bytes were dropped) similarly: int y = (int)(short)100000000000000000d; y is -1 as expected (Again, the extra bytes were dropped) A similar problem happens with floats and ints. Why is the int cast ...

48. Cast mismatch from Object to Integer

Edit: Just to clarify, if you are reading from the user, whether they type "hello" or "14987" both of these are still Strings regardless of the fact that the user may see one as a string and the other as an integer. 1 is an integer '1' is a character with an ASCII value of 49 "1" is a string containing ...

49. casting STRING to an INTEGER

im having two strings "rajesh", "1222" How to check (with IF condition) whether the string contains a number r not. I have used the below check, but its throwing null exception. if(Integer.valueOf((String) obj) !=null) { Integer s = Integer.valueOf(obj.toString()); int temp = s.intValue()+30000; String errorCode =s.toString(temp)+" "; } kindly help me out.