#Question about java

1 messages · Page 1 of 1 (latest)

versed thunder
#

Actually its more general coding question but whats the actual difference between writing normal function in the Main file and using OOP?

thanks in advance

delicate tinselBOT
#

<@&987246399047479336> please have a look, thanks.

#

You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.

versed thunder
#

And no bully im new

civic briar
# versed thunder Actually its more general coding question but whats the actual difference betwee...

Java doesn't really have "function" as they exist in other languges like e.g. C or Python. Java only has "methods". The difference is that Java methods are always bound to either a class (for static methods) or to a specific instance (non-static methods).

Take this for an example:

class A {
    static void f();
    void g();
}

...

public static void main(String... args) {
    // this is how we would call the 'static' method 'f'
    A.f();
    // this is how we would call the 'non-static' method 'g':
    A a = new A();
    a.g();

    // or in one line if we don't need the 'a' instance afterwards anymore:
    new A().g();
}
drowsy grove
#

A function would require you to have it outside of a class (which is not possible in Java). That way it decouples completely from a class and has no reference to it anymore (and the name of the class does not matter in this case, even if its called StringUtils or whatever).

uneven tendon
#

Unlike many other languages, Java is a purely object-oriented language. In functional programming (where you write normal functions), the functions are independent, and they are not assigned to classes.

In object-oriented programming, a function has to be assigned to an object (most likely a class). This is an example of the approaches in practice (I have to use different languages to show you):

Let's say you want to create a calculator.

  • In Java (an object-oriented programming language), you would do something like this:
public class Calculator {
  public static void main(String args[]) {
    // Rest of the code...  
  }

  public static int add(int a, int b) {
    return a + b;
  }

  // Rest of the code...
}

-In Haskell (a functional programming language), you would do something like this:

main :: IO ()
main = do
  -- Rest of the code...

add :: Int -> Int -> Int
add a b = a + b

-- Rest of the code...

The difference is that one uses an object (a class) and the other doesn't. Generally, these two types of programming serve different purposes, this was just an example I could compare them

silent cradle
#

like

#

OOP isn't a real thing

#

what is real, in java, is classes

#

so the difference between java and other languages is that all your functions need to live within a class definition

#

so static functions - which we call methods more often than not

#

those are the closest to a "normal function" in Python, JavaScript, whatever

tired star
#

For static methods the distinction is really just namespace. Plenty of languages have 'functions' organised by namespace.

Non-static methods are different, for one the methods effectively have an invisible first parameter 'this'.

Beyond that the OO model also provides a dynamic method dispatch model. But even this overlaps in part with dynamic dispatch models in non-OO languages.

The differences are cosmetic, convention and convenience... the convenience being that the language is design to make this approach the easier path to follow.

versed thunder
#

i'll read it all when i can but thanks alot guys

#

much appriciated

civic briar
#

the functions are atomic (independent)
That's not what atomic means. An atomic function would be one that executes effectively at once (this is important when it comes to multithreading to avoid race conditions).
An atomic action cannot stop in the middle: it either happens completely, or it doesn't happen at all. No side effects of an atomic action are visible until the action is complete.

An example for an atomic function from the real world would be CAS (compare and swap): https://en.wikipedia.org/wiki/Compare-and-swap

In computer science, compare-and-swap (CAS) is an atomic instruction used in multithreading to achieve synchronization. It compares the contents of a memory location with a given value and, only if they are the same, modifies the contents of that memory location to a new given value. This is done as a single atomic operation. The atomicity guara...

uneven tendon
civic briar
#

Basically you can think about it like so: An atomic function is (effectively) a single CPU instruction

uneven tendon
versed thunder
#

i genuinely feel like the noob player in this server

civic briar
#

*That's a German saying meaning basically: Noone started as a professional

silent cradle
#

part of why its probably feeling fuzzy is that "oop" is both a very muddy concept (decades of people with different takes on the subject), fundamentally about making larger programs (which you dont have experience making so frontloading it isn't great), and sorta only tangential to the mechanical questions you have (but still taints the explanations of those mechanics)

magic fjord
#

can i ask a question here

#

or do i have to make a forum

tacit siren
#

Make your own thread

versed thunder
#

if its related then ofc

versed thunder
#

alright

subtle palm
lusty crypt
#

being able to reason with the code better

#

since many people are familiar with objects IRL, and OOP is just an attempt to emulate that