0

During 3 years of my working career, I have been working with databases, data, etc. It was only during the last year that I started working with Python for some data analysis. Now i got interested in all the Big Data ecosystem and Python gets me far enough, yet.

However, recently I chose to learn Scala as my second programming language. It appears that usually my program needs to have a class, a method, and then it needs to be built. It is all very confusing to be honest :)

So I read on and it appears that Scala comes from JVM environment, and I started reading on Java and it turns out that in Java you cannot just create a program consisting of a single command. You need to create a class, a method, etc. I understand that it is probably because it follows one of the principles of OOP, but could anyone please direct me to the source, which would explain why do we need to create classes and methods in java - as opposed to listing commands only?

Denys
  • 4,287
  • 8
  • 50
  • 80
  • 1
    You gave the answer already. In OOP First class citizens are Objects. Some Languages take it a bit further where functions are already objects. Python in comparison to java doesn't follow such a clear (fundamental ;) ) principles – CAFEBABE Feb 16 '16 at 20:48
  • 2
    [Because bad design choices.](http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html) – user2357112 Feb 16 '16 at 20:48
  • @CAFEBABE Yeah, I listened a lecture on functional programming and it says that in OOP nouns are first class citizens (Classes/Objects) and verbs (methods) cannot go alone without a сonvoy :) – Denys Feb 16 '16 at 20:51
  • 1
    Actually the design flaw in java are the primitive data types and the operators '+' which break these principle. However, now I'm wondering: What is your question. – CAFEBABE Feb 16 '16 at 20:52
  • Possible duplicate of [Why use classes instead of functions?](http://stackoverflow.com/questions/6480676/why-use-classes-instead-of-functions) – CAFEBABE Feb 16 '16 at 20:53
  • I was kind of trying to understand what happens under the hood. What prevents me from firing a "Java console" and typing print('Hello World'). Will go back to my research, and will delete the question probably :) Thanks :) – Denys Feb 16 '16 at 20:55
  • I'd say that it's just a convention. A class file, which are compiled JVM programs needs an entry point, someone established that given the model of the class files, a static public method named `main` with a certain signature and residing in a public class would fit. And there the convention was established. Have you ever programmed in C? How a C program is traditionally executed? – pedrofurla Feb 16 '16 at 20:59
  • @Dennis The one thing preventing you from doing "print("hello world!")" is that you dpn't have a print method. – Bálint Feb 16 '16 at 21:00
  • @Bálint and the absence of "Java console" I assume ;) – Denys Feb 16 '16 at 21:01
  • @pedrofurla To be honest - never touched C. Will take a look on the simplest program now :) – Denys Feb 16 '16 at 21:01
  • 1
    That will give a partial explanation, or at least point to where `main` draws its inspiration from. Have a look here https://en.wikipedia.org/wiki/Entry_point. – pedrofurla Feb 16 '16 at 21:05
  • @pedrofurla oh, that's a good one, thanks – Denys Feb 16 '16 at 21:09

3 Answers3

1

So I read on and it appears that Scala comes from JVM environment, and I started reading on Java and it turns out that in Java you cannot just create a program consisting of a single command. You need to create a class, a method, etc. I understand that it is probably because it follows one of the principles of OOP

First, let me briefly explain why Python does not requires you to create a class or something similar in order to run it.

Python is designed as an interpreted language (or you can call it script). The instructions you wrote are series of operating system commands handled by the command interpreter. So what is a command interpreter?

Command interpreter is part of the OS which executes commands entered by you. In some OS, it is known as a shell.

Java on the other hand is a designed as a compiled language which needs to be compiled by a language compiler. The language compiler converts your source codes into machine language so that you processor can work on it.

In my opinion, it is not really about OOP in Java. It is what they are which made the difference - to be compiled vs to be interpreted.


If you are just wondering why Java codes need a class, it has been asked before in SO:

Why do C# and Java require everything to be in a class?

Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Interpreted vs Compiled still doesn't seem to be a valid answer in my opinion. JVM byte code is just a data structure that that eventually gets interpreted as much as Python source (or parsed ASTs) are a data structure that eventually gets interpreted. But, that's my view. – pedrofurla Feb 16 '16 at 21:17
  • If we added a link of from @pedrofurla on entry point en.wikipedia.org/wiki/Entry_point - that would help to accept the way things are and why they are that way :) – Denys Feb 16 '16 at 21:20
0

Java is an object-oriented (OO) language. Classes are objects, one of the basic building blocks of this language. Inside the classes, you have methods, containing the "commands" you are speaking of.

I suggest you refer to literature on that topic - there is a vast amount of books outlining OO principles and the differences between different types of languages (OO, functional, procedural, etc.).

All in all, wikipedia might be a good starting point - see the list of main paradigm approaches here:

https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms#Main_paradigm_approaches

P.S.: As I'm unsure what exactly your question is about, I have to add that of course, Python is an OO language too. That you can run Python code without creating a class first, is a design choice.

Newtron
  • 416
  • 3
  • 12
0

Scala is made because of this. It mixes functional and OOP language features, so you can create methods by themselves, without creating a class to contain them.

Java doesn't have this feature, methods can't be created outside a class. Java is very object oriented, everything (except the primitives) extends object.

Some people say, that this is like this, because java is garbage collected. Having functions inside objects makes it easier, to free space up.

It really depends on you, if you find this good or not. In my opinion, it's better to get as far from functional programming, as possible. Let's not get back to the C era.

Bálint
  • 4,009
  • 2
  • 16
  • 27
  • Actually Scala is more object oriented than Java. And you can't really have top level methods in Scala. – Jasper-M Feb 16 '16 at 21:51