Difficulty in understanding of object vs function. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difficulty in understanding of object vs function.

Hello Everyone, I know this is very silly question but I'm confused. Every time I get confused when people says this object but it is look like function. Let me explain you with example StringBuffer sbf = new StringBuffer ("Sololearn") ; Here StringBuffer is look like a function but we call it as object because I know we using new keyword and Assigning reference to sbf. But the definition of function is look like () or with arguments. So how to deal with that StringBuffer is look like function why we can not say function. Because not every time we use () in object but when () is there why we can't say as a function. In short Looks like function then why we called as object.

9th Jun 2021, 6:34 PM
ㅤㅤㅤㅤㅤㅤ
ㅤㅤㅤㅤㅤㅤ - avatar
4 Answers
+ 2
StringBuffer sbf = new StringBuffer("solo learn"); Here StringBuffer is a class and we are calling it's constructor with "solo learn" string value passing . "new keyword makes an object of StringBuffer with needed allocated space . Now through that sbf object , we can call that class properties by period operator sbf.reverse(); sbf object can get allocated memory.. Now a method in java can return a single value or nothing return . Any returned value can be stored in appropriate data type . It has no space in memory after return from method. So actually it does not depend on () as seeing.. StringBuffer("solo learn") ; is calling StringBuffer class constructor ,and constructor is also a method. But there actually a "sbf is an object " . new keyword creates memory for object . For method ,we don't use new keyword there. What it holds and what it returns is the difference. edit: in java, we call methods , not functions. Hoping you know it.
9th Jun 2021, 7:14 PM
Jayakrishna 🇮🇳
+ 1
StringBuffer is the class and and is the object
9th Jun 2021, 6:45 PM
Atul [Inactive]
+ 1
ㅤㅤㅤㅤㅤㅤ StringBuffer is a class which has Constructor StringBuffer (String str). Now you are creating an object "sbf". Using this object you can access class properties and methods. See explanation here: https://code.sololearn.com/cR8J5Ohz4MAO/?ref=app
9th Jun 2021, 7:07 PM
A͢J
A͢J - avatar
+ 1
Jayakrishna🇮🇳 If a function in class Or we are going to use only for a class then it call as method and If we declared a function independently [In Public Section] and we are using whenever in program then it call as function. But java is 99.9% Object oriented that's why we call as method.
13th Jun 2021, 7:29 AM
ㅤㅤㅤㅤㅤㅤ
ㅤㅤㅤㅤㅤㅤ - avatar