Java Programming: 10 - Reference and Value types

preview_player
Показать описание
In this tutorial we look at the difference between value types (primitives) and reference types (objects).

Primitives are passed by value, so when passed to a method, you aren't manipulating the original data, but a copy of that data.

Objects, on the other hand, are passed by reference, so you have the ability to change objects through that reference inside of methods.
Рекомендации по теме
Комментарии
Автор

why are you so good at explaining java? should definitly make more videos on the topic, i would watch them all. learned a lot from you, thanks bro

josephsong
Автор

Woah, I was always confused when something is a reference and when it is not. This explains it perfectly. Thanks!

DancingTeapot
Автор

I bought a domain for it, but unfortunately haven't had the time to create a site. The plan will be to go back over all the content I have done already and create extended written tutorials with code samples (and possibly exercises), then go from there.

It would be pretty cool to create a forum and try to build up a community for answering questions too.

It will be on my channel and possibly introduced in a video if/when it is available.

CodeMonkeyCharlie
Автор

In a nutshell, without getting overly technical, "new" gives you a the location in memory of a newly created object (a reference). The type you refer to that object with tells the computer how to interpret that memory.

Without "new", you would have to allocate and manage memory yourself.

CodeMonkeyCharlie
Автор

I am new to Java. this video would help me lot to understand what the difference between Value type and Reference type. Thank you soo much.

rajusri
Автор

Speaking from experience, it ends up being much, much easier to understand code when it is composed of many relatively small (100-200 line) classes rather than a few thousand line ones.

There are many benefits to the object-oriented design approach, too many to get into in such a limited reply.

CodeMonkeyCharlie
Автор

Yes, everything that isn't a primitive type (like int, double, bool) is a reference type, and all reference types are instances of classes, or objects.

So, when you say something like
List myList = new ArrayList();

You are creating an object of type ArrayList, an instance of the ArrayList class.

CodeMonkeyCharlie
Автор

@swaminathavijayaraj Strings are actually immutable objects, they can never be changed once they've been created. Everything that appears to change a String is actually creating a new one, and returning a reference to the new String.

So, the simplest way to get that code working as intended could be something like:

public static String changename(String name){
return name.replace("Swami", "John");
}

And the calling line:

name = changename(name);

CodeMonkeyCharlie
Автор

Standard renaming refactoring. The keyboard shortcut that I used was shift + alt + r, but you can also right click a variable name and select Refactor > Rename. It is a smart rename, not simply a find and replace. It will pay attention to scope, as it did where I used it in the video (only renames within the method).

CodeMonkeyCharlie
Автор

I certainly plan to, but it is one of many projects on top of work, so I'm not sure when I'll get to cranking out more videos. There's a lot of prep/production time behind the relatively few minutes that end up on YouTube. I don't want to put out anything low quality. :)

CodeMonkeyCharlie
Автор

You should be javaGuru and not codeMonkeyCharlie! Thanks, I have been trying to understand this concept for a long time.Finally I got it! Once again you

Shridharkota
Автор

Functionally, it doesn't matter much. The idea is to break your program into conceptual objects, and develop the program as a set of interacting objects. Doing this promotes reusability, and helps you to break a large problem up into many smaller ones that are easier to wrap your head around.

At the end of the day, though, it all gets compiled down to something the computer understands. Anything that isn't in a format the computer understands is there to make our lives as programmers easier.

CodeMonkeyCharlie
Автор

Crystal clear and with perfect timing! Thank you!

WorklLife
Автор

Person is a class. You could think of it like a template for Person objects.

Person p1 = new Person();

p1 is an instance of the class Person, an object created using that template.

CodeMonkeyCharlie
Автор

@swaminathavijayaraj I believe you understand correctly. You can't change the value of a String, but you can change what a String variable references, thus changing a String can be thought of as simply changing what the variable points to.

For example:
String s = "Hello "; // creates a String object, assigns a refernece to it to s
s = s + "World!"; // takes the value of s, appends "World!" to it, resulting in a new String reference, and assigns that new reference to s

CodeMonkeyCharlie
Автор

THANK you Charlie I tried 2 other videos and this was just so clearly explained.

ericajaffe
Автор

They're definitely appreciated. Thanks for all your work so far.

fonzie
Автор

Thanks for the explanation!!! It's more clear to me now.

yoelbeche
Автор

@Gibbonator42685 Are you hovering over the method name, or the red x on the sidebar? If I hover over the red x, only the warning shows, but if I hover over the method name, it gives me quick fix options, one of which is to create the method.

If that doesn't work, try having your cursor on that line and pressing ctrl + 1.

CodeMonkeyCharlie
Автор

It would be good to note that the reference you're passing to a method is not a pass by reference in the traditional sense, rather you are passing a reference by value. This means that you can't change the assignment in the calling code as you might in C++, but you can alter the object that both the original reference and argument reference point to. This is sometimes referred to as 'pass by object' or 'pass by object-sharing'.

wulfgarpro