网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java无敌代码,无敌的代码

在Java语言中 String str=new String("a") 这个语句创建了几个对象。

答案:两个,一个是字符串字面量"xyz"所对应的、驻留(intern)在一个全局共享的字符串常量池中的实例,另一个是通过new String(String)创建并初始化的、内容与"xyz"相同的实例

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名雅安服务器托管、营销软件、网站建设、柳北网站维护、网站推广。

这是根据Java语言规范相关规定可以给出的合理答案。考虑到Java语言规范中明确指出了:

The Java Language Specification, Third Edition 写道

The Java programming language is normally compiled to the bytecoded instruction set and binary format defined in The Java Virtual Machine Specification, Second Edition (Addison-Wesley, 1999).

也就是规定了Java语言一般是编译为Java虚拟机规范所定义的Class文件,但并没有规定“一定”(must),留有不使用JVM来实现Java语言的余地。

考虑上Java虚拟机规范,确实在这段代码里涉及的常量种类为CONSTANT_String_info的字符串常量也只有"xyz"一个。CONSTANT_String_info是用来表示Java语言中String类型的常量表达式的值(包括字符串字面量)用的常量种类,只在这个层面上考虑的话,这个解答也没问题。

所以这种解答可以认为是合理的。

值得注意的是问题中“在运行时”既包括类加载阶段,也包括这个代码片段自身执行的时候。下文会再讨论这个细节与楼主原本给出的问题的关系。

碰到这种问题首先应该想到去查阅相关的规范,这里具体是Java语言规范与Java虚拟机规范,以及一些相关API的JavaDoc。很多人喜欢把“按道理说”当作口头禅,规范就是用来定义各种“道理”的——“为什么XXX是YYY的意思?”“因为规范里是这样定义的!”——无敌了。

在Java虚拟机规范中相关的定义有下面一些:

The Java Virtual Machine Specification, Second Edition 写道

2.3 Literals

A literal is the source code representation of a value of a primitive type (§2.4.1), the String type (§2.4.8), or the null type (§2.4). String literals and, more generally, strings that are the values of constant expressions are "interned" so as to share unique instances, using the method String.intern.

The null type has one value, the null reference, denoted by the literal null. The boolean type has two values, denoted by the literals true and false.

2.4.8 The Class String

Instances of class String represent sequences of Unicode characters (§2.1). A String object has a constant, unchanging value. String literals (§2.3) are references to instances of class String.

2.17.6 Creation of New Class Instances

A new class instance is explicitly created when one of the following situations occurs:

Evaluation of a class instance creation expression creates a new instance of the class whose name appears in the expression.

Invocation of the newInstance method of class Class creates a new instance of the class represented by the Class object for which the method was invoked.

A new class instance may be implicitly created in the following situations:

Loading of a class or interface that contains a String literal may create a new String object (§2.4.8) to represent that literal. This may not occur if the a String object has already been created to represent a previous occurrence of that literal, or if the String.intern method has been invoked on a String object representing the same string as the literal.

Execution of a string concatenation operator that is not part of a constant expression sometimes creates a new String object to represent the result. String concatenation operators may also create temporary wrapper objects for a value of a primitive type (§2.4.1).

Each of these situations identifies a particular constructor to be called with specified arguments (possibly none) as part of the class instance creation process.

5.1 The Runtime Constant Pool

...

● A string literal (§2.3) is derived from a CONSTANT_String_info structure (§4.4.3) in the binary representation of a class or interface. The CONSTANT_String_info structure gives the sequence of Unicode characters constituting the string literal.

● The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String. In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus,

Java代码

("a" + "b" + "c").intern() == "abc"

must have the value true.

● To derive a string literal, the Java virtual machine examines the sequence of characters given by the CONSTANT_String_info structure.

○ If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.

○ Otherwise, a new instance of class String is created containing the sequence of Unicode characters given by the CONSTANT_String_info structure; that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.

...

The remaining structures in the constant_pool table of the binary representation of a class or interface, the CONSTANT_NameAndType_info (§4.4.6) and CONSTANT_Utf8_info (§4.4.7) structures are only used indirectly when deriving symbolic references to classes, interfaces, methods, and fields, and when deriving string literals.

把Sun的JDK看作参考实现(reference implementation, RI),其中String.intern()的JavaDoc为:

JavaDoc 写道

public String intern()

Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Returns:

a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

===============================================================

再换一个问题来问:

引用

问题:

Java代码

String s = new String("xyz");

涉及用户声明的几个String类型的变量?

答案也很简单:

引用

答案:一个,就是String s。

把问题换成下面这个版本,答案也一样:

引用

问题:

Java代码

String s = null;

涉及用户声明的几个String类型的变量?

Java里变量就是变量,引用类型的变量只是对某个对象实例或者null的引用,不是实例本身。声明变量的个数跟创建实例的个数没有必然关系,像是说:

Java代码

String s1 = "a";

String s2 = s1.concat("");

String s3 = null;

new String(s1);

这段代码会涉及3个String类型的变量,

1、s1,指向下面String实例的1

2、s2,指向与s1相同

3、s3,值为null,不指向任何实例

以及3个String实例,

1、"a"字面量对应的驻留的字符串常量的String实例

2、""字面量对应的驻留的字符串常量的String实例

(String.concat()是个有趣的方法,当发现传入的参数是空字符串时会返回this,所以这里不会额外创建新的String实例)

3、通过new String(String)创建的新String实例;没有任何变量指向它。

===============================================================

回到楼主开头引用的问题与“标准答案”

引用

问题:

Java代码

String s = new String("xyz");

创建了几个String Object?

答案:两个(一个是“xyz”,一个是指向“xyz”的引用对象s)

用归谬法论证。假定问题问的是“在执行这段代码片段时创建了几个String实例”。如果“标准答案”是正确的,那么下面的代码片段在执行时就应该创建4个String实例了:

Java代码

String s1 = new String("xyz");

String s2 = new String("xyz");

马上就会有人跳出来说上下两个"xyz"字面量都是引用了同一个String对象,所以不应该是创建了4个对象。

那么应该是多少个?

运行时的类加载过程与实际执行某个代码片段,两者必须分开讨论才有那么点意义。

为了执行问题中的代码片段,其所在的类必然要先被加载,而且同一个类最多只会被加载一次(要注意对JVM来说“同一个类”并不是类的全限定名相同就足够了,而是类全限定名, 定义类加载器一对都相同才行)。

根据上文引用的规范的内容,符合规范的JVM实现应该在类加载的过程中创建并驻留一个String实例作为常量来对应"xyz"字面量;具体是在类加载的resolve阶段进行的。这个常量是全局共享的,只在先前尚未有内容相同的字符串驻留过的前提下才需要创建新的String实例。

我的世界整人指令是什么?

我的世界恶作剧整人指令如下:

1、指令 /nick ID-名称-给某人改一个名,ID是加白名单的名字。

2、/backup-运行备份命令。

3、/fireball-发射一个火球(会破坏方块)。

4、/heal name-治疗某人。

5、/invsee name-查看某人的包裹。

6、/lingtning name-天谴(不填写名字会雷击鼠标指向的地方)。

7、/mute name-禁言某人。

8、/ping-让单位走路发出乒乓声。

9、/seen name-查看某人最后退出的时间。

10、/tp name1 name2-将人物1传送至人物2身旁。

11、/tp name-将自己传送至某人身旁。

12、/tphere name-将某人传送至自己身旁。

13、/tpall-将服务器内的所有人传送至自己身旁(大传送阵)。

求我的世界java指令全套

ascend - 把自己提升到上一个平台

bind 命令 {命令关键字} - 设置一键命令

clear - 清空控制台

damage - 关闭或者开启伤害 即无敌

descend - 把自己移动到下面一个的平台

destroy [all] - 破坏当前的东西(背包)

defuse [all] - 拆弹(拆除已经点燃了的TNT炸药)

diff - X

difficulty - 设置游戏难度

dropstore - 在身边创建一个储物柜

*drops - 开关物品掉落,关闭的话采矿打怪不掉东西。

dupe [all] - 复制东西

duplicate [all] - 复制手上的东西并丢出来

explode [范围] - 设置一个地方爆炸(在自家慎用)

extinguish [all] - 熄灭周围所有的火

ext [all] - 一样是熄灭火

falldamage - 开关高空落下伤害

firedamage - 开关火的伤害

fly - 飞行模式

*freeze - 冻结怪物

give 物品 [数量] - 给一样物品

goto 名字 - 去一个地方

grow [all] - 让立即小麦成长

h [COMMAND] - 命令列表/帮助

heal - 补指定的血

health - 设置生命值

help [COMMAND] - 命令列表/帮助

home 回到出生点

i 物品代码 [数量] - 刷东西

instantmine - 开关即时采矿(采矿无延迟)

item 物品代码|物品名称 [数量] [费用] 给玩家物品, 如果不指定则是最大的数量

itemname - 显示当前手上的物品名称

itemstack 物品代码 [数量] - 给玩家指定数量的物品

kill 自杀不解释

jump - 瞬移到鼠标所指的地方

killnpc [all] - 杀死周围全部NPC 或者叫 杀了附近所有除自己外的活体生物

*light - 把光永久性关闭

listwaypoints - 列出所有路径点

macro 文件名 {参数} - 允许运行宏

maxstack [物品ID|物品名称|全部] [数量] - 最大的把某物品堆起来

*mobdamage - 怪物不会给你伤害

msg 消息 - 添加一个消息到控制台

music [音量] - 播放音乐

noclip - 穿墙

p - 显示当前坐标

pos 现在玩家的坐标

reach - 玩家到指定地方

return - 传送到之前传送的地方

rem - 删除指定路点

removedrops [all] - 删掉地上物品

*rename - 修改命令名称

replenish [all] - X

repair [all] - 修复当前物品耐久

reset - 恢复默认设置

s 名字 - Same as /set

search 关键词 - 搜索物品名称

set 名字 - 在这世界标记一个路径点

setjump [JUMP|reset] - 设置跳跃的高度 落地伤害和移动 1:1

setspawn [ ] 设置当前位置 X轴 Y轴 Z轴

setspeed [速度|重置] - 设置移动速度

spawn [QTY] - 产生一个生物

spawnstack {NAME|ID|random} - 产生一个合体的怪物NPC

*superheat [all] - Turns items which are furnace-able into their furnaced form

t - Same as /tele

tele - 传送到此坐标

time [set|get|day|night [minute|hour|day [TIME]]] - 设置指定时间得到物品

timeschedule - 设定一段时间段,让世界永远保持在这段时间之间

unbind - 解除一个命令

useportal - 传送到地狱

waterdamage - 开关潜水伤害

Java 数据结构问题

acm上曾经做过一个类似的段子。记不清了,大概想一下。

这里为什么是place=x+y*10000;呢,是你随便写的还是与生产实际相关。如果是place=x*y;就显而易见了。

下面链接是完全二叉树的java实现,网上找的,我也没细看。

你这个问题,我说一个“创新”点的想法,哈哈,或许能激发你的思考。

先说逻辑上:

可以用一个二维数组,如A[][],每个元素存放A类的对象,如何定位呢,就根据x、y的值,正好是一对坐标,在二维上标定了一个位置,这个位置就放该A类对象。放心,这个数组不会占用太多的内存,几MB顶多了,这也是我看你说明了x、y的范围得到的启发,这样,创建数组时就可以指定大小。查找时,直观些的方法就是扫描,你找x最大的,那就顺着x轴从右往左扫,扫到的第一个A类对象就是目标。扫4个方向你需要的4个极值就都出来了。

数据结构上:

当然你就用数组这种结构就ok的,如果你按照图去处理,也可以用邻接矩阵或者邻接链表,从本质上说,就是你人为地对二维数组进行了优化,强化了“扫描”(查找)过程的效率,或者你自己稍微写个高效些的针对你这个项目的查找算法。

多说一句,如果place=x*y;的话,明显就是面积,扫描过之后就会圈出一个区域来,是一个图,然后对图再进行操作,会很容易解决后续的问题(你不可能仅找到4个极值就结束了吧)。

增删节点的话,就把该位置上的A对象赋空就可以了,相当于删除,新增则相反。然后重新启动扫描算法。

用java编写个程序并注释

import java.util.Date;

/**

* 名字:XX你是帅哥!

* 作用:突出版主很帅

* @author Administrator

*

*/

public class ShuaiG {

//姓名

private String name;

//拍马匹用的华丽语言

private String sName;

//拍马匹的时间

private Date date;

//获得拍马匹的人的姓名

public String getName() {

return name;

}

//设置拍马匹的人的姓名

public void setName(String name) {

this.name = name;

}

//设置拍马匹用的华丽语言

public String getSName() {

return sName;

}

//获得拍马匹用的华丽语言

public void setSName(String name) {

sName = name;

}

//获得拍马匹的时间

public Date getDate() {

return date;

}

//设置拍马匹的时间

public void setDate(Date date) {

this.date = date;

}

/**

* 程序主方法,用来设置和获得你的操作并输出结果

* @param args

*/

public static void main(String[] args) {

//创建一个帅哥去给老板拍马匹

ShuaiG shuaiG=new ShuaiG();

//拍马屁对象的名字叫:版主

shuaiG.setName("版主");

//设置你要对版主说的话:你帅呆了简直天下无敌:

shuaiG.setSName("你帅呆了简直天下无敌");

//指定什么时候对版主说这些话

shuaiG.setDate(new Date());

//现在开始对版主说:版主你帅呆了简直天下无敌

System.out.println(shuaiG.getName()+shuaiG.getSName());

//你说出这句话的准确时间

System.out.println(shuaiG.getDate());

}

}

java自学一周基本无敌了

首先可以看谭浩强的书,掌握基本的数据类型,类,对象,判断循环什么的,这个过程如果是自学,短则一周,长则一个月。并且要不断练习编写小程序,比如什么输出九九表啊,图形啊什么的。这个过程相当于学语文的学语法一样。

然后是看《Java核心技术》,学习常用的系统提供的方法,这个过程中你掌握的方法越多,往后学习起来越容易,并且要不断练习编写中、小程序,Java核心技术里的源代码特别多,比别的教材要好很多,而且工作中甚至都能用到那些源代码。还要大量阅读Java API文档,相当于学语文的时候翻字典一样。大概要半年时间。

然后是看《设计模式》,这个时候已经可以写一些稍稍大一点的程序了,比如俄罗斯方块啊,象棋五子棋啊之类的。这个过程相当于学语文的时候了解起承转合,了解各种修辞。大概要几个月吧。

然后是使用各种常用工具,Eclipse是最常用的Java开发工具,当然还有数据库,Java是用来编程的,但是一般的项目中,数据需要数据库来存储。好的Java程序结构需要使用struts,hibernate等工具,这些工具实际项目中使用,边用边学,不消耗多少时间。

当然,有人带会快些,没人带会慢些。前三个阶段,写代码多,则半年左右,甚至几个月。写代码少,则一年多甚至两年。

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!

vaela


文章标题:java无敌代码,无敌的代码
分享网址:http://bjjierui.cn/article/hcphhd.html

其他资讯