• Post Reply Bookmark Topic Watch Topic
  • New Topic

small, simple, efficient, cost effective and memory effective java code to print COGN  RSS feed

 
Ram Hebbale Hiriyanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the codes look like this:
1: class Cog{
public static void main(String []a){
System.out.println("COGNIZANT");
}
}
2: class Cog{
static{
System.out.println("COGNIZANT");
}
public static void main(String []a){}
}
is 1 more efficient in terms of memory than 2? if so, why?
is there a more efficient way to print COGNIZANT than the two mentioned here?
 
Manoj Kumkumath
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.
Memory calculation
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most "small, simple, efficient, cost effective and memory effective" way to write a program which prints a String and does nothing more looks like this:

echo "COGNIZANT"

(I used a shell built-in; I didn't use Java at all.)

But seriously -- maybe if you told us why you were asking this (very strange) question, we could help you with a better answer. Note that a static initialization block compiles into a method named "<clinit>". There is actually, therefore, very little difference between these two classes. If you ran the one with the sttic initializer, it'd print the message, then you'd get an exception regarding the missing "main" method, making the first one far more expensive computationally than the "normal" one.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you worrying about the efficiency differences between these two? Any differences between these two are irrelevant in a real world application.

Anyway, if you really want to know... I saved both examples as Cog1.java and Cog2.java, compiled them and disassembled them using javap (the Java disassembler which is included with the JDK). This is what I got:

And for the second example:

You see that in the first example, there's a constructor (which is not executed) and a main method, which consists of four byte code instructions. So the whole program runs in 4 instructions (getstatic, ldc, invokevirtual, return).

In the second example, there's a constructor (not executed), an main method with only one instruction (return) and a static initializer block with four instructions (exactly the same four as in the main method of the first example). So when you run this, it runs exactly the same four instructions as in the first example, plus an extra 'return' instruction (in the main method).
[ January 31, 2006: Message edited by: Jesper de Jong ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that the static initializer is far less efficient, because any time another programmer sees it you will have to spend time explaining to them why you felt it was necessary to write such silly code. Seriously, the only important differences I see are:
  • As EFH noted, the lack of a main() method means you can't use the class as the starting point of a java invocation.
  • The static initializer block can only execute once in a given JVM invocation (unless you use multiple classloaders and cause the class to reload), while the main method can execute as many times as you choose to call it.
  • The static initializer block is more likely to be misunderstood by junior programmers.

  • [ January 31, 2006: Message edited by: Jim Yingst ]
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What is this about "lack of main() method"? Perhaps I'm just delusional, but I see main() in both examples from the OP.

    Layne
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oops, right. What can I say - I foolishly assumed EFH knew what he was talking about.
     
    Ernest Friedman-Hill
    author and iconoclast
    Sheriff
    Posts: 24217
    38
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jim Yingst:
    I foolishly assumed EFH knew what he was talking about.


    Well exCUUUUUUUUUUUUUUUSE MEEEEEEEEEEEEE!
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We could go with the "Layne is delusional" theory instead I suppose. We might have to alter the evidence a bit to make that work, but it's not that difficult...
     
    Shilpa Marodia
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i believe second code is more efficient as the whole print statement will come under class scope.
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Shilpa Marodia:
    i believe second code is more efficient as the whole print statement will come under class scope.

    Huh? Can you please explain what you mean?
     
    Ram Hebbale Hiriyanna
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    dear ms. marodia, could you please explain that to me in detail over a coffee???
     
    Shilpa Marodia
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    well the print statement will behave exactly in the same manner everytime it runs.it has no dependency on the state of the object.And Static belongs to CLASS.A static variable for that matter will be shared by all instances of that class:there is only one copy..
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Shilpa Marodia:
    well the print statement will behave exactly in the same manner everytime it runs.it has no dependency on the state of the object.And Static belongs to CLASS.A static variable for that matter will be shared by all instances of that class:there is only one copy..

    So? The main method is also static. Code in either the static initializer or the main method is running in a static context.

    Did you see my post above? If you look at the byte code, you see that there is no practical difference at all between the two.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!