• Post Reply Bookmark Topic Watch Topic
  • New Topic

objects  RSS feed

 
Rajesh Goud kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
what is the difference between primitive type declaration and object type declaration? and the use of object type declaration? please give me the answer
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are eight primitive types in Java, of which seven are numbers:-
  • long
  • int
  • short
  • byte
  • double
  • float, and
  • char. Yes "char" is a number, from 0 to 65535, even though it is usually shown as a character.
  • Also
  • boolean.
  • When you pass a primitive type as an argument to a method you pass its actual value.At the end of that imaginary snippet the bar method receives the value 123, which it can do whatever it likes with. There is no way of altering the original value of "foo."

    Anything which isn't one of those eight primitive types shown above, that you can give a name (identifier) to, is an object. Similar code (copy and paste from above, with a few changes).Now what you pass to the bar() method is still a number, but not "123;" it is a number which represents where to find a reference to "foo" (which is why objects are also called reference types). Now the "bar()" method can get its hands on a reference to "foo" and can manipulate the object which we have called "foo." It can for example change the 123 to 246.

    Where are you learning? Have you not been told about objects in your lectures or classes? Have you not found the Java tutorial which I gave a link to in the reply to your other question? Have you not got a simple book which introduces objects?
     
    Niall Loughnane
    Ranch Hand
    Posts: 209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the java String variable is also regarded as a Java primitive
     
    David McCombs
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not sure what you meant but String is NOT a primitive, nor regarded as such. It is a primitive type(char[]) wrapper class, but is not itself a primitive. It is no more a primitive then Integer or Byte is.
     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is a String really a wrapper class for a char[] array?

    Not convinced . . .
     
    David McCombs
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hmm...

    Great question and poor assumption on my part. Going to go look in the String source code.

    OK, I am not. I have the source on my machine at home, but not on my laptop and the connection speed where I am at is way too slow.
    [ March 08, 2007: Message edited by: David McCombs ]
     
    David McCombs
    Ranch Hand
    Posts: 212
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, it looks like I was at least partially correct, although I am not sure what this does.
    private static final ObjectStreamField[] serialPersistentFields =
    new ObjectStreamField[0];

    I couldn't find where this reference is used in String.

    But it does store the String as a char[].
     
    Niall Loughnane
    Ranch Hand
    Posts: 209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sound

    good point String isnt "technically" a primitive type but it is regarded as such

    see: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, String is neither a primitive type nor is it regarded as such. If anybody has told you that, they are mistaken.

    Strings do have special treatment by the compiler and JVM however. Almost the first thing it tells us about Strings in their API page is that they are immutable. There are no methods which can change a String. Look for them; they would have some sort of change as their title (identifier) and a void return type. All the "replace" methods have a String return type; they don't alter the String but produce a new String. This means that any Strings declared in the code as the same are compiled so as to represent multiple references to the same object.
    Try this:-The compiler can identify four of five String combinations before compiling and use those four as identifiers to a single String object. I think what you have been told is an oversimplification of this behaviour of Strings, whereby a String object can be shared (see String API page).
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16028
    87
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Niall Loughnane:
    sound

    good point String isnt "technically" a primitive type but it is regarded as such

    see: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

    Note the way this is worded on Sun's tutorial page:

    "The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such."

    It says: "You will probably think of String as if it is a primitive type". It does not say that everybody in general agrees that String is regarded as a primitive type, which is what you seem to suggest. It just says that you might think that String is a primitive type, while in fact it is not.
    [ March 09, 2007: Message edited by: Jesper Young ]
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!