3

So, if you have ever looked on my page you might have found that I'm a Grade 10 student that's just started his computer science course in high school. Yaay! :)

The language that we are learning is Java, something which in my opinion, is very different from Python (at least so far). However, there's one thing that I have noticed when starting a Java program. Sorry if the syntax is off or wrong.

public static void main(String [] args){
    String school = "A beautiful school";
    System.out.print(school);
}

Is this equivalent to Python's:

if __name__ == "__main__":
    school = "A beautiful school"
    print(school)

I've asked my teacher about this but didn't seem to get an answer that I completely understand. I also took a look at this question, but it seemed as if it only answered the different keywords public, static, void, (which I only slightly understand as of now).

So does public static void main act the same as if __name__ == "__main__"? If not, what's the difference between the two? Thanks in advance!

Community
  • 1
  • 1
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • Nope. main() in java is the main function for your program. in python, each module/source file can have the main statment like that. It is executed when you directly run the given module/sourcefile. If you just import the modules, its not executed. Its useful if the modules can be used as a standallone programs or for quick testing. – Marcin Feb 05 '15 at 23:53
  • So main() in java operates when I explicitly say to run my java class, unlike Python where it can be for anything. Am I right? – Zizouz212 Feb 05 '15 at 23:55
  • That is correct for Java: `main` is the method that runs when run your class with the "java" command. – Daniel Kaplan Feb 06 '15 at 00:00
  • 2
    @Marcin My Java is a bit rusty, but I'm pretty sure you can have as many classes with `main` methods as you want, and choose which to run by naming that class when invoking the program. That's about as analogous as it gets, given all the other differences between the two languages. –  Feb 06 '15 at 00:00
  • @Marcin That's actually also how it works in Java. The mechanism is different, but the principle is the same. – RealSkeptic Feb 06 '15 at 00:01
  • 1
    Looking for equivalents is generally not a good way to learn a programming language. Each language has its own way of thinking, and will work best for you if you learn it on its own terms. – Patricia Shanahan Feb 06 '15 at 00:01
  • @delan. You right. I meant that in Java the execution starts in main function. In python, there is no need for any main, entry point, method like this. – Marcin Feb 06 '15 at 00:01
  • @PatriciaShanahan I think I disagree and would also add that I would find it impossible to avoid this. – Daniel Kaplan Feb 06 '15 at 00:07

1 Answers1

2

When you do if __name__ == "__main__": you are checking to see if you are in main already.

It's different because the entire python script is considered to be "main" in the way that public static void main is considered to be "main" in java.

When you run a python file, it starts from the very top and works its way down looking for executable statements (it bypasses declarations such as function definitions and classes).

When you run a java class it looks for the main method and starts from there.

The reason for having this if __name__ == "__main__": is so that it only executes when you run the python file directly.

As you will learn soon, python files can also be considered as "modules" to be included from other python scripts. In such cases you would not want this 'main' logic to be implicitly executed.

Much like you almost never invoked a classes main when importing it as part of a larger app in java.

robert
  • 4,612
  • 2
  • 29
  • 39
  • Ok. I understand that Python is basically a bunch of files with various functions and classes, which are all basically put together by a main script (at least that's how I have been programming in Python over the last couple of years), and in Java it just performs what happens in the public static void main? – Zizouz212 Feb 06 '15 at 00:30
  • 1
    Public static void main is what's called the "entry point" it's where main programmatic execution commences (though various declarative & initialisation activities can happen beforehand) – robert Feb 06 '15 at 00:41