In the following:
public class toupper {
private static void upper(){
String a = "chad";
System.out.println("Upper Case is:" + a.toUpperCase());
}
public static void main (String[] args) {
upper();
}
}
Why isn't the line
String a = new String();
Necessary? I thought this line might be omitted because for whatever
reasons I was thinking toUpperCase() was a static method. It isn't.
Because when I change the line:
System.out.println("Upper Case is:" + a.toUpperCase());
to
System.out.println("Upper Case is:" + String.toUpperCase());
I get the following:
$javac toupper.java
toupper.java:6: non-static method toUpperCase() cannot be referenced
from a static context
System.out.println("Upper Case is:" + String.toUpperCase());
^
1 error
John W. Kennedy - 13 Jul 2006 03:32 GMT
> In the following:
>
[quoted text clipped - 16 lines]
>
> Necessary?
Because you don't need to make a new String; you want to use the
existing string "chad".

Signature
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Stefan Ram - 13 Jul 2006 03:37 GMT
>Why isn't the line
>String a = new String();
>Necessary?
The value of a string literal already is a reference to a
string object that represents the same text as the text
represented that is represented by the string literal.
>I thought this line might be omitted because for whatever
>reasons I was thinking toUpperCase() was a static method.
I does not have to do with this property directly.
>I get the following:
>toupper.java:6: non-static method toUpperCase() cannot be referenced
You also could have looked it up in the documentation:
http://download.java.net/jdk6/docs/api/java/lang/String.html
Stefan Ram - 13 Jul 2006 03:38 GMT
>Why isn't the line
>String a = new String();
>Necessary?
The value of a string literal already is a reference to a
string object that represents the same text as the text that
is represented by the string literal.
>I thought this line might be omitted because for whatever
>reasons I was thinking toUpperCase() was a static method.
It does not have to do with this property directly.
>I get the following:
>toupper.java:6: non-static method toUpperCase() cannot be referenced
You also could have looked it up in the documentation:
http://download.java.net/jdk6/docs/api/java/lang/String.html
saimatam at yahoo dot com - 13 Jul 2006 03:46 GMT
String is a basic data type in Java language.
Accordingly, the following statement initializes
variable 'a' with the String "fred".
String a = "fred";
Look at the following code fragment:
Line 1: String a = new String();
Line 2: a = "fred";
The String variable 'a' has been initialized
to a reference of empty String. In the next
line it is re-initialized to a reference of
a String 'fred'. So line # 1 is not necessary.
String is the class name and 'a' is an instance.
So String.toUppercase() refers to a static method
'toUpperCase' in the class 'String'.
If a method is not static it would need to know
'on which instance' it is supposed to act! So
if the method is not a static then it would need
to know its instance - in this case 'a' hence
a.toUpperCase();
Hope this helps.
-- Sai Matam.
Oliver Wong - 13 Jul 2006 17:18 GMT
[...]
> String a = "chad";
[...]
> Why isn't the line
> String a = new String();
>
> Necessary?
It's syntactic sugar. String is an object type, but there's shortcuts in
the language to treating them like pseudo-primitives. It's an inconsistency
in the Java language for the sake of developer convenience.
- Oliver
Luc The Perverse - 13 Jul 2006 19:57 GMT
> [...]
>> String a = "chad";
[quoted text clipped - 7 lines]
> in the language to treating them like pseudo-primitives. It's an
> inconsistency in the Java language for the sake of developer convenience.
Personally - I do actually find it very convenient.

Signature
LTP
for( Base i : allYourBase)
i.AreBelongToUs();
jmcgill - 13 Jul 2006 21:07 GMT
>>> String a = "chad";
>> [...]
>>> String a = new String();
> Personally - I do actually find it very convenient.
It does behave slightly differently, due to the way immutables are
interned. Probably of no real concern to anybody.