thejartender
thejartender

Reputation: 9379

Java - When a String is not a literal but an Object?

Yet again I am revisiting Java after yet another setback in my brain (see my profile). Similar questions have been asked here, but none in the fashion I wish to present this question.

I have always understood what a String is. i.e, an An object as is clearly expressed in the official API. However I have been reading this Java guide on data types.

To me the concept is clear. Primitives are literals. i.e, they can not be instantiated and are assigned values as follows:

primtype prim =?;

The official guides on Java all seem to (correctly) identify a String as a literal, but leave out what the criteria are to be regarded as a literal. Given that java.lang.Stringit is indeed a java.lang.Obect (through extension), does this underlying difference between a String being an Object as apposed to a literal lie merely in how the variable is assigned the value - by mere assignment (literal) as apposed to construction (Object)? Please feel free to point me to more in-depth resources if you can to help clarify?

I would also like to know if the criteria for literals are consistent across languages (if this implies that "weakly typed" languages only use literals?)

Upvotes: 3

Views: 1958

Answers (5)

James Gaunt
James Gaunt

Reputation: 14783

I think you're confusing primitive and literal.

A type can be a primitive type, or an Object type (i.e. inheriting from Object).

A literal just means a representation of value 'literally' in the code. This could represent a primitive or an Object type.

So 'literal type' doesn't really make sense. A string is never a literal, it is always an Object. The literal is just one possible way of representing a string in code.

The treatment in other languages varies widely. For example in C# everything derives from Object, including types which are 'primitive' in Java.

Upvotes: 8

Ingo
Ingo

Reputation: 36339

You should not confuse different, non-exclusive concepts. "literal" is a lexical/syntactical concept for expressing certain constant items in a programming language. Many literals in Java describe primitive values, however, in different languages without primitives, the same syntax could be used to describe constant items of different quality.

Different literals describe more complex items, such as strings or arrays, or even functions (see the upcoming lambda-syntax - lambda expressions are widely seen as "function literals").

Upvotes: 3

tckmn
tckmn

Reputation: 59283

It is only called a literal because you can do this:

"this is a string"

Instead of having to do something like this:

new String(new char[] {'t', 'h', 'i', 's' ...})

A literal is, for example, 5, or 9.3f, or 'd'. Therefore, a String (constructed like "this") would fall into that category.

An example of a non-literal would be new Object();. If you make a String with the new keyword, it's not a literal.

However, once the String is made, it doesn't matter at all. They are still the same string, no matter if you make it like "this" or new String(new char[]{'t','h','i','s'}).

Upvotes: 6

ManMohan Vyas
ManMohan Vyas

Reputation: 4062

This is merely the matter of choice of how you distinguish it. By defination as it extends Object it is a Object, but java on the other hand allow you to simply use

String str = "this is a string"

may be for matter of simplicity. Now it is upto you what you want to call it, till you now what is happening underneath

Upvotes: 0

Nomesh Gajare
Nomesh Gajare

Reputation: 855

string is a a object and not a literal because it can be constructed using array of characters!

Upvotes: 2

Related Questions