• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Understanding java basics words

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going through a video tutorial and the instructor when saying certain words like "Instance, methods, object he selects the wrong one on the screen. Like for example when saying method he might select variable. So I want get this correct so I know what he is referring too.

I've added comments to the following code and want to know if they're all correct.

Question is:

1. Which one is a method?
2. Which is a function?
3. Which one is a object?
4. Which one is a instance of class?
5. Which one is a method type?



 
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's mostly right.

Instance and object are the same thing. Instance tends to get used with class, as in "an instance of the Person class" which is the same thing as "a Person object."

Person in Person person1 is the "Declared type" which means you are declaring the person1 variable to be a reference to a Person object, i.e. an instance of the Person class. In other words, you're saying that the type of object the person1 variable will reference is a Person

Line 9 is a declaration, assignment, and instantiation all in one. The left side of the assignment operator, =, is the variable declaration and the right side is the instantiation of a Person. That whole line would not be referred to as "an instance of a class" (unless you're trying to be misleading or don't know what you're talking about) although one might say correctly that "we create a new Person on line 9" or "we instantiate a Person on line 9" or "we call/invoke the no-args Person constructor on line 9" and "assign a reference to that object to the person1 variable" or simply, "and assign it to person1."

Hope this helps ... and Welcome to the Ranch!
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding your last question about "method type," can you give a bit more context? How was that term used? Just by itself, I would interpret that to mean "a method's declared type." In bothe methods in the code you gave, the main() method in the App class and the sayHello() method in the Person class, the declared type is void which means the methods don't return any value. Void methods only do things. Methods that are declared with any other type are expected to return a value of that type.

In Java, there are two categories of types: primitive types like int, long, boolean, double, float, byte, short, and char. Then you have reference types like String, Date, ArrayList, and everything else in the standard libraries, and any new type you define with a class declaration.

Arrays are kind of a special thing in that they are reference types (arrays are objects and an array type is a reference type) but they can have elements that are primitive values or references to objects. By the way, because "reference to object" is a mouthful, we often use just "object" as a shortcut, so I could have also said that "arrays can have elements that are primitive values or they can be objects." Arrays can only have one type so you can't have both primitive values and objects as elements of an array, it's either one or the other.

int[] is an array type, a primitive (int) array type

String[] is also an array type. An array of this type will have references to String objects as its elements. Alternatively, using the shortcut wording, we would say that type of array can hold String objects.

These are array declarations:

 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Instance and object are the same thing. . . .

I have always thought that instance is rather more specific than object. I say, as you did, and instance of class Foo, but I also say plain simple object without sayin which type it is an instance of.
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, which is why I also wrote that "instance" is usually used together with "class," as in "an instance of the Person class" or simply "an instance of Person" or even "a Person instance." The last one is synonymous with "a Person object" and "a Person."

That is, a Person object is an instance of the Person class.
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context is important when differentiating these terms. In many cases, "an object reference" and "an object" mean the same thing but in specific contexts, there is a clear distinction between a "reference" and an object. This distinction usually comes into play when discussing garbage collection and eligibility for being collected. But we'll cross that bridge when we get to it.
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

joebloggs Bloggs wrote:. . .
2. Which is a function?
. . .
// This is a method - aka function?
. . .

Answer to question 2:
none of them. We don't call anything a function as a Java® term (though “function” is usually used, maybe imprecisely, in languages like C). In Java® we call everything like that a method. Now, the mathematicians talk about functions as meaning a calculation where you ive it an input and get an output; the same input should always produce the same output. Square is an example of a function; if you give it −2.0 as an argument, it will always return 4.0. You will hear people talk about a method which implements a function; this is an example:-As Junilu says, we are informal in the way we talk, so people will go from saying that is a method implementing a function to calling it s function, but the correct word is “method” whether you are or are not implementing a function.
The sayHello() method you showed in your code block isn't a function because it neither takes any input nor returns any result. Printing something on the screen doesn't count as a result

And (again) welcome to the Ranch
 
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The sayHello() method you showed in your code block isn't a function because it neither takes any input nor returns any result. Printing something on the screen doesn't count as a result


Also is said, that functions shouldn't have side effects. In fact, printing something can be considered as side effect, because it prints to System.out. All void methods can be assumed as having some sort of side effects, apart from some nonsensical implementations as for example "with empty body".
 
Greenhorn
Posts: 6
3
MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I think a little bit of background will be helpful; lets start by loosely defining the terms.

Class: A class is the blueprint for an object. You write classes, and later you can use them to create objects:

This a class (it is empty, but still a valid class). That is easy to see, but it is important to understand what a class is, before understanding what an object is.

Object: An object is basically a set of state and behavior (variables and methods, we'll get there) which live in one place in the computer's memory. Remember that a class is the blueprint for an object. You create the design of your objects by writing classes. Then you create objects by instantiating a class i.e. you ask the Java Virtual Machine (JVM) to build the objects for you, from your blueprint. An object is an instance of a class. You cannot see objects, they only live in the computer's memory. You can use them, however, because you get what is called an object reference. This is like the object's street address. When you create an object you save its address in a variable, which is what you are doing below:

The "new Person()" here says "Hey java! Please make me a new object from the Person blueprint (class)." The "person1" is a variable, and you need it here to save the reference of the new object. Do note, though, that just as you can build many houses from the same blueprint, you can make as many objects as you like from a class.

Variables: A variable is a container. There are different containers for different things, though. We need to know what a container stores. When we declare a variable (basically create a new one) we need to say what kind of container it is. This is done by putting the variable's type (what kind it is) in front of it. For variables which store object references, the type is the class that the object is built from.


Ok, now for
Methods: A method is a set of statements which are grouped together into a something you can use over and over again. Methods don't have a type(they do have a return type, which just tells you what kind of value it returns). They also have a name, a list of parameters (they tell you what you must give a method when you call it), and a body (which contains all of the code inside the method, the code you want to reuse).

For a longer explanation of what a method is check out https://www.tutorialspoint.com/java/java_methods.htm

Now I hope this diagram helps:



Sorry for the late reply, I’ve been working on this for a couple of hours. I also lost part of it when I tried to submit it when I had been automatically logged out.

I really hope this makes sense, and will give you a little more understanding of these different things!
 
Liutauras Vilda
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jj Roberts, welcome to the Ranch! What a great first post!
 
Jj Roberts
Greenhorn
Posts: 6
3
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Jj Roberts, welcome to the Ranch! What a great first post!



Thank you so much!

And I get a cow too? Yeehaaa!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is my first post. I am wanting to see if I've "tagged" my code correctly. Sweet! Thank you for the help!
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Joshua Rowland!

Unfortunately, you didn't do it right. The code you want to post goes between the start and end tags. See the wikipage with complete instructions on how to UseCodeTags (←click that, it's a link)
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!