how to implement C# ref in Java

In C# we have ref and out, while in Java we don‘t.

To do something similar to ref, there are normally 4 ways.

Check this out: http://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java

Choice 1: make a public member variable in a class

Choice 2: return the value instead of pass by reference

Choice 3: make it a class or static variable

Choice 4: Create a single element array of type int and pass that

时间: 2024-09-30 15:19:05

how to implement C# ref in Java的相关文章

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If

[LeetCode] 232. Implement Queue using Stacks Java

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

[LeetCode] 225. Implement Stack using Queues Java

题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use on

232. Implement Queue using Stacks Java Solutions

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

[LeetCode] 208. Implement Trie (Prefix Tree) Java

题目: Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 题意及分析:实现一个字典树或者叫前缀树的插入.删除和startsWith判断.这里定义一个trieNode类作为字典树的节点类.然后有一个chilrden数组保存子节点,一个isWord变量来判断从根节点到该节点是否是一

LeetCode232 Implement Queue using Stacks Java 题解

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

深入探讨 java.lang.ref 包--转

概述 Java.lang.ref 是 Java 类库中比较特殊的一个包,它提供了与 Java 垃圾回收器密切相关的引用类.这些引用类对象可以指向其它对象,但它们不同于一般的引用,因为它们的存在并不防碍 Java 垃圾回收器对它们所指向的对象进行回收.其好处就在于使者可以保持对使用对象的引用,同时 JVM 依然可以在内存不够用的时候对使用对象进行回收.因此这个包在用来实现与缓存相关的应用时特别有用.同时该包也提供了在对象的“可达”性发生改变时,进行提醒的机制.本文通过对该包进行由浅入深的介绍与分析

序列化之protobuf与avro对比(Java)

最近在做socket通信中用到了关于序列化工具选型的问题,在调研过程中开始趋向于用protobuf,可以省去了编解码的过程.能够实现快速开发,且只需要维护一份协议文件即可. 但是调研过程中发现了protobuf的一些弊端,比如需要生成相应的文件类,和业务绑定太紧密,所以在看了AVRO之后发现它完美解决了这个问题. 下面记录下对这两种序列化工具的入门与测评. 一.protobuf基本操作 protobuf简介: Protocol Buffers (a.k.a., protobuf) are Goo

Java review-basic2

1.Implement a thread-safe (blocking) queue: Class Producer implements Runable{ Private final BlockingQueue queue; Producer (BlockingQueue q){queue=q;} Public void run(){ try{ while(true){queue.put(produce()); }catch(InterruptedException ex){…handle…}