Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #21058 > unrolled thread

Re: "Hello world!" without a public class?

Started byKevin McMurtrie <mcmurtrie@pixelmemory.us>
First post2013-01-06 11:37 -0800
Last post2013-01-06 21:35 -0500
Articles 4 — 2 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: "Hello world!" without a public class? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-06 11:37 -0800
    Re: "Hello world!" without a public class? Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 15:19 -0500
      Re: "Hello world!" without a public class? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-01-06 15:11 -0800
        Re: "Hello world!" without a public class? Arne Vajhøj <arne@vajhoej.dk> - 2013-01-06 21:35 -0500

#21058 — Re: "Hello world!" without a public class?

FromKevin McMurtrie <mcmurtrie@pixelmemory.us>
Date2013-01-06 11:37 -0800
SubjectRe: "Hello world!" without a public class?
Message-ID<50e9d27c$0$80180$742ec2ed@news.sonic.net>
In article <public-20130106091348@ram.dialup.fu-berlin.de>,
 ram@zedat.fu-berlin.de (Stefan Ram) wrote:

> »class HelloWorldApp {
>     public static void main(String[] args) {
>         System.out.println("Hello World!"); // Display the string.
>     }
> }
> «
> 
> http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
> 
>   There is no »public« in front of »class« in Oracles Tutorial!
> 
>   What should I teach in my classes?
> 
>     1.) »public class HelloWorldApp« (because this is most common IIRC)
> 
>     2.) »class HelloWorldApp« (because this is in Oracles tutorial)
> 
>     3.) »final class HelloWorldApp« (because this class is not designed
>         for inheritance and Bloch says that one should not inherit from
>         it in this case and the students can as well get used to this
>         right from the start)
> 
>     4.) »public final class HelloWorldApp« (combination of »1.)« and »3.)«)

'final' classes are useful for:

1) Safety.  Not all classes can be subclassed safely.  For example, a 
subclass of Thread that starts itself from the constructor may be 
executing the run() method before a subclass' constructor completes.  
Another case would be data for which the equals() method can not be 
generalized.  Marking the class as final forces compile-time safety 
against accidentally subclassing something that isn't ready for it.

2) Security.  Imagine the damage you could do if String was mutable.  
You wouldn't want a credit card encryptor/decryptor to gain any extra 
features because a mistake could cost millions of dollars.

3) Performance.  Methods known to have a single implementation can be 
highly optimized by the JIT.  This optimization must be removed as soon 
as it's possible for there to be more than one implementation. This is 
an awful side-effect that's difficult to trace. Classes requiring 
maximum performance should be guarded from subclassing.


So for "Hello World" the 'final' modifier isn't much use.  You could 
argue that demonstration code should not be subclassed (#1 above), but 
maybe somebody wants to make another demo.
-- 
I will not see posts from Google because I must filter them as spam

[toc] | [next] | [standalone]


#21069

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-01-06 15:19 -0500
Message-ID<50e9dc69$0$284$14726298@news.sunsite.dk>
In reply to#21058
On 1/6/2013 2:37 PM, Kevin McMurtrie wrote:
> In article <public-20130106091348@ram.dialup.fu-berlin.de>,
>   ram@zedat.fu-berlin.de (Stefan Ram) wrote:
>
>> »class HelloWorldApp {
>>      public static void main(String[] args) {
>>          System.out.println("Hello World!"); // Display the string.
>>      }
>> }
>> «
>>
>> http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
>>
>>    There is no »public« in front of »class« in Oracles Tutorial!
>>
>>    What should I teach in my classes?
>>
>>      1.) »public class HelloWorldApp« (because this is most common IIRC)
>>
>>      2.) »class HelloWorldApp« (because this is in Oracles tutorial)
>>
>>      3.) »final class HelloWorldApp« (because this class is not designed
>>          for inheritance and Bloch says that one should not inherit from
>>          it in this case and the students can as well get used to this
>>          right from the start)
>>
>>      4.) »public final class HelloWorldApp« (combination of »1.)« and »3.)«)
>
> 'final' classes are useful for:
>
> 1) Safety.  Not all classes can be subclassed safely.  For example, a
> subclass of Thread that starts itself from the constructor may be
> executing the run() method before a subclass' constructor completes.
> Another case would be data for which the equals() method can not be
> generalized.  Marking the class as final forces compile-time safety
> against accidentally subclassing something that isn't ready for it.

Yes.

> 2) Security.  Imagine the damage you could do if String was mutable.
> You wouldn't want a credit card encryptor/decryptor to gain any extra
> features because a mistake could cost millions of dollars.

Yes.

> 3) Performance.  Methods known to have a single implementation can be
> highly optimized by the JIT.  This optimization must be removed as soon
> as it's possible for there to be more than one implementation. This is
> an awful side-effect that's difficult to trace. Classes requiring
> maximum performance should be guarded from subclassing.

One can find that claim a gazillion places on the internet from
people you never heard about.

And then one can find:

http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html

<quote>
The common perception is that declaring classes or methods final makes 
it easier for the compiler to inline method calls, but this perception 
is incorrect (or at the very least, greatly overstated).
</quote>

by Brian Goetz.

Arne



[toc] | [prev] | [next] | [standalone]


#21080

FromKevin McMurtrie <mcmurtrie@pixelmemory.us>
Date2013-01-06 15:11 -0800
Message-ID<50ea0485$0$80103$742ec2ed@news.sonic.net>
In reply to#21069
In article <50e9dc69$0$284$14726298@news.sunsite.dk>,
 Arne Vajhøj <arne@vajhoej.dk> wrote:

> On 1/6/2013 2:37 PM, Kevin McMurtrie wrote:
> > In article <public-20130106091348@ram.dialup.fu-berlin.de>,
> >   ram@zedat.fu-berlin.de (Stefan Ram) wrote:
> >
> >> »class HelloWorldApp {
> >>      public static void main(String[] args) {
> >>          System.out.println("Hello World!"); // Display the string.
> >>      }
> >> }
> >> «
> >>
> >> http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
> >>
> >>    There is no »public« in front of »class« in Oracles Tutorial!
> >>
> >>    What should I teach in my classes?
> >>
> >>      1.) »public class HelloWorldApp« (because this is most common IIRC)
> >>
> >>      2.) »class HelloWorldApp« (because this is in Oracles tutorial)
> >>
> >>      3.) »final class HelloWorldApp« (because this class is not designed
> >>          for inheritance and Bloch says that one should not inherit from
> >>          it in this case and the students can as well get used to this
> >>          right from the start)
> >>
> >>      4.) »public final class HelloWorldApp« (combination of »1.)« and 
> >>      »3.)«)
> >
> > 'final' classes are useful for:
> >
> > 1) Safety.  Not all classes can be subclassed safely.  For example, a
> > subclass of Thread that starts itself from the constructor may be
> > executing the run() method before a subclass' constructor completes.
> > Another case would be data for which the equals() method can not be
> > generalized.  Marking the class as final forces compile-time safety
> > against accidentally subclassing something that isn't ready for it.
> 
> Yes.
> 
> > 2) Security.  Imagine the damage you could do if String was mutable.
> > You wouldn't want a credit card encryptor/decryptor to gain any extra
> > features because a mistake could cost millions of dollars.
> 
> Yes.
> 
> > 3) Performance.  Methods known to have a single implementation can be
> > highly optimized by the JIT.  This optimization must be removed as soon
> > as it's possible for there to be more than one implementation. This is
> > an awful side-effect that's difficult to trace. Classes requiring
> > maximum performance should be guarded from subclassing.
> 
> One can find that claim a gazillion places on the internet from
> people you never heard about.
> 
> And then one can find:
> 
> http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html
> 
> <quote>
> The common perception is that declaring classes or methods final makes 
> it easier for the compiler to inline method calls, but this perception 
> is incorrect (or at the very least, greatly overstated).
> </quote>
> 
> by Brian Goetz.
> 
> Arne

The IBM article addresses a misconception about bytecode compilation.  
Before high performance JITs, like Sun's HotSpot, compilers did make 
extremely unsafe global optimizations based on the 'final' keyword.  
That was nothing but trouble.

Making a class final and declaring instances of the class with that type 
makes sure that optimizations are available.

abstract class Bitmap
{
   abstract int getRGBA(int x, int y);
}


final class RGBA32Bitmap extends Bitmap
{
   int getRGBA(int x, int y) {...}
}

Calling getRGBA() on a reference defined as class Bitmap will have 
varying optimization during runtime depending on whether or not the 
implementation can be guaranteed at a specific time.  Sun's HotSpot may 
add and remove optimizations as condition change.  Calling getRGBA() on 
a reference defined as class RGBA32Bitmap will be highly optimized at 
runtime.

Of course I wouldn't bother with any of this unless it's needed.
-- 
I will not see posts from Google because I must filter them as spam

[toc] | [prev] | [next] | [standalone]


#21118

FromArne Vajhøj <arne@vajhoej.dk>
Date2013-01-06 21:35 -0500
Message-ID<50ea3464$0$282$14726298@news.sunsite.dk>
In reply to#21080
On 1/6/2013 6:11 PM, Kevin McMurtrie wrote:
> In article <50e9dc69$0$284$14726298@news.sunsite.dk>,
>   Arne Vajhøj <arne@vajhoej.dk> wrote:
>
>> On 1/6/2013 2:37 PM, Kevin McMurtrie wrote:
>>> In article <public-20130106091348@ram.dialup.fu-berlin.de>,
>>>    ram@zedat.fu-berlin.de (Stefan Ram) wrote:
>>>
>>>> »class HelloWorldApp {
>>>>       public static void main(String[] args) {
>>>>           System.out.println("Hello World!"); // Display the string.
>>>>       }
>>>> }
>>>> «
>>>>
>>>> http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
>>>>
>>>>     There is no »public« in front of »class« in Oracles Tutorial!
>>>>
>>>>     What should I teach in my classes?
>>>>
>>>>       1.) »public class HelloWorldApp« (because this is most common IIRC)
>>>>
>>>>       2.) »class HelloWorldApp« (because this is in Oracles tutorial)
>>>>
>>>>       3.) »final class HelloWorldApp« (because this class is not designed
>>>>           for inheritance and Bloch says that one should not inherit from
>>>>           it in this case and the students can as well get used to this
>>>>           right from the start)
>>>>
>>>>       4.) »public final class HelloWorldApp« (combination of »1.)« and
>>>>       »3.)«)
>>>
>>> 'final' classes are useful for:
>>>
>>> 1) Safety.  Not all classes can be subclassed safely.  For example, a
>>> subclass of Thread that starts itself from the constructor may be
>>> executing the run() method before a subclass' constructor completes.
>>> Another case would be data for which the equals() method can not be
>>> generalized.  Marking the class as final forces compile-time safety
>>> against accidentally subclassing something that isn't ready for it.
>>
>> Yes.
>>
>>> 2) Security.  Imagine the damage you could do if String was mutable.
>>> You wouldn't want a credit card encryptor/decryptor to gain any extra
>>> features because a mistake could cost millions of dollars.
>>
>> Yes.
>>
>>> 3) Performance.  Methods known to have a single implementation can be
>>> highly optimized by the JIT.  This optimization must be removed as soon
>>> as it's possible for there to be more than one implementation. This is
>>> an awful side-effect that's difficult to trace. Classes requiring
>>> maximum performance should be guarded from subclassing.
>>
>> One can find that claim a gazillion places on the internet from
>> people you never heard about.
>>
>> And then one can find:
>>
>> http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html
>>
>> <quote>
>> The common perception is that declaring classes or methods final makes
>> it easier for the compiler to inline method calls, but this perception
>> is incorrect (or at the very least, greatly overstated).
>> </quote>
>>
>> by Brian Goetz.
>
> The IBM article addresses a misconception about bytecode compilation.
> Before high performance JITs, like Sun's HotSpot, compilers did make
> extremely unsafe global optimizations based on the 'final' keyword.
> That was nothing but trouble.
>
> Making a class final and declaring instances of the class with that type
> makes sure that optimizations are available.
>
> abstract class Bitmap
> {
>     abstract int getRGBA(int x, int y);
> }
>
>
> final class RGBA32Bitmap extends Bitmap
> {
>     int getRGBA(int x, int y) {...}
> }
>
> Calling getRGBA() on a reference defined as class Bitmap will have
> varying optimization during runtime depending on whether or not the
> implementation can be guaranteed at a specific time.  Sun's HotSpot may
> add and remove optimizations as condition change.  Calling getRGBA() on
> a reference defined as class RGBA32Bitmap will be highly optimized at
> runtime.
>
> Of course I wouldn't bother with any of this unless it's needed.

You may be disappointed if you ever decide that you need to
do it.

I would believe Brian Goetz over all the random statements
on the internet.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web