For Java - What is the difference between primitive and class types? Watch

ShaolinTemple
Badges: 15
Rep:
?
#1
Report Thread starter 9 years ago
#1
What are the differences and how does have an effect on the program and the programmer?

I've been taught Javascript for the 1st semester and now we've moved onto Java. The lecturer said it is vital to understand these two types but to me so far they just seem like two variables except that class types only store references to objects whereas primitive types store the actual value. I don't think I understand the reference to objects bit. What's the importance of this? Does this limit me when I'm programming? Should I take it into careful consideration when programming or is it nonsense?
0
reply
INTit
Badges: 17
Rep:
?
#2
Report 9 years ago
#2
Objects are complex data types, primitives are err primitive :P
Objects you can create yourself whilst primitives are a standard part of the language.

When hes talking about references in Java think of a reference as a remote and the object as a tv. You might be able to control the tv with a remote but its not the actual tv.

Therefore If you use an == on an object it wont work as its actually comparing the address of the remote and not the object.

In C++ you can easily see this by doing: object1 = object2. The old object1 remains in memory ! this is because you just changed the remote/reference to point to something else you didn't actually touch the object itself. Java works the same but Java will delete an object if theres no references to it.

The main confusion with references happens when passing parametres to methods.

When you pass an Object to a method its actually passing a references(remote) to your method.

When you pass a primitive to a method it passes a copy of the variable.

Hope thats cleared some stuff up.
0
reply
djhworld
Badges: 0
#3
Report 9 years ago
#3
basically primitives are stored in a direct location in memory so say for example I used the code

int myAge = 21;

The compiler would read that from a direct location in memory, i.e. the compiler would look at the memory address where myAge is stored and find the value "21"

Objects are....well.....objects. They're classes basically that incorporate a multitude of things (attributes and methods) however when you instantiate one the compiler works a bit different. So if I were to use the code

AgeObject myAge = new AgeObject(21);

I'm making a reference to that object. Basically I'm pointing to where that object is located rather than referencing it directly. So when the compiler looks at "myAge" it is effectively looking at a memory address where the relevant object is located.
0
reply
tasGooner
Badges: 5
Rep:
?
#4
Report 9 years ago
#4
Primitives are the actuall "real" data ie int age = 21;

Classes (which, upon run time, are turned into objects) are complex and they can be modelled on the real world.

I.e. in a library, a Book Class may contain, title, author etc.

A Class/Object can represent anything from the real world.
0
reply
newman24x
Badges: 0
Rep:
?
#5
Report 9 years ago
#5
(Original post by ShaolinTemple)
What's the importance of this? Does this limit me when I'm programming? Should I take it into careful consideration when programming or is it nonsense?
Object Oriented Programming is one of the fundamental concepts/paradigm of programming hence Computer Science...so it's not nonsense I advice you to really take your time understanding from all the previous replies or you're going to have problems in further development.

If you still can't grasp OO concepts, feel free to PM me
0
reply
ShaolinTemple
Badges: 15
Rep:
?
#6
Report Thread starter 9 years ago
#6
What about classes, objects, field variables, methods and state?

From what I gather a class is basically a blueprint of an object. You can have lots of different instances a class which is an object. The method is more or less the main bulk of a class and it describes its behaviours. The field variables is just basically the variables declared outside the methods i.e. the variables that are local to that class and global to all the methods in that class. The state is the variables because these variables describe what state the object is.

To me an object and a class is more or less the same thing except that an object can change its values whereas a class cannot unless you use brutal force. It's the same up to the point when you start changing one or more of it's values. I also see an object just being a variable that's been referred to the class and then that variable takes the form of that class and you can mold it any way you like.

That's my way of thinking, just to simplify it all for my lil head.
0
reply
newman24x
Badges: 0
Rep:
?
#7
Report 9 years ago
#7
(Original post by ShaolinTemple)
What about classes, objects, field variables, methods and state?

From what I gather a class is basically a blueprint of an object. You can have lots of different instances a class which is an object. The method is more or less the main bulk of a class and it describes its behaviours. The field variables is just basically the variables declared outside the methods i.e. the variables that are local to that class and global to all the methods in that class. The state is the variables because these variables describe what state the object is.
correct.

To me an object and a class is more or less the same thing except that an object can change its values whereas a class cannot unless you use brutal force.
i think you already mentioned what's the difference between Class and Objects. As stated by you:

From what I gather a class is basically a blueprint of an object
basically a Class is a data type (a complex data type) that creates or in OO 'instantiates' an object. Basically an object is a just a variable that can invoke/called another variable (so here would be it's attributes) and functions or in OO we called it s behaviors/methods

I also see an object just being a variable that's been referred to the class and then that variable takes the form of that class
like the above.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you chained to your phone?

Yes (90)
20%
Yes, but I'm trying to cut back (181)
40.22%
Nope, not that interesting (179)
39.78%

Watched Threads

View All
Latest
My Feed