Does Java Ever Pass by Reference?

  Рет қаралды 720

Mary Elaine Califf

Mary Elaine Califf

Күн бұрын

Пікірлер: 6
@Rockyzach88
@Rockyzach88 Жыл бұрын
I enjoy your videos. That being said, then what does "pass by reference" mean if not passing the value of the reference? Is it more like pointers from c++?
@maryelainecaliff
@maryelainecaliff Жыл бұрын
It means that I am passing the memory address of the actual parameter. Passing a reference by value looks just like passing the value the reference was pointing at by reference. The difference is what you had to start with. In pass by reference, the actual parameter that we pass is not passed to the function. Instead, we pass the memory address of the actual parameter to the function. Then whenever we access that parameter in the function, we are automatically sent to the original item. It's a little like a pointer in C++, but pointers never get automatically dereferenced. When passing a reference by value, we started with a reference. We never had just the object in Java. We always had a reference to it. Therefore, the actual parameter we're passing is a memory address, and we're just making a copy of it. Does that help clarify the difference? Note that in languages that support pass by reference, you can typically pass different kinds of things by reference. So in C++, things I can pass by reference include ints, objects, and even pointers.
@mxie360
@mxie360 Жыл бұрын
Hello Dr Califf. Could you consider Java to be weakly typed language since if you create an object, you can change its datatype to one of its child class? For example if you have a object x, you can change it to a Integer object later down in the class, but also change it to a different object again later in the class. Of course you cannot do this with primitives, only with classes that have child classes. This is an example code that works, and shows that weak typing can exist in java. Object x = new Object(); System.out.println(x); x = new Integer(5); // changed typing to integer System.out.println(x); x = new Double(5.0); // changed typing again to double System.out.println(x); Thank you, Mike Xie.
@maryelainecaliff
@maryelainecaliff Жыл бұрын
TLDR: there are certainly languages that are more strongly typed than Java, but we generally consider Java to be toward the strongly typed end of the strongly to weakly typed continuum. Also, assigning a subclass object to a superclass pointer/reference is generally not considered a problem in any language with classes and inheritance. The more full answer: Strongly typed vs weakly typed isn't a dichotomy -- it's a continuum. Java is not one of the MOST strongly typed languages, but it is generally considered strongly typed as opposed to weakly typed. Remember that there are things that I can't do without casting, some I can do with casting but will cause runtime errors, and some I can't do even with casting. Try the following -- first without the casts and then with them: public static void main(String[] args) throws Exception { Object obj = new Object(); Double num1 = new Double(4.5); String str = new String("myStr"); Integer num2; num2 = (Integer) obj; num2 = (Integer) num1; num2 = (Integer) str; } Then try commenting out the last two lines and running the program. Then try doing similar things in languages like Javascript or Perl, which are very weakly typed languages. Now, really until we run the program, most of this is about Java being both primarily statically typed and somewhat strongly typed. Python could do the equivalent of the above as well, because it is dynamically typed. However, it is to some extent more strongly typed than Java when it comes to actually using variables, as demonstrated by the difference between String str="Label: "+ 14; // which works fine in Java and str = "Label: " + 14 # in Python which will cause an error (at runtime, because dynamically typed and interpreted) and then for dynamic and weak typing, consider if (1 == "1") in Javascript, which has a === operator for a reason. Note that we typically don't consider assigning an object to a superclass pointer or reference to be weak typing in general, because it supports the very valuable polymorphism feature of object-oriented programming. And, if subclasses are created well (following something called the Liskov substitution principle), we can always treat a subclass object as if it were a member of the superclass.
@mxie360
@mxie360 Жыл бұрын
​@@maryelainecaliff this is a very good write up. In college textbooks and lectures, they give a very simple explanation of weakly typed vs strongly typed. And then just group programming languages together based on very simplistic binary categories. I tried out the code, and I see that in some ways python can be more strongly typed in Java when using variables, and that these simplistic labels don't always accurately describe the language. Now I am seeing that these categories are not as binary, and is more on a "continuum". I am seeing that every category that we have for programming languages is also on a "continuum" and not binary. An example, that you might already know, but it is a really good example that wasn't taught at my college or lectures. Java is considered Object Oriented. However, Java is not a purely Object Oriented language since it allows users to use primitives. Python is also not a purely Object Oriented language, as it doesn't allow encapsulation, which is a main feature of OOP. Smalltalk is the only programming language that I know that can be classified as a pure OOP language. Yet we still consider Java and Python to be OOP languages. Also thank you for your response! I can't wait to see more videos.
@maryelainecaliff
@maryelainecaliff Жыл бұрын
Those are nice examples of some of the ways in which the categories we use are a little fuzzy.
Parameter Passing Mechanisms 1
16:49
Mary Elaine Califf
Рет қаралды 1,1 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 680 М.
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
How An Infinite Hotel Ran Out Of Room
6:07
Veritasium
Рет қаралды 31 МЛН
Function Activation Records
13:08
Mary Elaine Califf
Рет қаралды 5 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 996 М.
HashMaps & Dictionaries, Explained Simply
22:44
Nic Barker
Рет қаралды 13 М.
Differences Between Minecraft Java VS Bedrock
11:41
Skip the Tutorial
Рет қаралды 10 МЛН
What P vs NP is actually about
17:58
Polylog
Рет қаралды 147 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 342 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 197 М.
5 deadly Rust anti-patterns to avoid
13:25
Let's Get Rusty
Рет қаралды 39 М.
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН