user3318210
user3318210

Reputation: 11

Declaration of String type

While working with my project I've seen a declaration of a string variable like following :

private String compoundSourceColumnStr = "<compoundSourceColumnList>";

Can anyone please tell me what is actually being defined here?

Upvotes: 1

Views: 117

Answers (5)

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

It is a string which would hold a character array. The value is provided on the right side.

It actually is a identifier for a value. The value's datatype is string and the permission level is private which means it won't be accessible outside of its function. You can use public if you want to use it in other functions too. Such as:

public String id_name = "<value>";

Also note, <> is not required. It is just the part of the value.

Upvotes: 0

TheBrenny
TheBrenny

Reputation: 527

As with byte, int, float etc. these are all called primitive types. A String is sort of like a primitive type in the sense that you do not need to say String abc = new String("xyz");. It's actual name is a String Literal. Since Strings are used so much in Java programming, it's just a convenience method to use String abc = "xyz". The two are exactly the same when compiled, however one is arguably easier to read, but even then, there is still hardly any difference.

Upvotes: 1

Sathiyaraj
Sathiyaraj

Reputation: 343

In this case, "" is a private string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a private String object with its value.

Upvotes: 1

Maroun
Maroun

Reputation: 95948

private String compoundSourceColumnStr = "";

Since default value of objects in Java is null (see JLS), and since the programmer doesn't want that to be the case, he's giving the String initial value of empty string - "".

Upvotes: 0

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32458

An empty String literal "" will be initialized to that variable. "" is called empty String literal.

And you can use following way to check whether a variable having empty string as value or not.

compoundSourceColumnStr.isEmpty()

Upvotes: 3

Related Questions