There are eight primitive types in Java, of which seven are numbers:-
char. Yes "char" is a number, from 0 to 65535, even though it is usually shown as a character.
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?
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.
"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
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).
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 ]