Home Java Java-conversion Convert Object to Int
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Convert Object to Int

Advertisement
In this section, we are going to learn to convert a numeric string type Object into a primitive type int and Integer Object to primitive type int.

Convert Object to Int

     

In this section, we are going to learn to convert a numeric string type Object into a primitive type int and Integer Object to primitive type int. . 

Code Description:

The following program converts numeric string type object into primitive type int and Integer type object to primitive type int. The parseInt() method of an Integer class takes a numeric string object and returns the primitive type int value. Again, this program converts an Integer object into  a primitive type int using the intValue() method.

Here is the code of this program:

import java.io.*;
import java.lang.*;

public class ObjectToInt{
  public static void main(String[] args){
  //Numeric string to primitive int
  String Obj = "123";
  int i = Integer.parseInt(Obj);
  System.out.println("Numeric string to primitive int = " +i);
  //Integer object to primitive int
  Integer IntValue = 123;
  int intValue = IntValue.intValue();
  System.out.println("Integer object ot primitive int = "+intValue);
  }
}

Download this program:

Output of this program

C:\vinod>javac ObjectToInt.java

C:\vinod>java ObjectToInt
Numeric string to primitive int = 123
Integer object ot primitive int = 123

C:\vinod>
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 4, 2007

Related Tutorials

Ask Questions?    Discuss: Convert Object to Int   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
zeinab
June 3, 2011
question

int i = Integer.parseInt(Obj); how can i make this in c# not java ?please
Dinesh kumar jain
July 7, 2011
Java Question...

My question is.. import java.util.*; class DemoArrayList { public static void main(String args[]) { ArrayList<Object> a=new ArrayList<Object>(); a.add(10); a.add("+"); a.add(20); int ob=a.get(0); } } I am trying to convert a.get(0) (which is an object of Object class) to Object of Integer class.. reply soon please..
plop
December 22, 2011
THIS IS WRONG !

THIS IS WRONG !
Prakash
July 9, 2012
converting object to Integer in Java

How to convert the object (Example: 9.6K) to integer
Danny GutiƩrrez
August 20, 2012
Error

It is not object, is a simple string... bad example
stranger
November 17, 2012
This

OMFG dude, your subject is to convert from Object, not from string
DMCA.com