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八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...
随机推荐
- What does it mean for an algorithm to be fair
What does it mean for an algorithm to be fair In 2014 the White House commissioned a 90-day study th ...
- Unity3d 协程的注意问题(新手须注意,老手须加勉)
关于unity3d的协程,非常的好用,比如等待几秒执行,等待下一帧执行等! 但是也有潜在的问题: 1.协程是单线程的,在主线程中完成 2.如果发现yield, 那么这一帧会结束,那么等下一帧调用此脚本 ...
- 【UVA 11383】 Golden Tiger Claw (KM算法副产物)
Omi, Raymondo, Clay and Kimiko are on new adventure- in search of new Shen Gong Wu. But EvilBoy Geni ...
- PCR理解
http://blog.csdn.net/niehanzi/article/details/4450154 PCR的物理意义: PCR存在于TS包的自适应域中,如下图: PCR用来同步前端编码器和后端 ...
- win7下登陆中国银行网上银行IE浏览器版本过高问题解决
2013-11-23 2013-11-23登录中国银行出现以下提示. 操作系统为windows7 64位旗舰版,搜狗浏览器版本为4.1.1.7598.想不到突然出现如下提示.打电话去问,告知使用IE7 ...
- INFORMATION_SCHEMA.INNODB_LOCKS
INNODB_LOCKS Table: INNODB_LOCKS 表 包含信息关于每个锁 一个InnoDB 事务已经请求 但是没有获得锁, 每个lock 一个事务持有是堵塞另外一个事务 centos6 ...
- java学习多线程之创建多线程一
现在我们有这么一个需求,就是在主线程在运行的同时,我们想做其他的任务,这个时候我们就用到了多线程.那么如何创建多线程,我们知道在系统当中qq的多线程创建是由操作系统来完成的,那么如果我们想在java当 ...
- 数据库设主键以及where的应用
二.第二课 create table teacher ( tno int primary key identity(1,1), --将tno设为主键(primary key identity(1,1 ...
- ☀【组件】getRequest
→ GitHub <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...
- Java中Map遍历的四种方案
在Java中如何遍历Map对象 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashM ...