在Java中,我们看到许多final可以使用关键字的地方,但它的使用并不常见.
例如:
String str = "abc";
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)
在上述情况下,str可以final但通常不会这样做.
当一个方法永远不会被覆盖时,我们可以使用final关键字.类似地,如果一个类不会被继承.
在任何或所有这些案例中使用final关键字是否真的能提高性能?如果是这样,那怎么样?请解释.如果正确使用final真正重要的性能,应在Java程序员开发什么习惯,使关键字的最好用?
所以标题说明了一切.我的内部出现了编译错误onClick.
这是代码.
public class fieldsActivity extends Activity {
Button addSiteButton;
Button cancelButton;
Button signInButton;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// to create a custom title bar for activity window
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.fields);
// use custom layout title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);
Pager adapter = new Pager();
ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);
mPager.setAdapter(adapter);
mPager.setCurrentItem(1);
addSiteButton = (Button) findViewById(R.id.addSiteButton);
addSiteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(2, true); …Run Code Online (Sandbox Code Playgroud) 我看过很多人写的代码public static final String mystring = ...
然后才使用一个值.
他们为什么要那样做?为什么他们必须final在使用之前初始化值?
UPDATE
好的,感谢所有的答案,我理解这些关键的意义(公共静态最终).我不明白为什么人们使用它,即使常量只在一个地方使用而且只在同一个类中使用.为什么宣布它?为什么我们不只是使用变量?
我发现defs是圆形的,主语是由他们的动词定义的,但动词是未定义的!那么你如何定义它们呢?
通函定义
初始化:初始化变量.它可以在声明时完成.
赋值:为变量赋值.它可以在任何地方完成,只有最终标识符一次.
声明:向变量声明值.
[更新,尝试用lambda calc理解主题]
D(x type) = (?x.x is declared with type)
A(y D(x type)) = (?y.y is assigned to D(x type))
%Then after some beta reductions we get initialization.
D(x type) me human // "me" declared with type "human"
A(y (D(x type) me human)) asking // "asking" assigned to the last declaration
%if the last two statemets are valid, an initialization exists. Right?
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试了解有关最终字段的 JLS 部分。
为了更好地理解 JLS 中的文本,我还在阅读Jeremy Manson(JMM 的创建者之一)撰写的The Java Memory Model。
该论文包含让我感兴趣的示例:如果o具有 final 字段的对象对另一个线程可见t两次:
o的构造函数完成之前首先“不正确地”o的构造函数完成后的下一个“正确”然后即使仅通过“正确”发布的路径访问它,也t可以看到半构造的o。
这是论文中的部分:
图 7.3:简单最终语义示例
f1 是最后一个字段;它的默认值为 0
主题 1 主题 2 主题 3 Run Code Online (Sandbox Code Playgroud)o.f1 = 42; p = o; freeze o.f1; q = o; Run Code Online (Sandbox Code Playgroud)r1 = p; i = r1.f1; r2 = q; if (r2 == r1) k = r2.f1; Run Code Online (Sandbox Code Playgroud)r3 = q; j = r3.f1;我们假设 r1、r2 和 …
嗨,我刚开始学习android开发,我正在尝试构建一个从文件中读取文本的应用程序.我一直在网上搜索,但我似乎没有找到这样做的方式,所以我有几个问题..
1.怎么做?在android中逐行读取文件的首选方法是什么?
2.我应该在哪里存储文件?它应该在raw文件夹中还是在assets文件夹中?
所以这就是我已经尝试过的:"(我认为问题可能在于找到文件..)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filereader);
try {
// open the file for reading
InputStream fis = new FileInputStream("text.txt");
// if file the available for reading
if (fis != null) {
// prepare the file for reading
InputStreamReader chapterReader = new InputStreamReader(fis);
BufferedReader buffreader = new BufferedReader(chapterReader);
String line;
// read every line of the file into the line-variable, on line at the time
do {
line = buffreader.readLine();
// do something with the …Run Code Online (Sandbox Code Playgroud) 我知道Oracle Java命名约定,并且我已经阅读了类似的SO问题(比如Java常量变量,命名约定)但是我应该使用什么命名来实现常量函数?
例如,如果我有功能界面
public interface StringDecider {
public boolean decide(String str);
}
Run Code Online (Sandbox Code Playgroud)
现在我用它来定义一个常量函数.命名应该是"上蛇案",如
public final static StringDecider STRING_NOT_EMPTY = (str) -> (str!=null && !str.isEmpty());
STRING_NOT_EMPTY.decide("Example");
Run Code Online (Sandbox Code Playgroud)
或骆驼案
public final static StringDecider stringNotEmpty = (str) -> (str!=null && !str.isEmpty());
stringNotEmpty.decide("Example");
Run Code Online (Sandbox Code Playgroud) 这怎么可能?我怎样才能更改标记为final的变量?
public class Main
{
public static void main(String[] args)
{
final int NUM;
NUM = 22;
NUM = 33;
System.out.println(NUM);
}
}
Run Code Online (Sandbox Code Playgroud)
我在Android中使用AIDE应用程序...它已成功编译并打印33.
我对函数式语言和闭包很满意,并对以下错误感到惊讶:"不能引用封闭范围中定义的非最终局部变量邀请".
这是我的代码:
Session dbSession = HibernateUtil.getSessionFactory().openSession();
Transaction dbTransaction = dbSession.beginTransaction();
Criteria criteria = dbSession.createCriteria(Invite.class).add(Restrictions.eq("uuid", path).ignoreCase());
Invite invite = (Invite) criteria.uniqueResult();
if (invite.isExpired()) {
// Notify user the invite has expired.
} else {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// ERROR: `invite` is not guaranteed to exist when this code runs
invite.setExpired(true);
}
}, MAX_TIME);
}
Run Code Online (Sandbox Code Playgroud)
据我了解,invite在TimeTask实例中引用是一个错误,因为不保证该变量存在.所以我的问题是,表达我想要的Java的方式是什么,即加载一个邀请,然后设置一个计时器,在一段时间后使邀请过期.