Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type confusion: A type is class, a variable and an object?

Tags:

c#

I'm currently working through the Pluralsight C# 5.0 course, and I'm relatively new to programming.

I previously thought I understood the concept of Data Types on a basic level, Int/Array/Strings etc. In this course it starts to introduce C#'s huge emphasis on Types, and creating your own custom types.

One of the course code snippets which I've included below, is refusing to sink in and I was hoping someone could provide some clarity or a different way of thinking about it.

Program.cs:

GradeStatistics stats = book.ComputeStatistics();

GradeStatistics.cs:

namespace Grades
    {
        public class GradeStatistics
        {
            public GradeStatistics()
            {
                HighestGrade = 0;
                LowestGrade = float.MaxValue;
            }

            public float AverageGrade;
            public float HighestGrade;
            public float LowestGrade;
        }
    }

Grades:

public GradeStatistics ComputeStatistics()
        {
            GradeStatistics stats = new GradeStatistics();

            float sum = 0f;
            foreach (float grade in grades)
            {
                stats.HighestGrade = Math.Max(grade, stats.HighestGrade);
                stats.LowestGrade = Math.Min(grade, stats.LowestGrade);
                sum += grade;
            }

            stats.AverageGrade = sum / grades.Count;
            return stats;
        }

I'm finding it particularly difficult to understand what exactly GradeStatistics is. In the course it is referred to as not only a class, but as a variable, and furthermore also being returned as an object in Grades.

Any clarity is appreciated, as I'm finding it a little difficult to follow with all of the above terms being thrown around.

like image 846
orangey Avatar asked Apr 27 '14 19:04

orangey


People also ask

Can class be a type?

A class is a type. An interface is a type. A primitive is a type. An array is a type.

What is type () in Java?

Type is the common superinterface for all types in the Java programming language. These include raw types, parameterized types, array types, type variables and primitive types. Since: 1.5.

What is the difference between an object and a variable?

An object is a sort of separate entity. Importantly, the value of a variable or any expression is never an object, only a reference.

Is variable an object in Java?

However, declaring a variable does not create an object! This is an important point, which is related to this Very Important Fact: In Java, no variable can ever hold an object. A variable can only hold a reference to an object.


2 Answers

GradeStatistics is a class, from this declaration:

public class GradeStatistics

stats is a variable of type GradeStatistics, from this declaration:

GradeStatistics stats = new GradeStatistics();

The return type of ComputeStatistics is GradeStatistics, from this declaration:

public GradeStatistics ComputeStatistics()

So, it's a class. It is being used to declare a variable with a particular type, and it is used to declare what a particular method will return, an object of the type.

If it helps, you can sort of think of a type as a blueprint. You can have a blueprint of a house. This will tell you that "if you had a house", this is what it would look like.

When you construct an instance of the type, ie. build the house, you get an instance. It has a type (it follows the blueprint), but it may be different from other instances, having other property values (like the color of the paint used, or the style of doors).

like image 137
Lasse V. Karlsen Avatar answered Nov 10 '22 06:11

Lasse V. Karlsen


First, GradeStatistics is a Class.

Second, it is not referred as a variable anywhere. But stats is actually a variable of type GradeStatistics in this line.

GradeStatistics stats = new GradeStatistics();

Third, ComputeStatistics is a function which return GradeStatistics.

And if you have read OOP than you should know that any object of any class type can be returned as a function return value.

like image 27
Zeeshan Elahi Avatar answered Nov 10 '22 05:11

Zeeshan Elahi