`
xiongzhenhui
  • 浏览: 209644 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

java引用类型和值类型

 
阅读更多

Java传值还是传引用终极解释,还是看老外解释的清楚啊。

    博客分类:
    java开发

JavaIBMHTMLSUN

by http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Does Java pass by reference or pass by value?
Why can't you swap in Java?
By Tony Sintes, JavaWorld.com, 05/26/00
Q:If Java uses the pass-by reference, why won't a swap function work?
A:Your question demonstrates a common error made by Java language newcomers. Indeed, even seasoned veterans find it difficult to keep the terms straight.

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Take the badSwap() method for example:
Java代码  收藏代码

    public void badSwap(int var1, int var2) 
     
     int temp = var1; 
     var1 = var2; 
     var2 = temp; 



When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well. Now, here is where it gets tricky:
Java代码  收藏代码

     public void tricky(Point arg1, Point arg2) 
    { 
      arg1.x = 100; 
      arg1.y = 100; 
      Point temp = arg1; 
      arg1 = arg2; 
      arg2 = temp; 
    } 
    public static void main(String [] args) 
    { 
      Point pnt1 = new Point(0,0); 
      Point pnt2 = new Point(0,0); 
      System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);  
      System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); 
      System.out.println(" "); 
      tricky(pnt1,pnt2); 
      System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);  
      System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);   
    } 
     
                        

If we execute this main() method, we see the following output:
Java代码  收藏代码

     X: 0 Y: 0 
    X: 0 Y: 0 
    X: 100 Y: 100 
    X: 0 Y: 0 

The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.
Figure 1. After being passed to a method, an object will have at least two references

Figure 1. After being passed to a method, an object will have at least two references



Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.

Figure 2. Only the method references are swapped, not the original ones

Author Bio

Tony Sintes is a principal consultant at BroadVision. Tony, a Sun-certified Java 1.1 programmer and Java 2 developer, has worked with Java since 1997.O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap objects.
分享到:
评论

相关推荐

    Java中的基本类型和引用类型变量的区别

    Java 中的基本类型和引用类型变量的区别 Java 中的基本类型和引用类型变量是两个不同的概念,它们在内存分配、变量赋值和函数传递等方面有着本质的区别。 基本类型是 Java 中的八种基本类型,包括 byte、short、...

    浅析Java引用类型和方法参数传递

    ### 浅析Java引用类型和方法参数传递 #### 一、引言 在Java编程语言中,理解数据类型的处理方式对于编写高效、可维护的代码至关重要。本文将深入探讨Java中的引用类型及其如何影响方法参数的传递机制。通过具体实例...

    Java:按值传递还是按引用传递详细解说

    在Java编程语言中,关于参数传递的方式一直存在两种观点:一种认为Java仅支持按值传递,另一种则指出Java同时支持按值传递和按引用传递。实际上,这两种观点并非完全对立,而是根据不同的角度来描述Java中的参数传递...

    java教程课件5基本数据类型与引用类型

    Java教程中的核心知识点主要涉及Java的基本数据类型、引用类型、常用包、对象的内存分配以及字符串操作。下面将对这些内容进行详细的阐述。 1. **基本数据类型与引用类型** - Java的基本数据类型包括布尔型`...

    java值传递与引用传递

    2. 引用类型看似按引用传递,实际上传递的是对象引用的副本,因此函数可以影响实际对象的值。 3. `String`虽然是引用类型,但由于其不可变性,传递后的修改不会影响原始字符串。 理解这些概念有助于避免在编程中...

    java 值传递和引用传递的比较

    在Java编程语言中,了解值传递和引用传递的概念至关重要,因为它们直接影响到函数参数的处理方式。下面将详细探讨这两个概念及其区别。 首先,我们来理解什么是值传递。在Java中,基本数据类型(如int、double、...

    Java引用类型编程开发技术共13页.pdf.zip

    以上知识点涵盖了Java引用类型的基础概念和高级特性。在实际开发中,理解并熟练运用这些知识可以编写出高效、安全的Java代码。如果想要深入学习,可以查阅这13页的PDF文档,它可能会提供更具体的应用示例和实战技巧...

    JAVA数据类型与Hibernate的类型映射

    在Java编程语言中,数据类型分为两种:基本数据类型(如int、char)和引用数据类型(如类、接口、数组)。而在Java持久化框架Hibernate中,这些数据类型需要与数据库中的字段类型进行映射,以便正确地存储和检索数据...

    6.java引用类型.zip

    多态性是Java引用类型的一个关键特性,允许你用父类引用指向子类对象。这使得代码更加灵活,能够处理不同类型的对象。例如: ```java Animal animal = new Dog(); // 父类引用指向子类实例 animal.eat(); // 调用...

    JAVA中值类型和引用类型的区别

    在Java编程语言中,值类型和引用类型是两种基本的数据类型分类,它们在内存管理和数据处理上有显著的差异。理解这些区别对于编写高效且无错误的代码至关重要。 **值类型**,也称为基本数据类型,包括四类共八种:...

    引用类型传值方法

    在编程领域,我们经常需要处理各种数据类型,其中包括值类型(value types)和引用类型(reference types)。值类型如整型、浮点型、布尔型等,它们在赋值或作为参数传递时会进行副本复制,而引用类型则有所不同。本...

    Java中数据类型和MYSQL中数据类型的对比

    首先,Java 有两类数据类型:基本数据类型(如 int、char、float)和引用数据类型(如类、接口、数组)。基本数据类型是预定义的,它们的大小和值的范围是固定的。例如,`int` 在 Java 中占用 4 个字节,可存储 -2^...

    JAVA原始类型和引用类型有哪些区别?[定义].pdf

    Java编程语言中有两种基本的数据类型:原始...了解原始类型和引用类型之间的这些差异是编写高效、无错误的Java代码的关键。在设计和实现程序时,应根据具体需求和场景选择合适的数据类型,以确保代码的性能和正确性。

    JAVA数据类型思维导图

    本资源“JAVA数据类型思维导图”提供了一种直观的方式来理解和记忆Java中的数据类型,包括基本数据类型和引用数据类型。下面将对这两个主要的数据类型进行详细解释。 1. 基本数据类型(Primitive Data Types) - ...

    JAVA数据类型和运算符

    在Java中,数据类型和运算符是编程的基础,它们定义了程序中的变量、常量以及它们之间的操作。 一、标识符与关键字 1. 标识符是程序员在编程时用于命名常量、变量、方法、类、接口和包的符号。它们必须遵循以下规则...

    抽象类与接口的区别-java中值类型和引用类型的区别.docx

    ### 抽象类与接口的区别 #### 概述 在Java编程语言中,抽象类(Abstract Class)与...- **值类型和引用类型的主要区别**在于它们的存储位置、实例化方式及性能表现。选择合适的数据类型对程序的性能优化至关重要。

    java引用 对象 属性等数据类型知识

    在Java编程语言中,理解和掌握引用、对象以及属性等数据类型知识是至关重要的。这些概念构成了Java程序的基础,让我们深入探讨一下。 1. 引用(References): 在Java中,引用是一种变量,它存储了对象在内存中的...

    Java数值类型

    在Java中,数据类型分为两大类:基本数据类型和引用数据类型。其中,基本数据类型又进一步分为数值类型、字符型和布尔型。 #### 二、Java数值类型 ##### 1. 取值范围 Java中的数值类型主要包括整数型和浮点型。每...

Global site tag (gtag.js) - Google Analytics