-4

Sorry for noobish question, but why we even use String, int and others, if we can just use Object as a type?

I mean use Object one; instead of String one;

lxknvlk
  • 2,744
  • 1
  • 27
  • 32
  • 8
    What can you *do* with `one` after you've declared it as type `Object`? Compare that with what you can do with it when you've declared it as type `String`... – Jon Skeet Jan 31 '14 at 15:59
  • 1
    i hope u have learned about Object oriented programming before asking this question. – Ashish Ratan Jan 31 '14 at 16:00
  • @GrijeshChauhan sorry, i am reading lots of tutorials about everything, and some things just arent so obvious to a noob like me. – lxknvlk Jan 31 '14 at 16:02
  • For the same reason that we use programming languages instead of writing hexadecimal values directly to memory. We've adapted to more human-readable abstractions over the underlying system to allow for easier creation and support of more complex applications. As an exercise, write a complete application using *only* `Object` as a type. See how the code looks. – David Jan 31 '14 at 16:03
  • @lxknvlk yes because tutorials are not book!! one should access net if there is trouble in understanding from book... – Grijesh Chauhan Jan 31 '14 at 16:06
  • 1
    You might want to look at this recent question: http://stackoverflow.com/questions/21270862/difference-between-object-creation-syntax, and especially my answer (plug, plug, plug :). – ajb Jan 31 '14 at 16:09
  • @lxknvlk Your other fall back when you don't know the answer is to write yourself a short code snippet and try it out. You will find out, when trying to call String methods on an Object reference, that you can't. – Radiodef Jan 31 '14 at 16:10

4 Answers4

2

For example, the String class declares a bunch of methods. If your variable is declared as Object, then only the methods declared in Object and its super classes (none) are visible on that variable, regardless of the type of the actual object.

You can do

String one = new String("whatever");
System.out.println(one.length());

but you can't do

Object one = new String("whatever");
System.out.println(one.length());

Methods and fields are resolved on the compile time type of the variable.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Because in some cases if you know, you will have a String, it's better to use:

String str = "foo";
int length = str.length();

instead of doing it with typecasting like this:

Object str = "foo";
try {
    int length = ((String)str).length();
} catch (ClassCastException e) {
    // and now what?
}

by not knowing if str is really a String!

The class Object is only providing the Objects methods. So you can't access for example this length() method on an Object.

So, everytime you know, that you will only able to handle a String, use a String!

If you are not sure what you will have, you have to use Object and do your special logic around (typecasting, error handling on a ClassCastException and so on...).

bobbel
  • 3,327
  • 2
  • 26
  • 43
0

Because Object is the lowest common denominator. It's also not type safe and you would have to cast to the specific type each time you wanted to do something specific to that class.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

Quite a few reasons really..

  • Try and use charAt on a String object, referred to by an Object type reference. If we only used Object type references, we would only be able to use Object methods, which aren't exactly useful.

  • Some methods require String types, and not Object types. For example, string.substring() takes one or two arguments of type int. This will not compile if you pass an Object in, unless you cast it. Which means, every time you want to use some native Java functionality that doesn't accept type Object, and requires a more specific reference type, you've got to cast it... which is most of the time.

  • So the code makes some bloody sense. Using these types means that you automatically know the kind of data that it will hold, just by looking at the variable type. String will hold some text. int holds an integer, double holds a large decimal. This all helps in making code more readable, and thus more extendable.

christopher
  • 26,815
  • 5
  • 55
  • 89