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装箱拆箱,以及面向对象的小代码的更多相关文章

  1. java中的包装类与装箱拆箱定义

    JAVA 中int类型转String类型的通常方法,有三种:  1.String.valueOf(int i)  2.Integer.toString(int i)  3.i+"" ...

  2. java中Integer与int装箱拆箱一点收获

    示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...

  3. Java 性能要点:自动装箱/ 拆箱 (Autoboxing / Unboxing)

    [编者按]本文作者为 Ali Kemal TASCI,最早于2016年4月9日发布于DZONE社区.文章主要介绍通过改进 Java 1.5 就已存在的骨灰级特性大幅度提高应用性能. 本文系 OneAP ...

  4. JDK5.0新特性(静态导入、自动装箱/拆箱、增强for循环、可变参数、枚举、泛形)

    JDK5中新增了很多新的java特性,利用这些新语法可以帮助开发人员编写出更加高效.清晰,安全的代码. 这些新特性主要有:1.静态导入2.自动装箱/拆箱3.增强for循环4.可变参数5.枚举6.泛型7 ...

  5. Java中的装箱拆箱

    一)  装箱与拆箱 Java中有概念是一切皆对象,因为所有的类都默认继承自Object.但是,对于数据类型是个例外,如short,int,long,float,double, byte,char,bo ...

  6. Java 装箱 拆箱

    Java 自动装箱与拆箱   ??什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我 ...

  7. Java 的自动装箱拆箱

    Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类.以下是基本数据类型对应的包装类: 基本数据类型 包装类 byte(1字节) Byte short(2字节) Short int ...

  8. Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口

    Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...

  9. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?

    参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...

随机推荐

  1. php 文件上传 以及保存在本地的乱码问题处理

    要知道两点: ①浏览器传到PHP程序中是UTF-8编码 ②PHP程序保存上传的文件,要转换成GBK编码才保存在本地中,否则如果直接使用浏览器传过来的文件名保存在本地,会出现文件名乱码. <?ph ...

  2. PHP 判断从表单提交的值是否为空

    @$time = $_GET['time'];if(empty($time)) { echo "empty";} else { echo "not empty" ...

  3. [wikioi]能量项链

    http://wikioi.com/problem/1154/ 这是石子归并的加强版,基本就是分治法的DP.但是有了个环,因为任何一个位置都可开始,所以就建立2*N的数组,然后对可能的区间遍历一次,就 ...

  4. Visual Studio 2015 Update 1 成功安装后运行 “出现未能正确加载[XXXX]包,此问题可能是由配置更改或安装另一个扩展导致的。” 可能的解决方法

    作死装Visual Studio 2015 update 1.安装过程中虽然波澜不惊,但是安装之后运行回报未能正确安装[XXXX]包.找了半天,在stackoverflow中找到了相关的问题,在问题描 ...

  5. 匿名hash

    [root@oadb test]# cat a1.pl use Data::Dumper; my @a=qw/1 3 5 7 9/; push @b ,{@a}; print Dumper(@b); ...

  6. c++ lambda返回类型自动推导的一些需要注意的地方

    一句话,lambda返回类型自动推导走的是auto,而不是decltype,注意. class ObjectA { public: ObjectA() { val_ = ++g; } ObjectA( ...

  7. 区别typedef和#define

    1) #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错.例如:#define PI 3.1415 ...

  8. Tyvj P3119 核电站问题 动态规划

    题目:http://tyvj.cn/p/3119 P3119 核电站问题 时间: 1000ms / 空间: 65536KiB / Java类名: Main 描述 一个核电站有N个放核物质的坑,坑排列在 ...

  9. Cogs 1709. [SPOJ705]不同的子串 后缀数组

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1709 1709. [SPOJ705]不同的子串 ★★   输入文件:subst1.in   输出文件: ...

  10. It appears as though you do not have permission to view information for any of the services you requested