java String的比较,BOX装箱拆箱,以及面向对象的小代码
package cn.hncu.day2;
public class StringDemo {
public static void main(String[] args) {
String str1 = "abc";//直接赋值,放在栈中,数据共享
String str2 = "abc";
System.out.println("str1==str2: "+(str1==str2));//true
String str3 = new String("abc");//new的东西是在堆中开的,每次都新开空间,因此内存地址是不一样的
String str4 = new String("abc");
System.out.println("str3==str4: "+(str3==str4));//false,"=="是判断栈中的值是否相同
System.out.println("str1==str4: "+(str1==str4));
System.out.println("str1.equals(str4): "+str1.equals(str4));
//综上,以后我们做项目时,判断字符串变量是否相等,一定要用equals()方法
}
}
------------------------------------------------------------------------------------------------------------
package cn.hncu.day2;
public class BoxDemo {
public static void main(String[] args) {
//demo1();
demo2();
}
private static void demo2(){
Integer i1 = 100;
Integer i2 = 100;
System.out.println("i1==i2: "+(i1==i2));
Integer i3 = 200;
Integer i4 = 200;
System.out.println("i3==i4: "+(i3==i4));
int a=100;
int b=100;
System.out.println(a==b);
}
private static void demo1() {
Integer a1 = new Integer(5);
System.out.println(a1);
//自动装箱
Integer a2 = 15;
System.out.println(a2);
//自动拆箱
int x = new Integer(10);
System.out.println(x);
//混合使用
int sum = a2+x;
System.out.println("sum="+sum);
}
}
--------------------------------------------------------------------------------
package cn.hncu.day2;
public class Ball {
private double height;
private int downTimes;
private int jumpTimes;
private double sum;
public Ball(double height) {
this.height = height;
}
public void jump(){
height = height/2;
jumpTimes++;
sum = sum + height;
}
public void fall(){
downTimes++;
sum = sum + height;
}
public static void main(String[] args) {
//第10次落地时,共经过多少米?
Ball ball = new Ball(100);
while(true){
ball.fall();
if(ball.downTimes==10){
System.out.println(ball.sum);
break;
}
ball.jump();
}
//第10次反弹多高?
Ball ball2 = new Ball(100);
while(true){
ball2.fall();
ball2.jump();
if(ball2.jumpTimes==10){
System.out.println(ball2.height);
break;
}
}
System.out.println("-------------");
test2();
}
//用面向过程的方式求解
private static void test2() {
double sum =0;
double begin=100;
double fanTan=0;
for(int i=0; i<10; i++ ){
if(i==9){//第10次落地
sum = sum + begin;
fanTan = begin/2;
}else{
sum = sum + begin + begin/2;
}
begin = begin/2;
}
System.out.println(sum+","+fanTan);
}
}
java String的比较,BOX装箱拆箱,以及面向对象的小代码的更多相关文章
- java中的包装类与装箱拆箱定义
JAVA 中int类型转String类型的通常方法,有三种: 1.String.valueOf(int i) 2.Integer.toString(int i) 3.i+"" ...
- java中Integer与int装箱拆箱一点收获
示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...
- Java 性能要点:自动装箱/ 拆箱 (Autoboxing / Unboxing)
[编者按]本文作者为 Ali Kemal TASCI,最早于2016年4月9日发布于DZONE社区.文章主要介绍通过改进 Java 1.5 就已存在的骨灰级特性大幅度提高应用性能. 本文系 OneAP ...
- JDK5.0新特性(静态导入、自动装箱/拆箱、增强for循环、可变参数、枚举、泛形)
JDK5中新增了很多新的java特性,利用这些新语法可以帮助开发人员编写出更加高效.清晰,安全的代码. 这些新特性主要有:1.静态导入2.自动装箱/拆箱3.增强for循环4.可变参数5.枚举6.泛型7 ...
- Java中的装箱拆箱
一) 装箱与拆箱 Java中有概念是一切皆对象,因为所有的类都默认继承自Object.但是,对于数据类型是个例外,如short,int,long,float,double, byte,char,bo ...
- Java 装箱 拆箱
Java 自动装箱与拆箱 ??什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我 ...
- Java 的自动装箱拆箱
Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类.以下是基本数据类型对应的包装类: 基本数据类型 包装类 byte(1字节) Byte short(2字节) Short int ...
- Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口
Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...
- Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?
参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...
随机推荐
- Discuz! 7.2 SQL注入exp
已经有人写出一些工具了,但是感觉不怎么好用,就自己写了个. 参数:1.可直接getshell2.爆管理账号密码3.爆表前缀如果表前缀不是默认的cdb_ 只需更改代码中的 $table即可,方便快捷. ...
- 【BZOJ 2693】jzptab(莫比乌斯+分块)
2693: jzptab Description Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 表示N.M Output T行 每行一个整数 表示第i组数据的结果 Sample I ...
- 同一個Loader對象傳入不同參數時,从数据库中查询的結果每次都一樣
發現問題: LoaderManager().initLoader()方法調用時會根據第一個參數ID去判斷是否已經存在一個Loader加載器,如果存在則複 用,不存在則建一個新的加載器.由於我第一次已經 ...
- Android用户界面 UI组件--ImageView及其子类ImageButton,QuickContactBadge附带Draw9Patch工具说明
1.ImageView 常用属性: android:src 设置可绘制对象作为 ImageView 显示的内容 android:cropToPadding 如果设置为true,图片裁剪到保留该Imag ...
- java常量使用比较好的方法
1.首先建立一个工具类 public class AppConst { private static Map<String,String> map=new HashMap<Strin ...
- RMA Sales Order – Stuck with “Awaiting Return Disposition”
RMA Sales Order – Stuck with "Awaiting Return Disposition" Action : (P ...
- LA_3263_That_Nice_Euler_Circuits_(欧拉定理+计算几何基础)
描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=15& ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- ☀【canvas】直线 / 三角形 / 矩形 / 曲线 / 控制点 / 变换
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- suse系统卸载数据库实例
1.停止数据库: 2.执行以下命令: find $ORACLE_BASE/* -name '*[Tt][Ee][Ss][Tt]*' 其中TEST为数据库的实例名: 删除存在的文件: 3.删除/etc ...