Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1053885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:19:17+00:00 2026-05-16T17:19:17+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:19:17+00:00Added an answer on May 16, 2026 at 5:19 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If C# can cast an int to an object, why not an int[] to
I'm trying to check if an object can cast to a certain type using
I have some code in Java as follows: private Object addVertex(String label, int posX,
How can I convert a List<Integer> to int[] in Java? I'm confused because List.toArray()
I'm new in java programming and of an Objects matrix that I can cast
Is there any way where i can cast a object to GetType() a object
Can we cast an object with user-defined types, like we do for normal data
How can I cast Long to BigDecimal ?
How can I cast a bigint variable to a string? DECLARE @id bigint
For example the following cast can be found littered throughout the MSDN documentation: (LPTSTR)&lpMsgBuf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.