Problem1: I have a doubt that if we can only create objects of class then how are we able to create objects of primitive data types such as int,char etc.
Problem2: Now suppose Ankit is a class and if I write
Ankit a=new Ankit();
System.out.println(a.getClass().getName());
it will give me class name of a. Also if I write
System.out.println(Ankit.class);
Then too it will give class name.But If I write
int ar[]=new int[10];
System.out.println(ar.getClass().getName());
System.out.println(int.class);
then I get output as:
[I and int
Why so? Here also I should get same output as class name of int ar then why different outputs and what is [I?
Your second snippet does not do the same as your first snippet. Instead, you're printing
You should change the last line of your snippet to:
System.out.println(int[].class);
to make it print the same thing as the line above.
About the second part of your question: that's just how java represents class name for arrays.
int
is [I
int
is [[I
Ankit
is [LAnkit;
(which you can observe with System.out.println(Ankit[].class);
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With