Why isn't new used in the following.  
Author Message
grocery_stocker





PostPosted: 2006-7-13 10:10:00 Top

java-programmer, Why isn't new used in the following. 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





PostPosted: 2006-7-13 10:32:00 Top

java-programmer >> Why isn't new used in the following. grocery_stocker wrote:
> 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?

Because you don't need to make a new String; you want to use the
existing string "chad".

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
 
ram





PostPosted: 2006-7-13 10:37:00 Top

java-programmer >> Why isn't new used in the following. "grocery_stocker" <email***@***.com> writes:
>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

 
 
ram





PostPosted: 2006-7-13 10:39:00 Top

java-programmer >> Why isn't new used in the following. "grocery_stocker" <email***@***.com> writes:
>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





PostPosted: 2006-7-13 10:46:00 Top

java-programmer >> Why isn't new used in the following. 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





PostPosted: 2006-7-14 0:18:00 Top

java-programmer >> Why isn't new used in the following. "grocery_stocker" <email***@***.com> wrote in message
news:email***@***.com...
[...]
> 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





PostPosted: 2006-7-14 2:57:00 Top

java-programmer >> Why isn't new used in the following. "Oliver Wong" <email***@***.com> wrote in message
news:8nutg.135132$S61.44224@edtnps90...
> "grocery_stocker" <email***@***.com> wrote in message
> news:email***@***.com...
> [...]
>> 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.


Personally - I do actually find it very convenient.

--
LTP

for( Base i : allYourBase)
i.AreBelongToUs();


 
 
jmcgill





PostPosted: 2006-7-14 4:08:00 Top

java-programmer >> Why isn't new used in the following. Luc The Perverse wrote:

>>> 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.