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

  • Home
  • SEARCH
  • 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 3339488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:29:53+00:00 2026-05-18T00:29:53+00:00

Just trying to understand the behavior of java.lang.Object class. public class TypeCheck{ static void

  • 0

Just trying to understand the behavior of java.lang.Object class.

public class TypeCheck{
    static void printMethod(Object obj)
    {
        System.out.println(obj);
    }

    public static void main(String[] args)
    {
        Object obj       = new Object();
        Integer intObj   = new Integer(12);
        String stringObj = new String("Hello");

        printMethod(obj); //---> java.lang.Object@78b5f53a
        printMethod(intObj); //---> 12
        printMethod(stringObj); // ---> Hello
    }
}

My questions are:

  1. When should we use Object class?
  2. But, when I only pass printMethod(intObj) and in printMethod(Object obj) I do an addition: System.out.println(obj+1) it does not work. If obj recognizes that it’s an Integer, why can’t I do operations on it?
    Exception: TypeCheck.java:5: operator + cannot be applied to java.lang.Object,int System.out.println(obj+1);
  3. Now, if I do this:

    public class TypeCheck
    {
        static void printMethod(Object obj)
        {
            System.out.println(obj.getClass().getName());
        }
        public static void main(String[] args)
        {
            int i = 666;
            printMethod(i);
        }
    }
    

    It returns java.lang.Integer, but it’s defined as int (primitive type). Why does Java convert a primitive type to it’s wrapper class when passed to an Object.

  • 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-18T00:29:53+00:00Added an answer on May 18, 2026 at 12:29 am
    1. Almost never. You should use as specific an object you can.
    2. That should work. See below
    3. Because primitives, i.e. int, are not objects. Therefore it needs to be made an object (boxed) to be passed as one.

    EDIT in response to question edit:

    (Number 2)
    Inside of printMethod, the type of obj is not int, it is Object. You can’t add objects! You would need to cast it back to Integer or int before you could perform such operations on it. What would the + do if you passed an instance of TypeCheck into TypeCheck.printMethod?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.lang.reflect.Array; public class PrimitiveArrayGeneric { static <T> T[] genericArrayNewInstance(Class<T> componentType) { return (T[])
I'm trying to understand the exact behavior of storage class specifiers in C99, and
im just starting to learn flex and im trying to understand how Flex does
I'm just learning ruby and trying to understand the scope of code executed in
I just joined a new C++ software project and I'm trying to understand the
I'm trying to understand screen vs. print behavior. What's the reason the APSW docs
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my irb sessions to actually list the current line of
Just trying to get up to speed with the SDK... So, I've created my
Just trying to get my head round Spring and figuring out how I wire

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.