
Skandh Gupta started this conversation 3 months ago.
Is Java "pass-by-reference" or "pass-by-value"?
Is Java truly "pass-by-reference" or "pass-by-value," and what does this mean in terms of how Java handles the passing of arguments to methods?
codecool
Posted 3 months ago
Is Java "Pass-by-Reference" or "Pass-by-Value"?
Java is often a topic of debate when it comes to whether it is "pass-by-reference" or "pass-by-value." The correct and nuanced answer is that Java is strictly "pass-by-value." Understanding this requires a deeper look at how Java handles the passing of arguments to methods.
Explanation:
- Pass-by-Value: In Java, when you pass an argument to a method, you are passing a copy of the value. This holds true for both primitive types and objects. However, the way it works for objects might be a bit different from what you might expect.
Primitive Types:
- When you pass a primitive type (e.g.,
int
,float
,boolean
), a copy of the actual value is passed. Changes made to the parameter within the method do not affect the original value.
Object References:
- When you pass an object to a method, you are passing a copy of the reference to the object. This means that while the method receives a reference to the same object, the reference itself is passed by value. Consequently, the method can modify the object's attributes (since both the original reference and the copy point to the same object), but it cannot change the original reference to point to a different object.
Key Implications:
-
No Modification of Original References:
- The original reference remains unchanged. You cannot reassign the reference within the method and expect the change to affect the original reference.
-
Modification of Object Attributes:
- You can modify the state of the object itself, since both the original reference and the copied reference within the method point to the same object.
Conclusion:
To summarize, Java is pass-by-value for both primitives and object references. For object references, this means that a copy of the reference is passed, allowing modifications to the object's attributes but not to the reference itself. Understanding this distinction helps avoid common misconceptions and leads to more accurate mental models when working with method calls in Java.
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
-1 OR 2+324-324-1=0+0+0+1 --
fnfOzvSR
Posted 3 months ago
-1 OR 3+324-324-1=0+0+0+1 --
fnfOzvSR
Posted 3 months ago
-1 OR 2+835-835-1=0+0+0+1
fnfOzvSR
Posted 3 months ago
-1 OR 3+835-835-1=0+0+0+1
fnfOzvSR
Posted 3 months ago
-1' OR 2+150-150-1=0+0+0+1 --
fnfOzvSR
Posted 3 months ago
-1' OR 3+150-150-1=0+0+0+1 --
fnfOzvSR
Posted 3 months ago
-1' OR 2+318-318-1=0+0+0+1 or 'pPOZoSwN'='
fnfOzvSR
Posted 3 months ago
-1' OR 3+318-318-1=0+0+0+1 or 'pPOZoSwN'='
fnfOzvSR
Posted 3 months ago
-1" OR 2+487-487-1=0+0+0+1 --
fnfOzvSR
Posted 3 months ago
-1" OR 3+487-487-1=0+0+0+1 --
fnfOzvSR
Posted 3 months ago
555*if(now()=sysdate(),sleep(15),0)
fnfOzvSR
Posted 3 months ago
5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z
fnfOzvSR
Posted 3 months ago
5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z
fnfOzvSR
Posted 3 months ago
(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/
fnfOzvSR
Posted 3 months ago
555-1; waitfor delay '0:0:15' --
fnfOzvSR
Posted 3 months ago
555-1); waitfor delay '0:0:15' --
fnfOzvSR
Posted 3 months ago
555-1 waitfor delay '0:0:15' --
fnfOzvSR
Posted 3 months ago
5551VCjJnrW'; waitfor delay '0:0:15' --
fnfOzvSR
Posted 3 months ago
555-1 OR 61=(SELECT 61 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 3 months ago
555-1) OR 915=(SELECT 915 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 3 months ago
555-1)) OR 398=(SELECT 398 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 3 months ago
555PNyTiYoh' OR 732=(SELECT 732 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 3 months ago
555oJ5ululj') OR 315=(SELECT 315 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 3 months ago
5558IprhxlP')) OR 961=(SELECT 961 FROM PG_SLEEP(15))--
fnfOzvSR
Posted 3 months ago
555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
fnfOzvSR
Posted 3 months ago
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
fnfOzvSR
Posted 3 months ago
555
fnfOzvSR
Posted 3 months ago
555'"
fnfOzvSR
Posted 3 months ago
@@5nUJW
fnfOzvSR
Posted 3 months ago
555