As I thought about writing an operating system, I came across a point that I really couldn't figure out on my own:

Can an operating system truly be written in an Object-Oriented Programming (OOP) Language?

Being that these types of languages do not allow for direct accessing of memory, wouldn't this make it impossible for a developer to write an entire operating system using only an OOP Language?

Take, for example, the Android Operating System that runs many phones and some tablets in use around the world. I believe that this operating system uses only Java, an Object-Oriented language. In Java, I have been unsuccessful in trying to point at and manipulate a specific memory address that the run-time environment (JRE) has not assigned to my program implicitly. In C, C++, and other non-OOP languages, I can do this in a few lines.

So this makes me question whether or not an operating system can be written in an OOP, especially Java.

Any counterexamples or other information is appreciated.

migrated from stackoverflow.com Nov 27 '11 at 3:48

This question came from our site for professional and enthusiast programmers.

  • 2
    Being that these types of languages do not allow for direct accessing of memory. Really? – tibur Nov 27 '11 at 3:23
  • Yes. cosmos.codeplex.com But not entirely, see 'plugs' – Hightechrider Nov 27 '11 at 3:25
  • 3
    So Java sucks. Doesn't mean you can't do it with C++. – John Saunders Nov 27 '11 at 3:39
  • 5
    @nmagerko Java isn't a "true Object Oriented" language either, maybe you should try SmallTalk instead. – Raynos Nov 27 '11 at 4:13
  • 2
    It sounds more like you're asking if you can develop an OS in a language that runs in a sandbox or VM, rather than OO vs non-OO. – Bryan Oakley Nov 27 '11 at 13:41

The android backbone is written on top of linux so not really. It is possible to write an Operating System in C++ and there are many ones out there albeit not popular. C++ gives you the OOP concepts you want while still allowing you to do the low level stuff that you need in order to communicate with hardware. C still is the language that most operating systems are written in (with some assembly backbone required) and this is because C is lightweight and personally I find it better just because it removes a lot of the OOP stuff that I find isn't needed to write an operating system (that's just my opinion though).

Technically it is possible to write an operating system in Java in a sense you would need to hook into C/C++ code (I can't remember how to do it in Java but I believe you can) which would in turn call on the assembly required to talk to some of the hardware. The java code would also have to compile directly to machine language instead of the current bytecode scheme that runs on the Java Virtual Machine.

The high level concept of the OS could be done in java but the problem is that a lot of the functionality would have to exist in another form as doing some of the required things for handling data from devices and such cannot easily be accomplished in Java if at all.

  • Very interesting. I did not think of how the Linux kernel would aid in the creation, but this is a large oversight on my part. – nmagerko Nov 27 '11 at 3:26
  • 1
    Yes the android base is the linux kernel but the subsystem for android is java based which runs on a JVM on top of that kernel. – Jesus Ramos Nov 27 '11 at 3:28

You are miss-using a lot of terminology in your question.

OOP is a style of structuring programs. As such it is independent of programming language, you can easily do OOP in C. A lot of OS-APIs are in fact object-oriented, for example everything that returns a handle. OOP has nothing to do with not being able to unsafe operations in your programming language.

I think what you are really asking is: Can you write an OS in a language with automatic memory management and memory-safety?

The answer to that is: You have to cheat at some place.

Some options:

  • Make the compiler/runtime map some special objects directly to the addresses you need.
  • Add the necessary hardware-representation objects to the compiler/runtime build-in library (and do the unsafe stuff within the compiler) - somewhat similar to the first option
  • Extend the language/library with unsafe operations
  • Change the hardware so that it directly supports your language objects (e.g. http://en.wikipedia.org/wiki/Java_processor)
  • +1 OOP is a design style, not a language feature. much less a language restriction! – Javier Nov 27 '11 at 13:16
  • easily do oop in C? I'm not sure I agree with the easily part, but it's certainly possible. – Bryan Oakley Nov 27 '11 at 13:44
  • I do not understand the miss-use of terminology part. Please elaborate. – nmagerko Nov 27 '11 at 14:54
  • 3
    You are talking about memory-safety and automatic memory management in your question but are calling those things OOP. – Patrick Nov 27 '11 at 15:57
  • OOP is certainly possible in C albeit not trivial. C++ is implemented in C so it had to be done somehow ;) – Jesus Ramos Nov 27 '11 at 22:44

You can't write an entire operating system in C either. A small amount of it must be written in assembly for things like bootstrapping, entering supervisor modes, etc. Add a little more assembly wrapped with a small peek/poke class and an interrupt handler class, and you're pretty much good to go in any OO language. There will be obvious inefficiencies without direct pointer manipulation, but that just makes the language ill-suited.

The main problem with Java is not its object-orientation, but the fact that it doesn't compile to native bytecode, so you require a JVM that has to be written and bootstrapped via some other means.

  • 1
    Though I agree Java is not really suitable as an OS implementation language, there are Java compiler that generate real op-codes and there are actual physical implementations of the JVM; so the last point does not hold. – Martin York Nov 27 '11 at 5:19
  • How could you add Assembly into a Java class? I know there is inline Assembly in C (C++?), but is there such an API in Java? – nmagerko Nov 27 '11 at 14:56
  • Java has the Java Native Interface to cover such contigencies. Most languages have something similar. – Karl Bielefeldt Nov 27 '11 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.