How to assign an "Object variable" to an integer variable - java

I'm required to build a program in Java, while I have no idea about it.
I have an integer value stored in an object variable..
And I want to assign the value to another integer variable. but I can't find a way to convert an object to integer...
may you please help with this ..
thanks in advance ..
Here is my Code :
public class Bank extends unicastRemoteObject implements BankInterface
{
String[] columnNames = {"Account","Balance"};
Object[][] data = {{"a",10,},{"b",20}};
public Bank() throws RemoteException { //constructor
}
public int getRowCount() { // number of accounts
return data.length;
}
public Object getValueAt(int row, int col) { // returns the value of the cell
return data[row][col];
}
public void deposit(int account, int amount) throws RemoteException {
// work Description:
// find the tuple by searching about the account number
// add the amount to balance..
// conversions are needed
Object accnum; // to store the account number in. *string*
Object balancevalue; // to store the balance in. *integer*
for ( int i=0 ; i<=getRowCount() ; i++)
balancevalue = getValueAt(i,2)); // assign the current balance value..
accnum = getValueAt(i,1); // assign the account number..
int a = 0; // we will assign the integer type of accnum to a.
int b = 0; // we will assign the integer type of balancevalue to b.
if( a == account ) { // we find the account number.
b= b + amount ; // add the amount to the balance.
// we need to change the integer "b" into Object to store it in Data[i][2].
}
}

int to Object
int b = 10;
Object o = new Integer(b);
You can wrap your int into an Integer, which is a subclass of Object.
Object to int
Object o = new Integer(10);
int b = (Integer) o;
You can retrieve the value by casting o back to Integer. The JVM will then convert implicitely the Integer back to int.

Having something like Object[][] data is very bad design. Instead, you should have a class Account with fields accountNumber and balance with the approriate types. Then you have a List<Account>, everything is clean and typesafe, and the getValueAt() method becomes unneccessary.

Your data array already stores Object types - account number as String, balance as Integer (an Object wrapper for the primitive int). In your deposit method, you need to parse the account number String to an int value and get the primitive int value of the balance from the Integer.
Here is an example of how to make the conversions in the code you have shown.
for ( int i=0 ; i<=getRowCount() ; i++)
Integer balancevalue = (Integer)getValueAt(i,2));
String accnum = (String)getValueAt(i,1);
int a = Integer.parseInt(accnum); // parse String to int
int b = balancevalue.intValue(); // get primitive value of Integer wrapper
if( a == account ) { // we find the account number.
b= b + amount ; // add the amount to the balance.
// create new Integer (is an Object)
Integer newBalance = new Integer(b);
// store in the data array
data[row,2] = newBalance;
For the final step - conversion from int to Integer to store in the data array - you could rely on autoboxing instead of explicitly creating the Integer wrapper. So the last two lines of code could be replaced with this:
// store in the data array
data[row,2] = b;

You can use the Integer object which is a wrapper for an int primitive type.

Just do
Data[i][2] = b;
Auto boxing should work.
If you are using older version of java you can do
Data[i][2] = Integer.valueOf(b);

Related

Error in Creating Java array with Multiple Data Types

Can someone please explain why it doesn't work? The error is at obj[0][0]=1;. It says that GPA can't be converted to int, same thing for String variable assignment s.
public class GPA {
public String s;
public int n;
public GPA[][] a;
//constructor
public GPA(GPA[][] a){}
public static void main(String[] args) {
GPA[][] obj=new GPA[2][2];
obj[0][0]=1; //error here
}
}
obj is an Array of GPA objects.
obj[0] = 1 means you are assigning the first element of that array to an intvalue. It should be an object of type GPA.
You can do it like
obj[0] = new GPA("John Doe", 6);
I would also recommend using Java convention, by making variables private and set() them by public methods like setter()s.
The question is changed which makes the answer irrelevant.
It won't work and gives you compile time error because GPA is class type and you are trying to assigning int value to it.
You have two options.
Option 1:
GPA[] obj = new GPA[4];
obj[0] = new GPA();
obj[0].n = 1;
Option 2:
You can make members of GPA private and use setters to set the value. Below is example.
public class GPA {
private String s;
private int n;
private GPA[] a;
public GPA() {}
public GPA(GPA[] a) {}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public GPA[] getA() {
return a;
}
public void setA(GPA[] a) {
this.a = a;
}
}
and then set using setter.
obj[0].setN(1);
It's not good programming practice to make your members public. It is always advised to use setters.
What you're actualy doing is trying to assign int and/or string to variable that is expecting object of GPA class.
Didn't you want to do
obj[0].n=1;
obj[0].s="text;"
For array of object you always have to create on object at that position first. otherwise you alway get a NullPointerException.
So what you need goes something like this
GPA[][] obj = new GPA[2][2];
obj[0][0] = new GPA();
obj[0][0].s="text";
obj[0][0].n=1;
...
and so on for every position there is.
Java Arrays are homogeneous(Javascript arrays are heterogeneous). That means you can only store the type of elements which you used while creating an Array.
ex: `int intArray[];` //We can store only int type elements(it also accepts Integer etc.. types but java converts to int then store it)
Now, apply the same rule to public GPA[] a; here a is an array of type GPA. So it accept only GPA type object.
That mean, you can store values like as below
a[0] = new GPA("nameHere", 6);
If I want to store either a string or an int, one at a time( I have
to make table of Student Name vs GPA) ,how do I do it?
One solution to this requirement is, assign a variable using constructor or setter method.
GPA[] obj = new GPA[2];
obj[0] = new GPA("first", 6); // here you need to create a new constructor
or
obj[1] = new GPA(); // Here default constructor will work and you need to have setter methods
obj[1].setName("second");
Hope this help...

reading information from an array of objects filled with arrays of different data types

I've created an array of objects, with 5 different arrays primarily containing integers and strings (names of people and vehicle types). My question is once I've created an object array, how do I display the information pertaining to a selected index?
class Vehicle
{
int passengers, fuelCapacity, mpg;
String type, name;
Vehicle(String n, String t, int p, int f, int m)
{
passengers = p;
fuelCapacity = f;
mpg = m;
type = t;
name = n;
}
int range()
{
return mpg * fuelCapacity;
}
}
// Declaring Vehicles array for the pertinent information.
Vehicle[] iVehicles = new Vehicle[10];
// array consisting of the types of vehicles.
String[] types = {"Truck", "Sportscar", "SUV", "Sedan", "Coupe", "Truck","Truck", "Sportscar", "SUV", "Sedan"};
// array consisting of the number of passengers per vehicle.
int[] nmbrOfPassengers = {2,2,8,4,4,4,2,2,7,4};
// array consisting of each vehicles fuel capacity in gallons.
int[] fuelCapacity = {15,15,20,20,12,15,19,19,16,10};
//array consisting of integers containing each vehicles miles per gallon.
int[] milesPerGallon = {20,18,13,35,31,34,39,19,22,25};
// array consisting of the names
String[] aNames = { "brian","bob","fred","janet","claire","bill","rudy","sandy","samuel","joan"};
for (int i = 0; i < iVehicles.length; i++)
{
iVehicles[i] = new Vehicle(aNames[i], types[i], nmbrOfPassengers[i], fuelCapacity[i], milesPerGallon[i]);
}
// This is the portion i'm stumped on.
System.out.print(iVehicles[1]);
In order to display the information pertaining to a selected index the code is:
System.out.println(iVehicles[index])
However, System.out.println will automatically call toString method of class Vehicle. Unless you have override it, the default implementation (inherited from Object class) is useless for an user.
So, in order to display the information you should provide a custom implementation of this method, in which you concatenate in a String, the values from the Vehicle attributes that you want to display.
You can print like this:
System.out.print(iVehicles[1].passengers);
System.out.print(iVehicles[1].fuelCapacity);
System.out.print(iVehicles[1].mpg);
System.out.print(iVehicles[1].type);
System.out.print(iVehicles[1].name);
Or
in your class Vehicle override the toString() method:
public String toString() {
return ("Passengers: " + this.passengers); // Concatenate all other fields.
}
Now System.out.println(iVehicles[1]) will print what the toString() returns.

Can we assign values to objects passed in arguments without using return in Java?

This might be the silliest question anybody ever asked, but just for curiosity, Can we assign values to Objects which are passed as arguments. For example
public class ForDemo3 {
int i = 0;
ArrayList cp = new ArrayList();
public ForDemo3() {
int j = 30;
cp.add(j);
cp.add(i);
i = 5;
j = 12;
System.out.println("i = " + i + " & j= " + j);
assignDvalues(i, j);
System.out
.println("After Restoring the values i = " + i + " & j= " + j);
}
public void assignDvalues(Object... obj) {
for (int n = 0; n <= obj.length - 1; n++) {
obj[n] = cp.get(n);
System.out.println("" + obj[n]);
}
}
public static void main(String[] args) {
new ForDemo3();
}
}
I'm trying to develop checkpoint based recovery in java. And cp is a checkpoint object where values can be stored.
Output of Above is
i = 5 & j= 12
30
0
After Restoring the values i = 5 & j= 12
Edit
Apologies i am not a great user of stackoverflow. I'm working on a framework for parallel computing. And there will be a function savecheckpoint(List l) which will be used to store list or array list to DB as a checkpoint, and another functionpublic object restoreCheckPoint(int i) will restore the list. So far i'm able to save the checkpoint and then restore it back. But how can i assign back the values of the Objects or variables without effecting user's code? As given in the above example i want to restore the values using assignDvalues(). Is there any way to make it possible?
This will be a real hack, so use it wisely. Following method uses reflection and has two limitations. Can not handle primitive types (because of autoboxing) and can not handle Strings initialised as constants (i.e. String s = "some string" is not supported, but String s = new String("some string") works fine). Also you should use new Integer(X) instead of int x not to mess the primitive types (similar story to Strings)
In belows example I replaced the int i with Integer i and the same for j because as I said primitives are not supported.
The only thing which you would need to require by your users is to use "Integer" instead of "int", "Long" instead of "long" etc. and, what can be painful, avoid Strings defined as constants.
public class Demo2 {
Integer i = 0;
ArrayList cp = new ArrayList();
public Demo2() {
Integer j = 30;
String s = "def";
cp.add(j);
cp.add(i);
cp.add(s);
i = new Integer(5); // you need to use "i = new Integer(5)" instead of "i = 5" because you risk with messing the primitive int 5
j = new Integer(12); // here also you need to use new Integer(12)
s = new String("some string"); // NOTE: don't even try this with s ="some string" because this will lead to unexpected behaviour
//s = "some string" - if you pass string initialized this way you will mess up the string "some string" in jvm string pool
System.out.println("i = " + i + " & j= " + j + " & s= " + s);
assignDvalues(i, j, s);
System.out.println("After Restoring the values i = " + i + " & j= " + j + " & s= " + s);
}
public void assignDvalues(Object... obj) {
try {
for (int n = 0; n <= (obj.length - 1); n++) {
if (obj[n] instanceof Integer) {
Integer curInt = (Integer) obj[n];
Field field = curInt.getClass().getDeclaredField("value"); // Integer stores the real value in private field "value"
field.setAccessible(true);
field.set(curInt, cp.get(n));
System.out.println("" + obj[n]);
} else if (obj[n] instanceof String) {
String curS = (String) obj[n];
Field field = curS.getClass().getDeclaredField("value"); // String stores the text as char sequence in private field "value"
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); // need to reset final flag to override value of String
field.set(curS, ((String) cp.get(n)).toCharArray());
System.out.println("" + obj[n]);
} // else if (obj[n] insteanceOf SomeOtherType) { // you should provide implementation specific for each type/class you want to support with assignDvalues method
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static void main(String[] args) throws Exception {
new Demo2();
}
}
In this case, nothing detectable happens: the array of objects is created implicitly by the compiler, so you get no access to it. In addition, primitive ints are autoboxed into immutable Integers, so the changes made in the method cannot become visible to the caller through mutability of the objects themselves.
However, if you pass an array explicitly, the assignments are going to stay:
Object[] args = new Object[] {i, j};
assignDvalues(args);
System.out.println(args[0]+" "+args[1]);
Demo.
Java supports call by value. That means object references are passed as value when a function is called.
However, if you have a vector or ArrayList, then you can pass the vector or ArrayList object reference as an argument of a function, say f(ArrayList a) and within the function, you can insert items to the vector or arraylist just passed.
You can't change an object passed to a method to another object as far as the caller is concerned. Although you can change the state of passed objects.
In your example, your attempt at assigning a new value to the parameter array will have no effect on the calling code.
Not in this case.
Parameters in Java either have a primitive type or are references. They are passed by value, which means that the method has its own local copy of each parameter of a primitive type, or a local copy of each reference. If your method assigns to the parameter, the method modifies its own local copy, but the assignment doesn't affect anything the caller will see. However, if the method uses a reference to modify a field in another object, the caller will see a change in the referenced object; the method makes local copies of the reference, but not a local copy of the object itself.
An array is an object, so if you have an array parameter, a reference is passed; if the method uses that reference to modify the elements of the array, the caller will be able to see the change to the array element.
A vararg parameter is a special case. It is passed as an array, which is passed by reference. So if you write
public void assignSomething(Object... obj) {
obj[0] = "abcde";
}
If you call it like this:
Object[] objects = new Object[1];
assignSomething(objects);
System.out.println(objects[0]);
it will actually print "abcde". But if you pass individual arguments instead of an array:
Object obj1;
Object obj2;
assignSomething(obj1, obj2);
System.out.println(obj1);
The program creates a temporary array whose values are references to the objects you pass. The method will then replace the first reference in the temporary array (obj[0]) with a new one, but it will not use the previous value of the array element, which is the reference to obj1. So even though assigning an array element is normally a change that can be seen by the caller, in this case the element that gets assigned is part of a "hidden" array that nobody can see.
So, no, obj1 won't be affected--and in your example, i and j similarly won't be affected.
You can only achieve this by packing your object into another object. Assume you create generic wrapper class:
class ObjectHolder<T> {
private T obj;
private ObjectHolder(T obj) {
this.obj = obj;
}
public static ObjectHolder of(Object o) {
return new ObjectHolder(o);
}
public T getValue() {
return obj;
}
public void setValue(T value) {
obj = value;
}
#Override
public String toString() {
return obj.toString();
}
}
Now you can rewrite your program using that wrapper:
public class ForDemo3 {
ObjectHolder<Integer> i = ObjectHolder.of(0);
ArrayList cp = new ArrayList();
public ForDemo3() {
ObjectHolder<Integer> j = ObjectHolder.of(30);
cp.add(j.getValue());
cp.add(i.getValue());
i.setValue(5);
j.setValue(12);
System.out.println("i = " + i + " & j= " + j);
assignDvalues(i, j);
System.out.println("After Restoring the values i = " + i + " & j= " + j);
}
public void assignDvalues(ObjectHolder... obj) {
for (int n = 0; n <= (obj.length - 1); n++) {
obj[n].setValue(cp.get(n));
System.out.println("" + obj[n].getValue());
}
}
public static void main(String[] args) {
new ForDemo3();
}
}

cannot add one to variable pass by a function

I cannot add one to the integer on the function below, it still prints 5. Can anyone explain this?
public class HelloWorld {
public static void main(String[] args) {
int x = 5;
System.out.print('Hello world~~~~~');
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
System.out.println(Runtime.getRuntime().maxMemory());
System.out.println(Runtime.getRuntime().totalMemory());
System.out.println(Runtime.getRuntime().freeMemory());
OnePlusNumber(x);
System.out.println(x);
Date date = new Date();
System.out.println(date);
}
private static Integer OnePlusNumber(int number) {
number += 1;
return number;
}
}
you don't assign the value you return. change the following line:
OnePlusNumber(x);
to
x = OnePlusNumber(x);
Change
OnePlusNumber(x);
to
x=OnePlusNumber(x);
It will assign returned value from the method to x variable again, as it is a primitive data type (int).
If the passed parameter would have been an object of a class, you did not have to assign it like this. As same object's state gets changed when reference is passed to a method -
for ex-
Employee emp=new Employee();
emp.setName("A");
changeEmpName(emp);
public void changeEmpName(Employee employee){
employee.setName("B");
}
Then employees name becomes B.
This method will change original emp object , as it's reference was passed.
You have to change yout code:
OnePlusNumber(x);
Should be
x = OnePlusNumber(x);
So you have the return value.
And your method schould reurn an int:
private static int OnePlusNumber(int number){
The function OnePlusNumber(x); return a Integer.
Replace it with x = OnePlusNumber(x);
Java uses reference. When u pass x to OnePlusNumber. It passes reference of x, but in java , primitive types and string are immutable. X is Integer here.
so number+=1 will create new Integer. but X in original function , refers to old x.
That's why you need to assign return value.
x = OnePlusNumber(x);
http://en.wikipedia.org/wiki/Primitive_wrapper_class
http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
also read about
http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
This is the main difference between Java and C for example.In your case you don't send the exact item x you are jsut sending a "copy" of it to the function , so the result of the function doesn't modify x's adress just the local value of the copied object x.
The most common way of solving this is just assigning the result of the function to x
x=OnePlusNumber(x);

How to cast an Object to an int

How can I cast an Object to an int in java?
If you're sure that this object is an Integer :
int i = (Integer) object;
Or, starting from Java 7, you can equivalently write:
int i = (int) object;
Beware, it can throw a ClassCastException if your object isn't an Integer and a NullPointerException if your object is null.
This way you assume that your Object is an Integer (the wrapped int) and you unbox it into an int.
int is a primitive so it can't be stored as an Object, the only way is to have an int considered/boxed as an Integer then stored as an Object.
If your object is a String, then you can use the Integer.valueOf() method to convert it into a simple int :
int i = Integer.valueOf((String) object);
It can throw a NumberFormatException if your object isn't really a String with an integer as content.
Resources :
Oracle.com - Autoboxing
Oracle.com - Primitive Data types
On the same topic :
Java: What's the difference between autoboxing and casting?
Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);
Convert Object into primitive int
Scenario 1: simple case
If it's guaranteed that your object is an Integer, this is the simple way:
int x = (Integer)yourObject;
Scenario 2: any numerical object
In Java Integer, Long, BigInteger etc. all implement the Number interface which has a method named intValue. Any other custom types with a numerical aspect should also implement Number (for example: Age implements Number). So you can:
int x = ((Number)yourObject).intValue();
Scenario 3: parse numerical text
When you accept user input from command line (or text field etc.) you get it as a String. In this case you can use Integer.parseInt(String string):
String input = someBuffer.readLine();
int x = Integer.parseInt(input);
If you get input as Object, you can use (String)input, or, if it can have an other textual type, input.toString():
int x = Integer.parseInt(input.toString());
Scenario 4: identity hash
In Java there are no pointers. However Object has a pointer-like default implementation for hashCode(), which is directly available via System.identityHashCode(Object o). So you can:
int x = System.identityHashCode(yourObject);
Note that this is not a real pointer value. Objects' memory address can be changed by the JVM while their identity hashes are keeping. Also, two living objects can have the same identity hash.
You can also use object.hashCode(), but it can be type specific.
Scenario 5: unique index
In same cases you need a unique index for each object, like to auto incremented ID values in a database table (and unlike to identity hash which is not unique). A simple sample implementation for this:
class ObjectIndexer {
private int index = 0;
private Map<Object, Integer> map = new WeakHashMap<>();
// or:
// new WeakIdentityHashMap<>();
public int indexFor(Object object) {
if (map.containsKey(object)) {
return map.get(object);
} else {
index++;
map.put(object, index);
return index;
}
}
}
Usage:
ObjectIndexer indexer = new ObjectIndexer();
int x = indexer.indexFor(yourObject); // 1
int y = indexer.indexFor(new Object()); // 2
int z = indexer.indexFor(yourObject); // 1
Scenario 6: enum member
In Java enum members aren't integers but full featured objects (unlike C/C++, for example). Probably there is never a need to convert an enum object to int, however Java automatically associates an index number to each enum member. This index can be accessed via Enum.ordinal(), for example:
enum Foo { BAR, BAZ, QUX }
// ...
Object baz = Foo.BAZ;
int index = ((Enum)baz).ordinal(); // 1
Assuming the object is an Integer object, then you can do this:
int i = ((Integer) obj).intValue();
If the object isn't an Integer object, then you have to detect the type and convert it based on its type.
#Deprecated
public static int toInt(Object obj)
{
if (obj instanceof String)
{
return Integer.parseInt((String) obj);
} else if (obj instanceof Number)
{
return ((Number) obj).intValue();
} else
{
String toString = obj.toString();
if (toString.matches("-?\d+"))
{
return Integer.parseInt(toString);
}
throw new IllegalArgumentException("This Object doesn't represent an int");
}
}
As you can see, this isn't a very efficient way of doing it. You simply have to be sure of what kind of object you have. Then convert it to an int the right way.
You have to cast it to an Integer (int's wrapper class). You can then use Integer's intValue() method to obtain the inner int.
Answer:
int i = ( Integer ) yourObject;
If, your object is an integer already, it will run smoothly. ie:
Object yourObject = 1;
// cast here
or
Object yourObject = new Integer(1);
// cast here
etc.
If your object is anything else, you would need to convert it ( if possible ) to an int first:
String s = "1";
Object yourObject = Integer.parseInt(s);
// cast here
Or
String s = "1";
Object yourObject = Integer.valueOf( s );
// cast here
I use a one-liner when processing data from GSON:
int i = object != null ? Double.valueOf(object.toString()).intValue() : 0;
If the Object was originally been instantiated as an Integer, then you can downcast it to an int using the cast operator (Subtype).
Object object = new Integer(10);
int i = (Integer) object;
Note that this only works when you're using at least Java 1.5 with autoboxing feature, otherwise you have to declare i as Integer instead and then call intValue() on it.
But if it initially wasn't created as an Integer at all, then you can't downcast like that. It would result in a ClassCastException with the original classname in the message. If the object's toString() representation as obtained by String#valueOf() denotes a syntactically valid integer number (e.g. digits only, if necessary with a minus sign in front), then you can use Integer#valueOf() or new Integer() for this.
Object object = "10";
int i = Integer.valueOf(String.valueOf(object));
See also:
Inheritance and casting tutorial
int i = (Integer) object; //Type is Integer.
int i = Integer.parseInt((String)object); //Type is String.
Can't be done. An int is not an object, it's a primitive type. You can cast it to Integer, then get the int.
Integer i = (Integer) o; // throws ClassCastException if o.getClass() != Integer.class
int num = i; //Java 1.5 or higher
You can't. An int is not an Object.
Integer is an Object though, but I doubt that's what you mean.
If you mean cast a String to int, use Integer.valueOf("123").
You can't cast most other Objects to int though, because they wont have an int value. E.g. an XmlDocument has no int value.
I guess you're wondering why C or C++ lets you manipulate an object pointer like a number, but you can't manipulate an object reference in Java the same way.
Object references in Java aren't like pointers in C or C++... Pointers basically are integers and you can manipulate them like any other int. References are intentionally a more concrete abstraction and cannot be manipulated the way pointers can.
int[] getAdminIDList(String tableName, String attributeName, int value) throws SQLException {
ArrayList list = null;
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT admin_id FROM " + tableName + " WHERE " + attributeName + "='" + value + "'");
while (result.next()) {
list.add(result.getInt(1));
}
statement.close();
int id[] = new int[list.size()];
for (int i = 0; i < id.length; i++) {
try {
id[i] = ((Integer) list.get(i)).intValue();
} catch(NullPointerException ne) {
} catch(ClassCastException ch) {}
}
return id;
}
// enter code here
This code shows why ArrayList is important and why we use it. Simply casting int from Object. May be its helpful.
For Example Object variable; hastaId
Object hastaId = session.getAttribute("hastaID");
For Example Cast an Object to an int,hastaID
int hastaID=Integer.parseInt(String.valueOf(hastaId));
Refer This code:
public class sample
{
public static void main(String[] args)
{
Object obj=new Object();
int a=10,b=0;
obj=a;
b=(int)obj;
System.out.println("Object="+obj+"\nB="+b);
}
}
so divide1=me.getValue()/2;
int divide1 = (Integer) me.getValue()/2;
We could cast an object to Integer in Java using below code.
int value = Integer.parseInt(object.toString());
If you want to convert string-object into integer...
you can simply pass as:
int id = Integer.valueOf((String) object_name);
Hope this will be helpful :-)
Integer x = 11
int y = x.intValue();
System.out.println("int value"+ y);
Finally, the best implementation for your specification was found.
public int tellMyNumber(Object any) {
return 42;
}
first check with instanceof keyword . if true then cast it.

Categories