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 6090497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:13:14+00:00 2026-05-23T12:13:14+00:00

I am reading Thinking in java , Generics. In one example(at paragraph The action

  • 0

I am reading Thinking in java , Generics. In one example(at paragraph “The action at the boundaries”)

public class GenericHolder<T> {
    private T obj;
    public void set(T obj) { this.obj = obj; }
    public T get() { return obj; }
    public static void main(String[] args) {
        GenericHolder<String> holder =
        new GenericHolder<String>();
        holder.set("Item");
        String s = holder.get();
    }
} ///:~


public void set(java.lang.Object);
    0: aload_0
    1: aload_1
    2: putfield #2; //Field obj:Object;
    5: return
public java.lang.Object get();
    0: aload_0
    1: getfield #2; //Field obj:Object;
    4: areturn
public static void main(java.lang.String[]);
    470 Thinking in Java Bruce Eckel
    0: new #3; //class GenericHolder
    3: dup
    4: invokespecial #4; //Method "<init>":()V
    7: astore_1
    8: aload_1
    9: ldc #5; //String Item
    11: invokevirtual #6; //Method set:(Object;)V
    14: aload_1
    15: invokevirtual #7; //Method get:()Object;
    18: checkcast #8; //class java/lang/String
    21: astore_2
    22: return

According to the disassembled code line 18, the compiler adds the checkcast code. I want to know if it is always to such a checkcast for generic. I replaced the String to Integer and tried again, but I didn’t find the checkcast code at last. So does the Object type.

Could someone explain that? Is String in Java is a special 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-23T12:13:15+00:00Added an answer on May 23, 2026 at 12:13 pm

    I replace the String to Integer and tried again, but I didn’t find the checkcast code at last.

    For me it produces a checkcast also for Integer:

      0: new    #3; //class GenericHolder
      3: dup
      4: invokespecial  #4; //Method GenericHolder."<init>":()V
      7: astore_1
      8: aload_1
      9: iconst_5
     10: invokestatic   #5; //Method java/lang/Integer.valueOf:(I)
     13: invokevirtual  #6; //Method GenericHolder.set:(Ljava/lang/Object;)V
     16: aload_1
     17: invokevirtual  #7; //Method GenericHolder.get:()Ljava/lang/Object;
     20: checkcast  #8; //class java/lang/Integer
     23: astore_2
     24: return
    

    As you can see, the get method has the signature Ljava/lang/Object; i.e., the checkcast is to make sure that the Object returned is indeed an Integer.


    Edit: In the code you posted as a comment:

    0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
    3: new #3; //class b/TestG
    6: dup
    7: new #4; //class java/lang/Integer
    10: dup
    11: iconst_2
    12: invokespecial #5; //Method java/lang/Integer."<init>":(I)V
    15: invokespecial #6; //Method b/TestG."<init>":(Ljava/lang/Object;)V
    18: invokevirtual #7; //Method b/TestG.getT:()Ljava/lang/Object;
    21: invokevirtual #8; //Method java/io/PrintStream.print:(Ljava/lang/Objec t;)V 
    24: return
    

    you pass on the returned value to PrintStream.print which accepts an Object. Thus, there is no need to cast the returned value. (checkcast java.lang.Object would always go through!)

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

Sidebar

Related Questions

I've been reading Bruce Eckel's Thinking In Java and in the chapter on generics,
I'm just reading Code Complete by Steve McConell and I'm thinking of an Example
Reading through Thinking in Java i stuck in ex:6 of Inner Classes chapter. Exercise
I'm reading Bruce Eckel's Thinking in Java and there's an exercise I'm just not
I am reading Thinking in Java 4th Edition . There described a strange workaround
I've just finished reading the chapter of 'Thinking in Java' concerning type information and
I'm reading Thinking in C++ by Bruce Eckel. In Chapter 15 (Volume 1) under
I'm reading Thinking in C++ (vol. 2): Whenever a function is called, information about
Reading this blog post about HttpOnly cookies made me start thinking, is it possible
I am thinking of using sed for reading .properties file, but was wondering if

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.