java 值與引用

2021-05-24 02:10:44 字數 1903 閱讀 7648

package com.lining;

public class test {

public void play(animal a){

system.out.println("動物年齡為:"+a.age);

public static void main(string args){

cat cat=new cat(12,45);

cat cat2=new cat(12,45);

system.out.println("值為:"+cat.equals(cat2));

system.out.println("值為:"+(cat==cat2));

string str="lining";

string str2="lining";

system.out.println("值為:"+str.equals(str2));

system.out.println("值為:"+(str==str2));

dog dog=new dog(23,45);

animal ani=new animal(10);

system.out.println("cat is or isn't a animal ?"+(cat instanceof animal));

system.out.println("dog is or isn't a animal ?"+(dog instanceof animal));

system.out.println("ani is or isn't a animal ?"+(ani instanceof animal));

system.out.println("ani is or isn't a cat ?"+(ani instanceof cat));

test t=new test();

t.play(ani);

t.play(cat);

t.play(dog);

animal a=(animal)cat;

system.out.println("a is or isn't a cat ?"+(a instanceof cat));

class animal{

int age;

animal(int a){

this.age=a;

class cat extends animal{

int age;

int weight;

cat(int a,int w){

super(a);

this.age=a;

this.weight=w;

public boolean equals(object obj){

if(obj==null)

return false;

else if(obj instanceof cat){

return true;

else

return false;

class dog extends animal{

int age;

int weight;

dog(int a,int w)

super(a);

this.age=a;

this.weight=w;

值為:true

值為:false

值為:true

值為:true

cat is or isn't a animal ?true

dog is or isn't a animal ?true

ani is or isn't a animal ?true

ani is or isn't a cat ?false

動物年齡為:10

動物年齡為:12

動物年齡為:23

a is or isn't a cat ?true

左值引用與右值引用

引用的本質是乙個閹割的指標,它是乙個物件的別名,通過操作符 來實現,c 11又提出了乙個左值引用與右值引用的概念。這裡提一下變數的兩個屬性 左值和右值,左值是變數的位址,右值是變數儲存的內容,變數的本質即儲存空間的名稱,編譯後為對應的位址。左值引用 乙個c 引用宣告後必須被初始化,否則會編譯不過,初...

java中的值傳遞與引用傳遞

1.說明 原始型別 int,long等 和包裝類 integer,long等 進行引數傳遞時是值傳遞,所有函式體內針對引數的操作,都是對於拷貝的操作,不會改變本體。類和容器 list,set等 作為引數時傳的是引用的乙個副本 注意 不是引用本身。區別於c 2.例項 1 以下為原始型別和包裝類作為引數...

java傳值與傳引用問題總結

1.基本型別與引用型別區別 對於基本型別 int long short double char byte float boolean 變數直接儲存資料的值 對於引用型別,變數儲存的是物件的位址,由此位址指向實際物件。如圖 對於基本型別,直接傳值,方法裡進行重新賦值不會導致原值改變 public cl...