#Question about java
1 messages · Page 1 of 1 (latest)
<@&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.
And no bully im new
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();
}
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).
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
Basically none
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
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.
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...
You are correct, I'll edit the message immedietely
Basically you can think about it like so: An atomic function is (effectively) a single CPU instruction
Yeah, I confused them for a bit, thanks
i genuinely feel like the noob player in this server
no master has fallen from the sky
*That's a German saying meaning basically: Noone started as a professional
eh so
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)
Make your own thread
they got a thread here now https://discord.com/channels/272761734820003841/1356591711438241863
alright
Goal of OOp is to make large code easier to understand and most importantly easier to maintain
scalability
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