java.equal例子_Java中的== 和equals()方法详解与实例

Java中的== 和equals()方法:

Java中的数据类型,可分为两类:

1.基本数据类型,也称原始数据类型。

byte,short,char,int,long,float,double,boolean,他们之间的比较,应用双等号(==),比较的是他们的值。

2.引用数据类型(类)

当它们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false。

Java当中所有的类都是继承于Object这个基类的,在Object中的基类中定义了一个equals()的方法,这个方法的初始行为是比较对象的内存地址,但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址。

对于引用数据类型之间进行equals比较,在没有覆写equals方法的情况下,他们之间的比较还是基于他们在内存中的存放位置的地址值的,因为Object的equals方法也是用双等号(==)进行比较的,所以比较后的结果跟双等号(==)的结果相同。

Object中的equals()方法

publicbooleanequals(Objectobj){

return(this==obj);

}

举个实例:

packageorg.java.test;

publicclassPerson{

privateintage;

privateStringname;

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicPerson(intage,Stringname){

this.age=age;

this.name=name;

}

publicPerson(){

}

@Override

publicinthashCode(){

finalintprime=31;

intresult=1;

result=prime*result+age;

result=prime*result+((name==null)?0:name.hashCode());

returnresult;

}

@Override

publicbooleanequals(Objectobj){

if(this==obj)

returntrue;

if(obj==null)

returnfalse;

if(getClass()!=obj.getClass())

returnfalse;

Personother=(Person)obj;

if(age!=other.age)

returnfalse;

if(name==null){

if(other.name!=null)

returnfalse;

}elseif(!name.equals(other.name))

returnfalse;

returntrue;

}

}

MainTest.java[java]view plaincopyprint?

packageorg.java.test;

publicclassMainTest{

publicstaticvoidmain(String[]args){

Personp1=newPerson(99,"A");

Personp2=newPerson(99,"A");

Personp3=p1;

System.out.println(p1==p2);//false

System.out.println(p1==p3);//true

System.out.println(p1.equals(p2));

//未重写equals()方法,返回false

//重写equals()方法后,比较的是内容,返回true;

System.out.println("<====================>");

Strings1="hello";

Strings2="hello";

Strings3=newString("hello");

System.out.println(s1==s2);//true

System.out.println(s1==s3);//false

System.out.println(s1.equals(s2));//true

System.out.println(s1.equals(s3));//true

}

}

希望本文对各位朋友有所帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import time import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from tensorflow.examples.tutorials.mnist import input_data import mnist_inference import mnist_train tf.compat.v1.reset_default_graph() EVAL_INTERVAL_SECS = 10 def evaluate(mnist): with tf.Graph().as_default() as g: #定义输入与输出的格式 x = tf.compat.v1.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.compat.v1.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels} #直接调用封装好的函数来计算前向传播的结果 y = mnist_inference.inference(x, None) #计算正确率 correcgt_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correcgt_prediction, tf.float32)) #通过变量重命名的方式加载模型 variable_averages = tf.train.ExponentialMovingAverage(0.99) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) #每隔10秒调用一次计算正确率的过程以检测训练过程正确率的变化 while True: with tf.compat.v1.Session() as sess: ckpt = tf.train.get_checkpoint_state(minist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: #load the model saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training steps, validation accuracy = %g" % (global_step, accuracy_score)) else: print('No checkpoint file found') return time.sleep(EVAL_INTERVAL_SECS) def main(argv=None): mnist = input_data.read_data_sets(r"D:\Anaconda123\Lib\site-packages\tensorboard\mnist", one_hot=True) evaluate(mnist) if __name__ == '__main__': tf.compat.v1.app.run()对代码进行改进
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值