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!

8
  • 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
    Commented Feb 5, 2015 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
    Commented Feb 5, 2015 at 23:55
  • That is correct for Java: main is the method that runs when run your class with the "java" command. Commented Feb 6, 2015 at 0: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.
    – user395760
    Commented Feb 6, 2015 at 0:00
  • 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. Commented Feb 6, 2015 at 0:01

1 Answer 1

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.

2
  • 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
    Commented Feb 6, 2015 at 0: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
    Commented Feb 6, 2015 at 0:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.